File: html_to_markdown_to_html_spec.rb

package info (click to toggle)
ruby-reverse-markdown 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 440 kB
  • sloc: ruby: 1,452; makefile: 4
file content (113 lines) | stat: -rw-r--r-- 2,902 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# coding:utf-8

require 'kramdown'
require 'spec_helper'

describe 'Round trip: HTML to markdown (via reverse_markdown) to HTML (via redcarpet)' do

  # helpers

  def roundtrip_should_preserve(input)
    output = html2markdown2html input
    expect(normalize_html(output)).to eq normalize_html(input)
  end

  def html2markdown2html(orig_html)
    markdown = ReverseMarkdown.convert orig_html
    new_html = Kramdown::Document.new(markdown).to_html
    new_html
  end

  def normalize_html(html)
    squeeze_whitespace(html).gsub('> <', '><').strip
  end

  def squeeze_whitespace(string)
    string.tr("\n\t", ' ').squeeze(' ').gsub(/\A \z/, '')
  end

  # specs

  it "should preserve <blockquote> blocks" do
    roundtrip_should_preserve('<blockquote><p>some text</p></blockquote>')
  end

  it "should preserve unordered lists" do
    roundtrip_should_preserve("
      <ol>
        <li>Bird</li>
        <li>McHale</li>
        <li>Parish</li>
      </ol>
    ")
  end

  it "should preserve ordered lists" do
    roundtrip_should_preserve("
      <ul>
        <li>Bird</li>
        <li>McHale</li>
        <li>Parish</li>
      </ul>
    ")
  end

  it "should preserve lists with paragraphs" do
    roundtrip_should_preserve("
      <ul>
        <li><p>Bird</p></li>
        <li><p>McHale</p></li>
        <li><p>Parish</p></li>
      </ul>
      ")
  end

  it "should preserve <hr> tags" do
    roundtrip_should_preserve("<hr />")
  end

  it "should preserve <em> tags" do
    roundtrip_should_preserve("<p><em>yes!</em></p>")
  end

  it "should preserve links inside <strong> tags" do
    roundtrip_should_preserve(%{<p><strong><a href="/wiki/Western_philosophy" title="Western philosophy">Western philosophy</a></strong></p>})
  end

  it "should preserve <strong> tags" do
    roundtrip_should_preserve("<p><strong>yes!</strong></p>")
  end

  it "should preserve <br> tags" do
    roundtrip_should_preserve("<p>yes!<br />\n we can!</p>")
  end

  it "should preserve <a> tags" do
    roundtrip_should_preserve(%{<p>This is <a href="http://example.com/" title="Title">an example</a> inline link.</p>})
    roundtrip_should_preserve(%{<p><a href="http://example.net/">This link</a> has no title attribute.</p>})
  end

  it "should preserve <img> tags" do
    roundtrip_should_preserve(%{<p><img src="http://foo.bar/dog.png" alt="My Dog" title="Ralph" /></p>})
    roundtrip_should_preserve(%{<p><img src="http://foo.bar/dog.png" alt="My Dog" /></p>})
  end

  it "should preserve code blocks" do
    roundtrip_should_preserve(%{
      <p>This is a normal paragraph:</p>

      <pre><code>This is a code block. </code></pre>
    })
  end

  it "should preserve code blocks with embedded whitespace" do
    roundtrip_should_preserve(%{
      <p>Here is an example of AppleScript:</p>

      <pre><code>tell application Foo
          beep
      end tell
      </code></pre>
    })
  end
end