reverse-markdown 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/reverse-markdown.rb +125 -31
  2. metadata +4 -5
@@ -1,15 +1,22 @@
1
- #!/usr/bin/env ruby
2
-
3
- # file: reverse-markdown.rb
4
-
5
- # it is forked from https://github.com/xijo/reverse-markdown/blob/master/reverse_markdown.rb
6
1
  require 'rexml/document'
7
2
  require 'benchmark'
8
3
  include REXML
9
4
  include Benchmark
10
5
 
6
+ # reverse markdown for ruby
7
+ # author: JO
8
+ # e-mail: xijo@gmx.de
9
+ # date: 14.7.2009
10
+ # version: 0.1
11
+ # license: GPL
12
+ # taken from https://github.com/xijo/reverse-markdown/raw/master/reverse_markdown.rb
13
+
14
+ # TODO
15
+ # - ol numbering is buggy, in fact doesn't matter for markdown code
16
+ # -
17
+
11
18
  class ReverseMarkdown
12
-
19
+
13
20
  # set basic variables:
14
21
  # - @li_counter: numbering list item (li) tags in an ordered list (ol)
15
22
  # - @links: hold the links for adding them to the bottom of the @output
@@ -24,20 +31,17 @@ class ReverseMarkdown
24
31
  @indent = 0
25
32
  @errors = []
26
33
  end
27
-
34
+
28
35
  # Invokes the HTML parsing by using a string. Returns the markdown code in @output.
29
36
  # To garantuee well-formed xml for REXML a <root> element will be added, but has no effect.
30
37
  # After parsing all elements, the 'reference style'-links will be inserted.
31
38
  def parse_string(string)
32
39
  doc = Document.new("<root>\n"+string+"\n</root>")
33
- root = doc.root
34
- root.elements.each do |element|
35
- parse_element(element, :root)
36
- end
40
+ parse_element(doc.root, :none)
37
41
  insert_links()
38
42
  @output
39
43
  end
40
-
44
+
41
45
  # Parsing an element and its children (recursive) and writing its markdown code to @output
42
46
  # 1. do indent for nested list items
43
47
  # 2. add the markdown opening tag for this element
@@ -50,33 +54,33 @@ class ReverseMarkdown
50
54
  @output << indent() if name.eql?(:li)
51
55
  # 2.
52
56
  @output << opening(element, parent)
53
-
57
+
54
58
  # 3a.
55
59
  if (element.has_text? and element.children.size < 2)
56
60
  @output << text_node(element, parent)
57
61
  end
58
-
62
+
59
63
  # 3b.
60
64
  if element.has_elements?
61
65
  element.children.each do |child|
62
66
  # increase indent if nested list
63
67
  @indent += 1 if element.name=~/(ul|ol)/ and parent.eql?(:li)
64
-
68
+
65
69
  if child.node_type.eql?(:element)
66
70
  parse_element(child, element.name.to_sym)
67
71
  else
68
72
  if parent.eql?(:blockquote)
69
- @output << child.to_s.gsub("\n", "\n>")
73
+ @output << child.to_s.gsub("\n ", "\n>")
70
74
  else
71
75
  @output << child.to_s
72
76
  end
73
77
  end
74
-
78
+
75
79
  # decrease indent if end of nested list
76
80
  @indent -= 1 if element.name=~/(ul|ol)/ and parent.eql?(:li)
77
81
  end
78
82
  end
79
-
83
+
80
84
  # 4.
81
85
  @output << ending(element, parent)
82
86
  end
@@ -95,16 +99,22 @@ class ReverseMarkdown
95
99
  ""
96
100
  when :h2
97
101
  "## "
102
+ when :h3
103
+ "### "
104
+ when :h4
105
+ "#### "
106
+ when :h5
107
+ "##### "
108
+ when :h6
109
+ "###### "
98
110
  when :em
99
111
  "*"
100
- when :br
101
- "\n"
102
- "\n>" if parent.eql?(:blockquote)
103
112
  when :strong
104
113
  "**"
105
114
  when :blockquote
106
- type.delete(type.elements[1]) if type.first.name.to_sym.eql?(:br)
107
- ">"
115
+ # remove leading newline
116
+ type.children.first.value = ""
117
+ "> "
108
118
  when :code
109
119
  parent.eql?(:pre) ? " " : "`"
110
120
  when :a
@@ -113,12 +123,14 @@ class ReverseMarkdown
113
123
  "!["
114
124
  when :hr
115
125
  "----------\n\n"
126
+ when :root
127
+ ""
116
128
  else
117
129
  @errors << "unknown start tag: "+type.name.to_s
118
130
  ""
119
131
  end
120
132
  end
121
-
133
+
122
134
  # Returns the closing markdown tag, like opening()
123
135
  def ending(type, parent)
124
136
  case type.name.to_sym
@@ -126,6 +138,14 @@ class ReverseMarkdown
126
138
  " #\n\n"
127
139
  when :h2
128
140
  " ##\n\n"
141
+ when :h3
142
+ " ###\n\n"
143
+ when :h4
144
+ " ####\n\n"
145
+ when :h5
146
+ " #####\n\n"
147
+ when :h6
148
+ " ######\n\n"
129
149
  when :p
130
150
  parent.eql?(:root) ? "\n\n" : "\n"
131
151
  when :ol
@@ -149,12 +169,14 @@ class ReverseMarkdown
149
169
  @links << type.attribute('src').to_s
150
170
  "" + type.attribute('alt').to_s + "][" + @links.size.to_s + "] "
151
171
  "#{type.attribute('alt')}][#{@links.size}] "
