reverse_markdown 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/README.md +20 -6
- data/lib/reverse_markdown.rb +4 -2
- data/lib/reverse_markdown/cleaner.rb +11 -4
- data/lib/reverse_markdown/config.rb +3 -3
- data/lib/reverse_markdown/converters.rb +10 -3
- data/lib/reverse_markdown/converters/blockquote.rb +1 -1
- data/lib/reverse_markdown/converters/bypass.rb +14 -0
- data/lib/reverse_markdown/converters/del.rb +29 -0
- data/lib/reverse_markdown/converters/{dump.rb → drop.rb} +2 -2
- data/lib/reverse_markdown/converters/li.rb +1 -1
- data/lib/reverse_markdown/converters/pass_through.rb +1 -6
- data/lib/reverse_markdown/converters/pre.rb +1 -1
- data/lib/reverse_markdown/errors.rb +3 -0
- data/lib/reverse_markdown/version.rb +1 -1
- data/reverse_markdown.gemspec +1 -1
- data/spec/components/unknown_tags_spec.rb +22 -4
- data/spec/lib/reverse_markdown/cleaner_spec.rb +6 -0
- data/spec/lib/reverse_markdown/converters/del_spec.rb +38 -0
- data/spec/lib/reverse_markdown/converters/li_spec.rb +13 -0
- data/spec/spec_helper.rb +1 -1
- metadata +9 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2cd83465e91f441918e2b09ff1a9aca35b029e74
|
4
|
+
data.tar.gz: b605fad98b1666375bc80a1d16db02388a6aa43a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aad0b3de89e84616aa8c1de8af86949a3ae6559c90ed1361aac5e747f163b5e419fdebaab60288dfe0da3153890a5320fb80f0ee575800dcf3c7e49be5b0cc3d
|
7
|
+
data.tar.gz: dfde7c437ca43d2a87ed4d009b4870766b27e81e75a708092777b709eeac4795fd441c2e9652f2db91e59d02ec1131bf7016d0d6513e597d7618c93589c6b26c
|
data/README.md
CHANGED
@@ -4,20 +4,29 @@ Transform html into markdown. Useful for example if you want to import html into
|
|
4
4
|
|
5
5
|
[![Build Status](https://secure.travis-ci.org/xijo/reverse_markdown.png?branch=master)](https://travis-ci.org/xijo/reverse_markdown) [![Gem Version](https://badge.fury.io/rb/reverse_markdown.png)](http://badge.fury.io/rb/reverse_markdown) [![Code Climate](https://codeclimate.com/github/xijo/reverse_markdown.png)](https://codeclimate.com/github/xijo/reverse_markdown) [![Code Climate](https://codeclimate.com/github/xijo/reverse_markdown/coverage.png)](https://codeclimate.com/github/xijo/reverse_markdown)
|
6
6
|
|
7
|
-
##
|
7
|
+
## Changelog
|
8
|
+
|
9
|
+
### 0.5.1 - April 2014
|
10
|
+
|
11
|
+
1. Adds support for ruby versions 1.9.3 back in
|
12
|
+
2. More options for handling of unknown tags
|
13
|
+
3. Bugfixes in `li` indentation behavior
|
14
|
+
|
15
|
+
### 0.5.0 - March 2014
|
8
16
|
|
9
17
|
There were some breaking changes, please make sure you don't miss them:
|
10
18
|
|
11
|
-
1. Only ruby versions 2.0.0 or
|
19
|
+
1. Only ruby versions 2.0.0 or above are supported
|
12
20
|
2. There is no `Mapper` class any more. Just use `ReverseMarkdown.convert(input, options)`
|
13
21
|
3. Config option `github_style_code_blocks` changed its name to `github_flavored`
|
14
22
|
|
15
23
|
Please open an issue and let me know about it if you have any trouble with the new version.
|
16
24
|
|
25
|
+
|
17
26
|
## Requirements
|
18
27
|
|
19
28
|
1. [Nokogiri](http://nokogiri.org/)
|
20
|
-
2. Ruby
|
29
|
+
2. Ruby 1.9.3 or higher
|
21
30
|
|
22
31
|
## Installation
|
23
32
|
|
@@ -67,15 +76,19 @@ $ cat file.html | reverse_markdown > file.md
|
|
67
76
|
|
68
77
|
The following options are available:
|
69
78
|
|
70
|
-
|
71
|
-
|
79
|
+
- `unknown_tags` (default `pass_through`) - how to handle unknown tags. Valid options are:
|
80
|
+
- `pass_through` - Include the unknown tag completely into the result
|
81
|
+
- `drop` - Drop the unknown tag and its content
|
82
|
+
- `bypass` - Ignore the unknown tag but try to convert its content
|
83
|
+
- `raise` - Raise an error to let you know
|
84
|
+
- `github_flavored` (default `false`) - use [github flavored markdown](https://help.github.com/articles/github-flavored-markdown) (yet only code blocks are supported)
|
72
85
|
|
73
86
|
### As options
|
74
87
|
|
75
88
|
Just pass your chosen configuration options in after the input
|
76
89
|
|
77
90
|
```ruby
|
78
|
-
ReverseMarkdown.convert(input,
|
91
|
+
ReverseMarkdown.convert(input, unknown_tags: :raise, github_flavored: true)
|
79
92
|
```
|
80
93
|
|
81
94
|
### Preconfigure
|
@@ -92,6 +105,7 @@ end
|
|
92
105
|
|
93
106
|
# Related stuff
|
94
107
|
|
108
|
+
- [Write custom converters](https://github.com/xijo/reverse_markdown/wiki/Write-your-own-converter) - Wiki entry about how to write your own converter
|
95
109
|
- [html_massage](https://github.com/harlantwood/html_massage) - A gem by Harlan T. Wood to convert regular sites into markdown using reverse_markdown
|
96
110
|
- [word-to-markdown](https://github.com/benbalter/word-to-markdown) - Convert word docs into markdown while using reverse_markdown, by Ben Balter
|
97
111
|
- [markdown syntax](http://daringfireball.net/projects/markdown) - The markdown syntax specification
|
data/lib/reverse_markdown.rb
CHANGED
@@ -9,9 +9,11 @@ require 'reverse_markdown/converters/base'
|
|
9
9
|
require 'reverse_markdown/converters/a'
|
10
10
|
require 'reverse_markdown/converters/blockquote'
|
11
11
|
require 'reverse_markdown/converters/br'
|
12
|
+
require 'reverse_markdown/converters/bypass'
|
12
13
|
require 'reverse_markdown/converters/code'
|
14
|
+
require 'reverse_markdown/converters/del'
|
13
15
|
require 'reverse_markdown/converters/div'
|
14
|
-
require 'reverse_markdown/converters/
|
16
|
+
require 'reverse_markdown/converters/drop'
|
15
17
|
require 'reverse_markdown/converters/em'
|
16
18
|
require 'reverse_markdown/converters/h'
|
17
19
|
require 'reverse_markdown/converters/hr'
|
@@ -24,8 +26,8 @@ require 'reverse_markdown/converters/pre'
|
|
24
26
|
require 'reverse_markdown/converters/strong'
|
25
27
|
require 'reverse_markdown/converters/table'
|
26
28
|
require 'reverse_markdown/converters/td'
|
27
|
-
require 'reverse_markdown/converters/tr'
|
28
29
|
require 'reverse_markdown/converters/text'
|
30
|
+
require 'reverse_markdown/converters/tr'
|
29
31
|
|
30
32
|
module ReverseMarkdown
|
31
33
|
|
@@ -38,6 +38,12 @@ module ReverseMarkdown
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
+
result = result.gsub /\s?~{2,}.*?~{2,}\s?/ do |match|
|
42
|
+
preserve_border_whitespaces(match, default_border: ' ') do
|
43
|
+
match.strip.sub('~~ ', '~~').sub(' ~~', '~~')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
41
47
|
result.gsub /\s?\[.*?\]\s?/ do |match|
|
42
48
|
preserve_border_whitespaces(match) do
|
43
49
|
match.strip.sub('[ ', '[').sub(' ]', ']')
|
@@ -47,10 +53,11 @@ module ReverseMarkdown
|
|
47
53
|
|
48
54
|
private
|
49
55
|
|
50
|
-
def preserve_border_whitespaces(string,
|
51
|
-
|
52
|
-
|
53
|
-
|
56
|
+
def preserve_border_whitespaces(string, options = {}, &block)
|
57
|
+
default_border = options.fetch(:default_border, '')
|
58
|
+
string_start = present_or_default(string[/\A\s*/], default_border)
|
59
|
+
string_end = present_or_default(string[/\s*\Z/], default_border)
|
60
|
+
result = yield
|
54
61
|
string_start + result + string_end
|
55
62
|
end
|
56
63
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module ReverseMarkdown
|
2
2
|
class Config
|
3
|
-
attr_accessor :
|
3
|
+
attr_accessor :unknown_tags, :github_flavored
|
4
4
|
|
5
5
|
def initialize
|
6
6
|
reset
|
@@ -13,8 +13,8 @@ module ReverseMarkdown
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def reset
|
16
|
-
@
|
17
|
-
@github_flavored
|
16
|
+
@unknown_tags = :pass_through
|
17
|
+
@github_flavored = false
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
@@ -12,10 +12,17 @@ module ReverseMarkdown
|
|
12
12
|
private
|
13
13
|
|
14
14
|
def self.default_converter(tag_name)
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
case ReverseMarkdown.config.unknown_tags.to_sym
|
16
|
+
when :pass_through
|
17
|
+
ReverseMarkdown::Converters::PassThrough.new
|
18
|
+
when :drop
|
19
|
+
ReverseMarkdown::Converters::Drop.new
|
20
|
+
when :bypass
|
21
|
+
ReverseMarkdown::Converters::Bypass.new
|
22
|
+
when :raise
|
18
23
|
raise UnknownTagError, "unknown tag: #{tag_name}"
|
24
|
+
else
|
25
|
+
raise InvalidConfigurationError, "unknown value #{ReverseMarkdown.config.unknown_tags.inspect} for ReverseMarkdown.config.unknown_tags"
|
19
26
|
end
|
20
27
|
end
|
21
28
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module ReverseMarkdown
|
2
|
+
module Converters
|
3
|
+
class Bypass < Base
|
4
|
+
def convert(node)
|
5
|
+
treat_children(node)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
register :document, Bypass.new
|
10
|
+
register :html, Bypass.new
|
11
|
+
register :body, Bypass.new
|
12
|
+
register :span, Bypass.new
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module ReverseMarkdown
|
2
|
+
module Converters
|
3
|
+
class Del < Base
|
4
|
+
def convert(node)
|
5
|
+
content = treat_children(node)
|
6
|
+
if disabled? || content.strip.empty? || already_crossed_out?(node)
|
7
|
+
content
|
8
|
+
else
|
9
|
+
"~~#{content}~~"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def enabled?
|
14
|
+
ReverseMarkdown.config.github_flavored
|
15
|
+
end
|
16
|
+
|
17
|
+
def disabled?
|
18
|
+
!enabled?
|
19
|
+
end
|
20
|
+
|
21
|
+
def already_crossed_out?(node)
|
22
|
+
node.ancestors('del').size > 0 || node.ancestors('strike').size > 0
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
register :strike, Del.new
|
27
|
+
register :del, Del.new
|
28
|
+
end
|
29
|
+
end
|
@@ -2,13 +2,8 @@ module ReverseMarkdown
|
|
2
2
|
module Converters
|
3
3
|
class PassThrough < Base
|
4
4
|
def convert(node)
|
5
|
-
|
5
|
+
node.to_s
|
6
6
|
end
|
7
7
|
end
|
8
|
-
|
9
|
-
register :document, PassThrough.new
|
10
|
-
register :html, PassThrough.new
|
11
|
-
register :body, PassThrough.new
|
12
|
-
register :span, PassThrough.new
|
13
8
|
end
|
14
9
|
end
|
data/reverse_markdown.gemspec
CHANGED
@@ -24,6 +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'
|
27
|
+
# s.add_development_dependency 'byebug'
|
28
28
|
s.add_development_dependency 'codeclimate-test-reporter'
|
29
29
|
end
|
@@ -6,16 +6,34 @@ describe ReverseMarkdown do
|
|
6
6
|
let(:document) { Nokogiri::HTML(input) }
|
7
7
|
let(:result) { ReverseMarkdown.convert(input) }
|
8
8
|
|
9
|
-
context 'with
|
10
|
-
before { ReverseMarkdown.config.
|
9
|
+
context 'with unknown_tags = :pass_through' do
|
10
|
+
before { ReverseMarkdown.config.unknown_tags = :pass_through }
|
11
11
|
|
12
12
|
it { result.should include "<foo><bar>Foo with bar</bar></foo>" }
|
13
13
|
end
|
14
14
|
|
15
|
-
context 'with
|
16
|
-
before { ReverseMarkdown.config.
|
15
|
+
context 'with unknown_tags = :raise' do
|
16
|
+
before { ReverseMarkdown.config.unknown_tags = :raise }
|
17
17
|
|
18
18
|
it { expect { result }.to raise_error(ReverseMarkdown::UnknownTagError) }
|
19
19
|
end
|
20
|
+
|
21
|
+
context 'with unknown_tags = :drop' do
|
22
|
+
before { ReverseMarkdown.config.unknown_tags = :drop }
|
23
|
+
|
24
|
+
it { result.should eq '' }
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with unknown_tags = :bypass' do
|
28
|
+
before { ReverseMarkdown.config.unknown_tags = :bypass }
|
29
|
+
|
30
|
+
it { result.should eq "Foo with bar\n\n" }
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'with unknown_tags = :something_wrong' do
|
34
|
+
before { ReverseMarkdown.config.unknown_tags = :something_wrong }
|
35
|
+
|
36
|
+
it { expect { result }.to raise_error(ReverseMarkdown::InvalidConfigurationError) }
|
37
|
+
end
|
20
38
|
end
|
21
39
|
|
@@ -71,6 +71,12 @@ describe ReverseMarkdown::Cleaner do
|
|
71
71
|
result = cleaner.clean_tag_borders(input)
|
72
72
|
result.should eq "1 __italic__ 2 __italic__ 3 __italic__ 4"
|
73
73
|
end
|
74
|
+
|
75
|
+
it 'cleans strikethrough stuff as well' do
|
76
|
+
input = "1 ~~italic ~~ 2 ~~ italic~~ 3~~italic ~~4"
|
77
|
+
result = cleaner.clean_tag_borders(input)
|
78
|
+
result.should eq "1 ~~italic~~ 2 ~~italic~~ 3 ~~italic~~ 4"
|
79
|
+
end
|
74
80
|
end
|
75
81
|
|
76
82
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ReverseMarkdown::Converters::Del do
|
4
|
+
let(:converter) { ReverseMarkdown::Converters::Del.new }
|
5
|
+
|
6
|
+
context 'with github_flavored = true' do
|
7
|
+
before { ReverseMarkdown.config.github_flavored = true }
|
8
|
+
|
9
|
+
it 'converts the input as expected' do
|
10
|
+
input = Nokogiri::XML.parse('<del>deldeldel</del>').root
|
11
|
+
converter.convert(input).should eq '~~deldeldel~~'
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'skips empty tags' do
|
15
|
+
input = Nokogiri::XML.parse('<del></del>').root
|
16
|
+
converter.convert(input).should eq ''
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'knows about its enabled/disabled state' do
|
20
|
+
converter.should be_enabled
|
21
|
+
converter.should_not be_disabled
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'with github_flavored = false' do
|
26
|
+
before { ReverseMarkdown.config.github_flavored = false }
|
27
|
+
|
28
|
+
it 'does not convert anything' do
|
29
|
+
input = Nokogiri::XML.parse('<del>deldeldel</del>').root
|
30
|
+
converter.convert(input).should eq 'deldeldel'
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'knows about its enabled/disabled state' do
|
34
|
+
converter.should_not be_enabled
|
35
|
+
converter.should be_disabled
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ReverseMarkdown::Converters::Li do
|
4
|
+
|
5
|
+
let(:converter) { ReverseMarkdown::Converters::Li.new }
|
6
|
+
|
7
|
+
it 'does not fail without a valid parent context' do
|
8
|
+
input = Nokogiri::XML.parse("<li>foo</li>").root
|
9
|
+
result = converter.convert(input)
|
10
|
+
result.should eq "- foo\n"
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reverse_markdown
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Johannes Opper
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -80,20 +80,6 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: byebug
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - ">="
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '0'
|
90
|
-
type: :development
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - ">="
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: '0'
|
97
83
|
- !ruby/object:Gem::Dependency
|
98
84
|
name: codeclimate-test-reporter
|
99
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -132,9 +118,11 @@ files:
|
|
132
118
|
- lib/reverse_markdown/converters/base.rb
|
133
119
|
- lib/reverse_markdown/converters/blockquote.rb
|
134
120
|
- lib/reverse_markdown/converters/br.rb
|
121
|
+
- lib/reverse_markdown/converters/bypass.rb
|
135
122
|
- lib/reverse_markdown/converters/code.rb
|
123
|
+
- lib/reverse_markdown/converters/del.rb
|
136
124
|
- lib/reverse_markdown/converters/div.rb
|
137
|
-
- lib/reverse_markdown/converters/
|
125
|
+
- lib/reverse_markdown/converters/drop.rb
|
138
126
|
- lib/reverse_markdown/converters/em.rb
|
139
127
|
- lib/reverse_markdown/converters/h.rb
|
140
128
|
- lib/reverse_markdown/converters/hr.rb
|
@@ -180,6 +168,8 @@ files:
|
|
180
168
|
- spec/lib/reverse_markdown/cleaner_spec.rb
|
181
169
|
- spec/lib/reverse_markdown/converters/blockquote_spec.rb
|
182
170
|
- spec/lib/reverse_markdown/converters/br_spec.rb
|
171
|
+
- spec/lib/reverse_markdown/converters/del_spec.rb
|
172
|
+
- spec/lib/reverse_markdown/converters/li_spec.rb
|
183
173
|
- spec/lib/reverse_markdown_spec.rb
|
184
174
|
- spec/spec_helper.rb
|
185
175
|
homepage: http://github.com/xijo/reverse_markdown
|
@@ -234,5 +224,7 @@ test_files:
|
|
234
224
|
- spec/lib/reverse_markdown/cleaner_spec.rb
|
235
225
|
- spec/lib/reverse_markdown/converters/blockquote_spec.rb
|
236
226
|
- spec/lib/reverse_markdown/converters/br_spec.rb
|
227
|
+
- spec/lib/reverse_markdown/converters/del_spec.rb
|
228
|
+
- spec/lib/reverse_markdown/converters/li_spec.rb
|
237
229
|
- spec/lib/reverse_markdown_spec.rb
|
238
230
|
- spec/spec_helper.rb
|