forked from xijo/reverse_markdown
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreverse_markdown.rb
297 lines (259 loc) · 7.23 KB
/
reverse_markdown.rb
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
require 'rexml/document'
require 'benchmark'
include REXML
include Benchmark
# reverse markdown for ruby
# author: JO
# e-mail: [email protected]
# date: 14.7.2009
# version: 0.1
# license: GPL
# taken from https://github.com/xijo/reverse-markdown/raw/master/reverse_markdown.rb
# TODO
# - ol numbering is buggy, in fact doesn't matter for markdown code
# -
class ReverseMarkdown
# set basic variables:
# - @li_counter: numbering list item (li) tags in an ordered list (ol)
# - @links: hold the links for adding them to the bottom of the @output
# this means 'reference style', please take a look at http://daringfireball.net/projects/markdown/syntax#link
# - @outout: fancy markdown code in here!
# - @indent: control indention level for nested lists
# - @errors: appearing errors, like unknown tags, go into this array
def initialize()
@li_counter = 0
@links = []
@output = ""
@indent = 0
@errors = []
end
# Invokes the HTML parsing by using a string. Returns the markdown code in @output.
# To garantuee well-formed xml for REXML a <root> element will be added, but has no effect.
# After parsing all elements, the 'reference style'-links will be inserted.
def parse_string(string)
doc = Document.new("<root>\n"+string+"\n</root>")
parse_element(doc.root, :none)
insert_links()
@output
end
# Parsing an element and its children (recursive) and writing its markdown code to @output
# 1. do indent for nested list items
# 2. add the markdown opening tag for this element
# 3a. if element only contains text, handle it like a text node
# 3b. if element is a container handle its children, which may be text- or element nodes
# 4. finally add the markdown ending tag for this element
def parse_element(element, parent)
name = element.name.to_sym
# 1.
@output << indent() if name.eql?(:li)
# 2.
@output << opening(element, parent)
# 3a.
if (element.has_text? and element.children.size < 2)
@output << text_node(element, parent)
end
# 3b.
if element.has_elements?
element.children.each do |child|
# increase indent if nested list
@indent += 1 if element.name=~/(ul|ol)/ and parent.eql?(:li)
if child.node_type.eql?(:element)
parse_element(child, element.name.to_sym)
else
if parent.eql?(:blockquote)
@output << child.to_s.gsub("\n ", "\n>")
else
@output << child.to_s
end
end
# decrease indent if end of nested list
@indent -= 1 if element.name=~/(ul|ol)/ and parent.eql?(:li)
end
end
# 4.
@output << ending(element, parent)
end
# Returns opening markdown tag for the element. Its parent matters sometimes!
def opening(type, parent)
case type.name.to_sym
when :h1
"# "
when :li
parent.eql?(:ul) ? " - " : " "+(@li_counter+=1).to_s+". "
when :ol
@li_counter = 0
""
when :ul
""
when :h2
"## "
when :h3
"### "
when :h4
"#### "
when :h5
"##### "
when :h6
"###### "
when :em
"*"
when :strong
"**"
when :blockquote
# remove leading newline
type.children.first.value = ""
"> "
when :code
parent.eql?(:pre) ? " " : "`"
when :a
"["
when :img
"!["
when :hr
"----------\n\n"
when :root
""
else
@errors << "unknown start tag: "+type.name.to_s
""
end
end
# Returns the closing markdown tag, like opening()
def ending(type, parent)
case type.name.to_sym
when :h1
" #\n\n"
when :h2
" ##\n\n"
when :h3
" ###\n\n"
when :h4
" ####\n\n"
when :h5
" #####\n\n"
when :h6
" ######\n\n"
when :p
parent.eql?(:root) ? "\n\n" : "\n"
when :ol
parent.eql?(:li) ? "" : "\n"
when :ul
parent.eql?(:li) ? "" : "\n"
when :em
"*"
when :strong
"**"
when :li
""
when :blockquote
""
when :code
parent.eql?(:pre) ? "" : "`"
when :a
@links << type.attribute('href').to_s
"][" + @links.size.to_s + "] "
when :img
@links << type.attribute('src').to_s
"" + type.attribute('alt').to_s + "][" + @links.size.to_s + "] "
"#{type.attribute('alt')}][#{@links.size}] "
when :root
""
else
@errors << " unknown end tag: "+type.name.to_s
""
end
end
# Perform indent: two space, @indent times - quite simple! :)
def indent
str = ""
@indent.times do
str << " "
end
str
end
# Return the content of element, which should be just text.
# If its a code block to indent of 4 spaces.
# For block quotation add a leading '>'
def text_node(element, parent)
if element.name.to_sym.eql?(:code) and parent.eql?(:pre)
element.text.gsub("\n","\n ") << "\n"
elsif parent.eql?(:blockquote)
element.text.gsub!("\n ","\n>")
else
element.text
end
end
# Insert the mentioned reference style links.
def insert_links
@output << "\n"
@links.each_index do |index|
@output << " [#{index+1}]: #{@links[index]}\n"
end
end
# Print out all errors, that occured and have been written to @errors.
def print_errors
@errors.each do |error|
puts error
end
end
# Perform a benchmark on a given string n-times.
def speed_benchmark(string, n)
initialize()
bm(15) do |test|
test.report("reverse markdown:") { n.times do; parse_string(string); initialize(); end; }
end
end
end
if __FILE__ == $0
# Example HTML Code for parsing
example = <<-EOF
This text, though not within an element, should also be shown.
<h2>heading 1.1</h2>
<p>text *italic* and **bold**.</p>
<pre><code>text *italic* and **bold**.
sdfsdff
sdfsd
sdf sdfsdf
</code></pre>
<blockquote>
<p>text <em>italic</em> and <strong>bold</strong>. sdfsdff
sdfsd sdf sdfsdf</p>
</blockquote>
<p>asdasd <code>sdfsdfsdf</code> asdad <a href="http://www.bla.de">link text</a></p>
<p><a href="http://www.bla.de">link <strong>text</strong></a></p>
<ol>
<li>List item</li>
<li>List <em>item</em>
<ol><li>List item</li>
<li>dsfdsf
<ul><li>dfwe</li>
<li>dsfsdfsdf</li></ul></li>
<li>lidsf <img src="http://www.dfgdfg.de/dsf.jpe" alt="item" title="" /></li></ol></li>
<li>sdfsdfsdf
<ul><li>sdfsdfsdf</li>
<li>sdfsdfsdf <strong>sdfsdf</strong></li></ul></li>
</ol>
<blockquote>
<p>Lorem ipsum dolor sit amet, consetetur
voluptua. At vero eos et accusam et
justo duo dolores et ea rebum. Stet
clita kasd gubergren, no sea takimata
sanctus est Lorem ipsum dolor sit
amet. <em>italic</em></p>
</blockquote>
<hr />
<blockquote>
<p>Lorem ipsum dolor sit amet, consetetur
sadipscing elitr, sed diam nonumy
eirmod tempor invidunt ut labore et
dolore magna aliquyam erat, sed</p>
</blockquote>
This should also be shown, even if it's not wrapped in an element.
<p>nur ein text! nur eine maschine!</p>
This text should not be invisible!
EOF
r = ReverseMarkdown.new
puts r.parse_string(example)
#r.print_errors
#r.speed_benchmark(example, 100)
end