reverse_markdown 0.4.7 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +2 -3
- data/LICENSE +13 -0
- data/README.md +69 -30
- data/bin/reverse_markdown +1 -1
- data/lib/reverse_markdown/cleaner.rb +66 -0
- data/lib/reverse_markdown/config.rb +20 -0
- data/lib/reverse_markdown/converters/a.rb +19 -0
- data/lib/reverse_markdown/converters/base.rb +24 -0
- data/lib/reverse_markdown/converters/blockquote.rb +13 -0
- data/lib/reverse_markdown/converters/br.rb +11 -0
- data/lib/reverse_markdown/converters/code.rb +11 -0
- data/lib/reverse_markdown/converters/div.rb +11 -0
- data/lib/reverse_markdown/converters/dump.rb +9 -0
- data/lib/reverse_markdown/converters/em.rb +21 -0
- data/lib/reverse_markdown/converters/h.rb +17 -0
- data/lib/reverse_markdown/converters/hr.rb +11 -0
- data/lib/reverse_markdown/converters/img.rb +14 -0
- data/lib/reverse_markdown/converters/li.rb +28 -0
- data/lib/reverse_markdown/converters/ol.rb +12 -0
- data/lib/reverse_markdown/converters/p.rb +11 -0
- data/lib/reverse_markdown/converters/pass_through.rb +14 -0
- data/lib/reverse_markdown/converters/pre.rb +17 -0
- data/lib/reverse_markdown/converters/strong.rb +21 -0
- data/lib/reverse_markdown/converters/table.rb +11 -0
- data/lib/reverse_markdown/converters/td.rb +13 -0
- data/lib/reverse_markdown/converters/text.rb +30 -0
- data/lib/reverse_markdown/converters/tr.rb +21 -0
- data/lib/reverse_markdown/converters.rb +22 -0
- data/lib/reverse_markdown/errors.rb +6 -2
- data/lib/reverse_markdown/version.rb +1 -1
- data/lib/reverse_markdown.rb +40 -8
- data/reverse_markdown.gemspec +3 -1
- data/spec/assets/lists.html +24 -0
- data/spec/assets/unknown_tags.html +9 -0
- data/spec/components/anchors_spec.rb +5 -5
- data/spec/components/basic_spec.rb +5 -5
- data/spec/components/code_spec.rb +5 -5
- data/spec/components/escapables_spec.rb +3 -3
- data/spec/components/from_the_wild_spec.rb +5 -5
- data/spec/components/html_fragment_spec.rb +2 -2
- data/spec/components/lists_spec.rb +18 -7
- data/spec/components/paragraphs_spec.rb +2 -2
- data/spec/components/quotation_spec.rb +5 -5
- data/spec/components/tables_spec.rb +2 -2
- data/spec/components/unknown_tags_spec.rb +21 -0
- data/spec/html_to_markdown_to_html_spec.rb +1 -2
- data/spec/lib/reverse_markdown/cleaner_spec.rb +76 -0
- data/spec/lib/reverse_markdown/converters/blockquote_spec.rb +18 -0
- data/spec/lib/reverse_markdown/converters/br_spec.rb +9 -0
- data/spec/lib/reverse_markdown_spec.rb +37 -0
- data/spec/spec_helper.rb +9 -1
- metadata +83 -24
- data/License-MIT +0 -7
- data/lib/reverse_markdown/mapper.rb +0 -241
- data/spec/mapper_spec.rb +0 -63
- data/spec/reverse_markdown_spec.rb +0 -18
@@ -0,0 +1,22 @@
|
|
1
|
+
module ReverseMarkdown
|
2
|
+
module Converters
|
3
|
+
def self.register(tag_name, converter)
|
4
|
+
@@converters ||= {}
|
5
|
+
@@converters[tag_name.to_sym] = converter
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.lookup(tag_name)
|
9
|
+
@@converters[tag_name.to_sym] or default_converter(tag_name)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def self.default_converter(tag_name)
|
15
|
+
if ReverseMarkdown.config.ignore_unknown_tags
|
16
|
+
ReverseMarkdown::Converters::Dump.new
|
17
|
+
else
|
18
|
+
raise UnknownTagError, "unknown tag: #{tag_name}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/reverse_markdown.rb
CHANGED
@@ -1,24 +1,56 @@
|
|
1
1
|
require 'digest'
|
2
|
+
require 'nokogiri'
|
2
3
|
require 'reverse_markdown/version'
|
3
|
-
require 'reverse_markdown/mapper'
|
4
4
|
require 'reverse_markdown/errors'
|
5
|
-
require '
|
5
|
+
require 'reverse_markdown/cleaner'
|
6
|
+
require 'reverse_markdown/config'
|
7
|
+
require 'reverse_markdown/converters'
|
8
|
+
require 'reverse_markdown/converters/base'
|
9
|
+
require 'reverse_markdown/converters/a'
|
10
|
+
require 'reverse_markdown/converters/blockquote'
|
11
|
+
require 'reverse_markdown/converters/br'
|
12
|
+
require 'reverse_markdown/converters/code'
|
13
|
+
require 'reverse_markdown/converters/div'
|
14
|
+
require 'reverse_markdown/converters/dump'
|
15
|
+
require 'reverse_markdown/converters/em'
|
16
|
+
require 'reverse_markdown/converters/h'
|
17
|
+
require 'reverse_markdown/converters/hr'
|
18
|
+
require 'reverse_markdown/converters/img'
|
19
|
+
require 'reverse_markdown/converters/li'
|
20
|
+
require 'reverse_markdown/converters/ol'
|
21
|
+
require 'reverse_markdown/converters/p'
|
22
|
+
require 'reverse_markdown/converters/pass_through'
|
23
|
+
require 'reverse_markdown/converters/pre'
|
24
|
+
require 'reverse_markdown/converters/strong'
|
25
|
+
require 'reverse_markdown/converters/table'
|
26
|
+
require 'reverse_markdown/converters/td'
|
27
|
+
require 'reverse_markdown/converters/tr'
|
28
|
+
require 'reverse_markdown/converters/text'
|
6
29
|
|
7
30
|
module ReverseMarkdown
|
8
31
|
|
9
|
-
def self.
|
32
|
+
def self.convert(input, options = {})
|
10
33
|
root = case input
|
11
34
|
when String then Nokogiri::HTML(input).root
|
12
35
|
when Nokogiri::XML::Document then input.root
|
13
36
|
when Nokogiri::XML::Node then input
|
14
37
|
end
|
15
38
|
|
16
|
-
|
39
|
+
root or return ''
|
40
|
+
config.apply(options)
|
41
|
+
result = ReverseMarkdown::Converters.lookup(root.name).convert(root)
|
42
|
+
config.reset
|
43
|
+
cleaner.tidy(result)
|
17
44
|
end
|
18
45
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
46
|
+
def self.config
|
47
|
+
@config ||= Config.new
|
48
|
+
yield @config if block_given?
|
49
|
+
@config
|
23
50
|
end
|
51
|
+
|
52
|
+
def self.cleaner
|
53
|
+
@cleaner ||= Cleaner.new
|
54
|
+
end
|
55
|
+
|
24
56
|
end
|
data/reverse_markdown.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.authors = ["Johannes Opper"]
|
9
9
|
s.email = ["xijo@gmx.de"]
|
10
10
|
s.homepage = "http://github.com/xijo/reverse_markdown"
|
11
|
-
s.summary = %q{
|
11
|
+
s.summary = %q{Convert html code into markdown.}
|
12
12
|
s.description = %q{Map simple html back into markdown, e.g. if you want to import existing html data in your application.}
|
13
13
|
|
14
14
|
s.rubyforge_project = "reverse_markdown"
|
@@ -24,4 +24,6 @@ Gem::Specification.new do |s|
|
|
24
24
|
s.add_development_dependency 'simplecov'
|
25
25
|
s.add_development_dependency 'rake'
|
26
26
|
s.add_development_dependency 'redcarpet'
|
27
|
+
s.add_development_dependency 'byebug'
|
28
|
+
s.add_development_dependency 'codeclimate-test-reporter'
|
27
29
|
end
|
data/spec/assets/lists.html
CHANGED
@@ -52,5 +52,29 @@
|
|
52
52
|
<li><p>li 2, p 1</p></li>
|
53
53
|
</ul>
|
54
54
|
|
55
|
+
<ol>
|
56
|
+
<li>
|
57
|
+
one
|
58
|
+
<ol>
|
59
|
+
<li>one one</li>
|
60
|
+
<li>one two</li>
|
61
|
+
</ol>
|
62
|
+
</li>
|
63
|
+
<li>
|
64
|
+
two
|
65
|
+
<ol>
|
66
|
+
<li>
|
67
|
+
two one
|
68
|
+
<ol>
|
69
|
+
<li>two one one</li>
|
70
|
+
<li>two one two</li>
|
71
|
+
</ol>
|
72
|
+
</li>
|
73
|
+
<li>two two</li>
|
74
|
+
</ol>
|
75
|
+
</li>
|
76
|
+
<li>three</li>
|
77
|
+
</ol>
|
78
|
+
|
55
79
|
</body>
|
56
80
|
</html>
|
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ReverseMarkdown
|
3
|
+
describe ReverseMarkdown do
|
4
4
|
|
5
5
|
let(:input) { File.read('spec/assets/anchors.html') }
|
6
6
|
let(:document) { Nokogiri::HTML(input) }
|
7
|
-
subject { ReverseMarkdown.
|
7
|
+
subject { ReverseMarkdown.convert(input) }
|
8
8
|
|
9
9
|
it { should include ' [Foobar](http://foobar.com) ' }
|
10
|
-
it { should include ' [Fubar](http://foobar.com "f
|
10
|
+
it { should include ' [Fubar](http://foobar.com "f\*\*\*\*\* up beyond all recognition") ' }
|
11
11
|
it { should include ' [**Strong foobar**](http://strong.foobar.com) ' }
|
12
12
|
|
13
13
|
it { should include '  ' }
|
@@ -18,9 +18,9 @@ describe ReverseMarkdown::Mapper do
|
|
18
18
|
|
19
19
|
context "links to ignore" do
|
20
20
|
it { should include ' ignore anchor tags with no link text ' }
|
21
|
-
it { should include ' not ignore [
|
21
|
+
it { should include ' not ignore [](foo.html) anchor tags with images' }
|
22
22
|
it { should include ' pass through the text of internal jumplinks without treating them as links ' }
|
23
23
|
it { should include ' pass through the text of anchor tags with no href without treating them as links ' }
|
24
24
|
end
|
25
25
|
|
26
|
-
end
|
26
|
+
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ReverseMarkdown
|
3
|
+
describe ReverseMarkdown do
|
4
4
|
|
5
5
|
let(:input) { File.read('spec/assets/basic.html') }
|
6
6
|
let(:document) { Nokogiri::HTML(input) }
|
7
|
-
subject { ReverseMarkdown.
|
7
|
+
subject { ReverseMarkdown.convert(input) }
|
8
8
|
|
9
9
|
it { should match /plain text ?\n/ }
|
10
10
|
it { should match /# h1\n/ }
|
@@ -25,7 +25,7 @@ describe ReverseMarkdown::Mapper do
|
|
25
25
|
it { should match /before and after strong tags containing whitespace/ }
|
26
26
|
it { should match /\*\*double strong tags\*\*/ }
|
27
27
|
it { should match /\*\*double strong tags in p tag\*\*/ }
|
28
|
-
it { should match /before \*\*
|
28
|
+
it { should match /before \*\*double strong tags containing whitespace\*\* after/ }
|
29
29
|
|
30
30
|
it { should match /_i tag content_/ }
|
31
31
|
it { should match /\*\*b tag content\*\*/ }
|
@@ -33,7 +33,7 @@ describe ReverseMarkdown::Mapper do
|
|
33
33
|
it { should match /br tags become double space followed by newline \n/ }
|
34
34
|
#it { should match /br tags XXX \n/ }
|
35
35
|
|
36
|
-
it { should match
|
36
|
+
it { should match /before hr \n\* \* \*\n after hr/ }
|
37
37
|
|
38
38
|
it { should match /section 1\n ?\nsection 2/ }
|
39
|
-
end
|
39
|
+
end
|
@@ -1,24 +1,24 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ReverseMarkdown
|
3
|
+
describe ReverseMarkdown do
|
4
4
|
|
5
5
|
let(:input) { File.read('spec/assets/code.html') }
|
6
6
|
let(:document) { Nokogiri::HTML(input) }
|
7
|
-
subject { ReverseMarkdown.
|
7
|
+
subject { ReverseMarkdown.convert(input) }
|
8
8
|
|
9
9
|
it { should match /inline `code` block/ }
|
10
10
|
it { should match /\ var this\;\n this\.is/ }
|
11
11
|
it { should match /block"\)\n console/ }
|
12
12
|
|
13
13
|
context "with github style code blocks" do
|
14
|
-
subject { ReverseMarkdown.
|
14
|
+
subject { ReverseMarkdown.convert(input, github_flavored: true) }
|
15
15
|
it { should match /inline `code` block/ }
|
16
16
|
it { should match /```\nvar this\;\nthis/ }
|
17
|
-
it { should match /it is"\) ?\n
|
17
|
+
it { should match /it is"\) ?\n```/ }
|
18
18
|
end
|
19
19
|
|
20
20
|
context "code with indentation" do
|
21
|
-
subject { ReverseMarkdown.
|
21
|
+
subject { ReverseMarkdown.convert(input) }
|
22
22
|
it { should match(/^ tell application "Foo"\n/) }
|
23
23
|
it { should match(/^ beep\n/) }
|
24
24
|
it { should match(/^ end tell\n/) }
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ReverseMarkdown
|
3
|
+
describe ReverseMarkdown do
|
4
4
|
|
5
5
|
let(:input) { File.read('spec/assets/escapables.html') }
|
6
6
|
let(:document) { Nokogiri::HTML(input) }
|
7
|
-
subject { ReverseMarkdown.
|
7
|
+
subject { ReverseMarkdown.convert(input) }
|
8
8
|
|
9
9
|
context "multiple asterisks" do
|
10
10
|
it { should include ' \*\*two asterisks\*\* ' }
|
@@ -19,4 +19,4 @@ describe ReverseMarkdown::Mapper do
|
|
19
19
|
context "underscores within words in code blocks" do
|
20
20
|
it { should include ' var theoretical_max_infin = 1.0;' }
|
21
21
|
end
|
22
|
-
end
|
22
|
+
end
|
@@ -1,16 +1,16 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ReverseMarkdown
|
3
|
+
describe ReverseMarkdown do
|
4
4
|
|
5
5
|
let(:input) { File.read('spec/assets/from_the_wild.html') }
|
6
6
|
let(:document) { Nokogiri::HTML(input) }
|
7
|
-
subject { ReverseMarkdown.
|
7
|
+
subject { ReverseMarkdown.convert(input) }
|
8
8
|
|
9
9
|
it "should make sense of strong-crazy markup (as seen in the wild)" do
|
10
10
|
subject.should ==
|
11
11
|
'** .' + " \n" +
|
12
|
-
'\*\*\* intentcast ** : logo design' + "
|
13
|
-
|
12
|
+
' \*\*\* intentcast ** : logo design' + " \n" +
|
13
|
+
"**.**\n\n"
|
14
14
|
end
|
15
15
|
|
16
|
-
end
|
16
|
+
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ReverseMarkdown
|
3
|
+
describe ReverseMarkdown do
|
4
4
|
|
5
5
|
let(:input) { File.read('spec/assets/html_fragment.html') }
|
6
6
|
let(:document) { Nokogiri::HTML(input) }
|
7
|
-
subject { ReverseMarkdown.
|
7
|
+
subject { ReverseMarkdown.convert(input) }
|
8
8
|
|
9
9
|
it { should == "naked text 1\n\nparagraph text\n\nnaked text 2" }
|
10
10
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ReverseMarkdown
|
3
|
+
describe ReverseMarkdown do
|
4
4
|
|
5
5
|
let(:input) { File.read('spec/assets/lists.html') }
|
6
6
|
let(:document) { Nokogiri::HTML(input) }
|
7
|
-
subject { ReverseMarkdown.
|
7
|
+
subject { ReverseMarkdown.convert(input) }
|
8
8
|
|
9
9
|
it { should match /\n- unordered list entry\n/ }
|
10
10
|
it { should match /\n- unordered list entry 2\n/ }
|
@@ -22,10 +22,10 @@ describe ReverseMarkdown::Mapper do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
context "nested list with lots of whitespace" do
|
25
|
-
it { should match /\n- item wa\n/ }
|
26
|
-
it { should match /\n- item wb\n/ }
|
27
|
-
it { should match /\n
|
28
|
-
it { should match /\n
|
25
|
+
it { should match /\n- item wa \n/ }
|
26
|
+
it { should match /\n- item wb \n/ }
|
27
|
+
it { should match /\n - item wbb \n/ }
|
28
|
+
it { should match /\n - item wbc \n/ }
|
29
29
|
end
|
30
30
|
|
31
31
|
context "lists containing links" do
|
@@ -42,4 +42,15 @@ describe ReverseMarkdown::Mapper do
|
|
42
42
|
xit { should match /\n- li 1, p 1\n\n- li 1, p 2\n/ }
|
43
43
|
end
|
44
44
|
|
45
|
-
|
45
|
+
context 'it produces correct numbering' do
|
46
|
+
it { should include "1. one" }
|
47
|
+
it { should include " 1. one one" }
|
48
|
+
it { should include " 2. one two" }
|
49
|
+
it { should include "2. two" }
|
50
|
+
it { should include " 1. two one" }
|
51
|
+
it { should include " 1. two one one" }
|
52
|
+
it { should include " 2. two one two" }
|
53
|
+
it { should include " 2. two two" }
|
54
|
+
it { should include "3. three" }
|
55
|
+
end
|
56
|
+
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ReverseMarkdown
|
3
|
+
describe ReverseMarkdown do
|
4
4
|
|
5
5
|
let(:input) { File.read('spec/assets/paragraphs.html') }
|
6
6
|
let(:document) { Nokogiri::HTML(input) }
|
7
|
-
subject { ReverseMarkdown.
|
7
|
+
subject { ReverseMarkdown.convert(input) }
|
8
8
|
|
9
9
|
it { should_not start_with "\n\n" }
|
10
10
|
it { should start_with "First content\n\nSecond content\n\n" }
|
@@ -1,12 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ReverseMarkdown
|
3
|
+
describe ReverseMarkdown do
|
4
4
|
|
5
5
|
let(:input) { File.read('spec/assets/quotation.html') }
|
6
6
|
let(:document) { Nokogiri::HTML(input) }
|
7
|
-
subject { ReverseMarkdown.
|
7
|
+
subject { ReverseMarkdown.convert(input) }
|
8
8
|
|
9
|
-
it { should
|
10
|
-
it { should include "\n> First quoted paragraph\n\n> Second quoted paragraph" }
|
9
|
+
it { should match /^ Block of code$/ }
|
10
|
+
it { should include "\n> First quoted paragraph\n> \n> Second quoted paragraph" }
|
11
11
|
|
12
|
-
end
|
12
|
+
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ReverseMarkdown
|
3
|
+
describe ReverseMarkdown do
|
4
4
|
|
5
5
|
let(:input) { File.read('spec/assets/tables.html') }
|
6
6
|
let(:document) { Nokogiri::HTML(input) }
|
7
|
-
subject { ReverseMarkdown.
|
7
|
+
subject { ReverseMarkdown.convert(input) }
|
8
8
|
|
9
9
|
it { should match /\n\| header 1 \| header 2 \| header 3 \|\n\| --- \| --- \| --- \|\n/ }
|
10
10
|
it { should match /\n\| data 1-1 \| data 2-1 \| data 3-1 \|\n/ }
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ReverseMarkdown do
|
4
|
+
|
5
|
+
let(:input) { File.read('spec/assets/unknown_tags.html') }
|
6
|
+
let(:document) { Nokogiri::HTML(input) }
|
7
|
+
let(:result) { ReverseMarkdown.convert(input) }
|
8
|
+
|
9
|
+
context 'with ignore_unknown_tags enabled' do
|
10
|
+
before { ReverseMarkdown.config.ignore_unknown_tags = true }
|
11
|
+
|
12
|
+
it { result.should include "<foo><bar>Foo with bar</bar></foo>" }
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'with ignore_unknown_tags disabled' do
|
16
|
+
before { ReverseMarkdown.config.ignore_unknown_tags = false }
|
17
|
+
|
18
|
+
it { expect { result }.to raise_error(ReverseMarkdown::UnknownTagError) }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
@@ -12,7 +12,7 @@ describe 'Round trip: HTML to markdown (via reverse_markdown) to HTML (via redca
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def html2markdown2html(orig_html)
|
15
|
-
markdown = ReverseMarkdown.
|
15
|
+
markdown = ReverseMarkdown.convert orig_html
|
16
16
|
new_html = Redcarpet::Markdown.new(Redcarpet::Render::HTML).render(markdown)
|
17
17
|
new_html
|
18
18
|
end
|
@@ -60,7 +60,6 @@ describe 'Round trip: HTML to markdown (via reverse_markdown) to HTML (via redca
|
|
60
60
|
end
|
61
61
|
|
62
62
|
it "should preserve links inside <strong> tags" do
|
63
|
-
pending
|
64
63
|
roundtrip_should_preserve(%{<p><strong><a href="/wiki/Western_philosophy" title="Western philosophy">Western philosophy</a></strong></p>})
|
65
64
|
end
|
66
65
|
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ReverseMarkdown::Cleaner do
|
4
|
+
let(:cleaner) { ReverseMarkdown::Cleaner.new }
|
5
|
+
|
6
|
+
describe '#remove_newlines' do
|
7
|
+
it 'removes more than 2 subsequent newlines' do
|
8
|
+
result = cleaner.remove_newlines("foo\n\n\nbar")
|
9
|
+
result.should eq "foo\n\nbar"
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'skips single and double newlines' do
|
13
|
+
result = cleaner.remove_newlines("foo\nbar\n\nbaz")
|
14
|
+
result.should eq "foo\nbar\n\nbaz"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#remove_inner_whitespaces' do
|
19
|
+
it 'removes duplicate whitespaces from the string' do
|
20
|
+
result = cleaner.remove_inner_whitespaces('foo bar')
|
21
|
+
result.should eq "foo bar"
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'performs changes for multiple lines' do
|
25
|
+
result = cleaner.remove_inner_whitespaces("foo bar\nbar foo")
|
26
|
+
result.should eq "foo bar\nbar foo"
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'keeps leading whitespaces' do
|
30
|
+
result = cleaner.remove_inner_whitespaces(" foo bar\n bar foo")
|
31
|
+
result.should eq " foo bar\n bar foo"
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'keeps trailing whitespaces' do
|
35
|
+
result = cleaner.remove_inner_whitespaces("foo \n")
|
36
|
+
result.should eq "foo \n"
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'keeps trailing newlines' do
|
40
|
+
result = cleaner.remove_inner_whitespaces("foo\n")
|
41
|
+
result.should eq "foo\n"
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'removes tabs as well' do
|
45
|
+
result = cleaner.remove_inner_whitespaces("foo\t \tbar")
|
46
|
+
result.should eq "foo bar"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '#clean_tag_borders' do
|
51
|
+
it 'removes not needed whitespaces from strong tags' do
|
52
|
+
input = "foo ** foobar ** bar"
|
53
|
+
result = cleaner.clean_tag_borders(input)
|
54
|
+
result.should eq "foo **foobar** bar"
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'remotes leading or trailing whitespaces independently' do
|
58
|
+
input = "1 **fat ** 2 ** fat** 3"
|
59
|
+
result = cleaner.clean_tag_borders(input)
|
60
|
+
result.should eq "1 **fat** 2 **fat** 3"
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'adds whitespaces if there are none' do
|
64
|
+
input = "1**fat**2"
|
65
|
+
result = cleaner.clean_tag_borders(input)
|
66
|
+
result.should eq "1 **fat** 2"
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'cleans italic stuff as well' do
|
70
|
+
input = "1 __italic __ 2 __ italic__ 3__italic __4"
|
71
|
+
result = cleaner.clean_tag_borders(input)
|
72
|
+
result.should eq "1 __italic__ 2 __italic__ 3 __italic__ 4"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ReverseMarkdown::Converters::Blockquote do
|
4
|
+
|
5
|
+
let(:converter) { ReverseMarkdown::Converters::Blockquote.new }
|
6
|
+
|
7
|
+
it 'converts nested elements as well' do
|
8
|
+
input = Nokogiri::XML.parse("<blockquote><ul><li>foo</li></ul></blockquote>").root
|
9
|
+
result = converter.convert(input)
|
10
|
+
result.should eq "> - foo"
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'can deal with paragraphs inside' do
|
14
|
+
input = Nokogiri::XML.parse("<blockquote><p>Some text.</p><p>Some more text.</p></blockquote>").root
|
15
|
+
result = converter.convert(input)
|
16
|
+
result.should eq "> Some text.\n> \n> Some more text."
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ReverseMarkdown do
|
4
|
+
let(:input) { File.read('spec/assets/minimum.html') }
|
5
|
+
let(:document) { Nokogiri::HTML(input) }
|
6
|
+
|
7
|
+
it "parses nokogiri documents" do
|
8
|
+
lambda { ReverseMarkdown.convert(document) }.should_not raise_error
|
9
|
+
end
|
10
|
+
|
11
|
+
it "parses nokogiri elements" do
|
12
|
+
lambda { ReverseMarkdown.convert(document.root) }.should_not raise_error
|
13
|
+
end
|
14
|
+
|
15
|
+
it "parses string input" do
|
16
|
+
lambda { ReverseMarkdown.convert(input) }.should_not raise_error
|
17
|
+
end
|
18
|
+
|
19
|
+
it "behaves in a sane way when root element is nil" do
|
20
|
+
ReverseMarkdown.convert(nil).should == ''
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#config' do
|
24
|
+
it 'stores a given configuration option' do
|
25
|
+
ReverseMarkdown.config.github_flavored = true
|
26
|
+
ReverseMarkdown.config.github_flavored.should be_true
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'can be used as a block configurator as well' do
|
30
|
+
ReverseMarkdown.config do |config|
|
31
|
+
config.github_flavored.should be_false
|
32
|
+
config.github_flavored = true
|
33
|
+
end
|
34
|
+
ReverseMarkdown.config.github_flavored.should be_true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
+
require 'codeclimate-test-reporter'
|
2
|
+
CodeClimate::TestReporter.start
|
3
|
+
|
1
4
|
require 'simplecov'
|
5
|
+
require 'byebug'
|
2
6
|
|
3
7
|
SimpleCov.adapters.define 'gem' do
|
4
8
|
add_filter '/spec/'
|
@@ -11,4 +15,8 @@ require 'reverse_markdown'
|
|
11
15
|
|
12
16
|
RSpec.configure do |config|
|
13
17
|
config.color_enabled = true
|
14
|
-
|
18
|
+
|
19
|
+
config.after(:each) do
|
20
|
+
ReverseMarkdown.config.reset
|
21
|
+
end
|
22
|
+
end
|