172
+ when :root
173
+ ""
152
174
  else
153
175
  @errors << " unknown end tag: "+type.name.to_s
154
176
  ""
155
177
  end
156
178
  end
157
-
179
+
158
180
  # Perform indent: two space, @indent times - quite simple! :)
159
181
  def indent
160
182
  str = ""
@@ -163,7 +185,7 @@ class ReverseMarkdown
163
185
  end
164
186
  str
165
187
  end
166
-
188
+
167
189
  # Return the content of element, which should be just text.
168
190
  # If its a code block to indent of 4 spaces.
169
191
  # For block quotation add a leading '>'
@@ -171,12 +193,12 @@ class ReverseMarkdown
171
193
  if element.name.to_sym.eql?(:code) and parent.eql?(:pre)
172
194
  element.text.gsub("\n","\n ") << "\n"
173
195
  elsif parent.eql?(:blockquote)
174
- element.text.gsub("\n","\n>")
196
+ element.text.gsub!("\n ","\n>")
175
197
  else
176
198
  element.text
177
199
  end
178
200
  end
179
-
201
+
180
202
  # Insert the mentioned reference style links.
181
203
  def insert_links
182
204
  @output << "\n"
@@ -184,14 +206,14 @@ class ReverseMarkdown
184
206
  @output << " [#{index+1}]: #{@links[index]}\n"
185
207
  end
186
208
  end
187
-
209
+
188
210
  # Print out all errors, that occured and have been written to @errors.
189
211
  def print_errors
190
212
  @errors.each do |error|
191
213
  puts error
192
214
  end
193
215
  end
194
-
216
+
195
217
  # Perform a benchmark on a given string n-times.
196
218
  def speed_benchmark(string, n)
197
219
  initialize()
@@ -199,5 +221,77 @@ class ReverseMarkdown
199
221
  test.report("reverse markdown:") { n.times do; parse_string(string); initialize(); end; }
200
222
  end
201
223
  end
202
-
224
+
203
225
  end
226
+
227
+ if __FILE__ == $0
228
+
229
+ # Example HTML Code for parsing
230
+ example = <<-EOF
231
+ This text, though not within an element, should also be shown.
232
+
233
+ <h2>heading 1.1</h2>
234
+
235
+ <p>text *italic* and **bold**.</p>
236
+
237
+ <pre><code>text *italic* and **bold**.
238
+ sdfsdff
239
+ sdfsd
240
+ sdf sdfsdf
241
+ </code></pre>
242
+
243
+ <blockquote>
244
+ <p>text <em>italic</em> and <strong>bold</strong>. sdfsdff
245
+ sdfsd sdf sdfsdf</p>
246
+ </blockquote>
247
+
248
+ <p>asdasd <code>sdfsdfsdf</code> asdad <a href="http://www.bla.de">link text</a></p>
249
+
250
+ <p><a href="http://www.bla.de">link <strong>text</strong></a></p>
251
+
252
+ <ol>
253
+ <li>List item</li>
254
+ <li>List <em>item</em>
255
+ <ol><li>List item</li>
256
+ <li>dsfdsf
257
+ <ul><li>dfwe</li>
258
+ <li>dsfsdfsdf</li></ul></li>
259
+ <li>lidsf <img src="http://www.dfgdfg.de/dsf.jpe" alt="item" title="" /></li></ol></li>
260
+ <li>sdfsdfsdf
261
+ <ul><li>sdfsdfsdf</li>
262
+ <li>sdfsdfsdf <strong>sdfsdf</strong></li></ul></li>
263
+ </ol>
264
+
265
+ <blockquote>
266
+ <p>Lorem ipsum dolor sit amet, consetetur
267
+ voluptua. At vero eos et accusam et
268
+ justo duo dolores et ea rebum. Stet
269
+ clita kasd gubergren, no sea takimata
270
+ sanctus est Lorem ipsum dolor sit
271
+ amet. <em>italic</em></p>
272
+ </blockquote>
273
+
274
+ <hr />
275
+
276
+ <blockquote>
277
+ <p>Lorem ipsum dolor sit amet, consetetur
278
+ sadipscing elitr, sed diam nonumy
279
+ eirmod tempor invidunt ut labore et
280
+ dolore magna aliquyam erat, sed</p>
281
+ </blockquote>
282
+
283
+ This should also be shown, even if it's not wrapped in an element.
284
+
285
+ <p>nur ein text! nur eine maschine!</p>
286
+
287
+ This text should not be invisible!
288
+ EOF
289
+
290
+ r = ReverseMarkdown.new
291
+
292
+ puts r.parse_string(example)
293
+
294
+ #r.print_errors
295
+
296
+ #r.speed_benchmark(example, 100)
297
+ end
metadata CHANGED
@@ -2,16 +2,16 @@
2
2
  name: reverse-markdown
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - James Robertson
9
+ - JO
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
13
 
13
- date: 2011-10-09 00:00:00 +01:00
14
- default_executable:
14
+ date: 2012-05-26 00:00:00 Z
15
15
  dependencies: []
16
16
 
17
17
  description:
@@ -24,7 +24,6 @@ extra_rdoc_files: []
24
24
 
25
25
  files:
26
26
  - lib/reverse-markdown.rb
27
- has_rdoc: true
28
27
  homepage:
29
28
  licenses: []
30
29
 
@@ -48,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
47
  requirements: []
49
48
 
50
49
  rubyforge_project:
51
- rubygems_version: 1.5.2
50
+ rubygems_version: 1.8.23
52
51
  signing_key:
53
52
  specification_version: 3
54
53
  summary: reverse-markdown