maruku 0.3.0 → 0.4.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.
Files changed (108) hide show
  1. data/bin/{maruku0.3 → marudown} +6 -14
  2. data/bin/maruku +1 -1
  3. data/bin/marutest +37 -9
  4. data/docs/TOFIX.html +22 -0
  5. data/docs/TOFIX.md +3 -0
  6. data/docs/changelog-0.2.13.html +30 -0
  7. data/docs/changelog-0.2.13.md +6 -0
  8. data/docs/changelog-0.3.html +19 -5
  9. data/docs/faq.html +51 -40
  10. data/docs/faq.md +3 -3
  11. data/docs/hidden_o_n_squared.md +10 -0
  12. data/docs/index.html +84 -396
  13. data/docs/markdown_syntax.html +139 -330
  14. data/docs/markdown_syntax.md +80 -93
  15. data/docs/maruku.html +84 -396
  16. data/docs/maruku.md +88 -158
  17. data/docs/proposal.html +13 -106
  18. data/docs/proposal.md +3 -3
  19. data/docs/todo.html +38 -28
  20. data/lib/maruku.rb +77 -11
  21. data/lib/maruku/attributes.rb +186 -0
  22. data/lib/maruku/defaults.rb +40 -0
  23. data/lib/maruku/errors_management.rb +55 -39
  24. data/lib/maruku/helpers.rb +156 -72
  25. data/lib/maruku/input/charsource.rb +319 -0
  26. data/lib/maruku/{html_helper.rb → input/html_helper.rb} +30 -9
  27. data/lib/maruku/input/linesource.rb +111 -0
  28. data/lib/maruku/input/parse_block.rb +562 -0
  29. data/lib/maruku/{parse_doc.rb → input/parse_doc.rb} +60 -28
  30. data/lib/maruku/{parse_span_better.rb → input/parse_span_better.rb} +226 -256
  31. data/lib/maruku/input/type_detection.rb +137 -0
  32. data/lib/maruku/maruku.rb +33 -0
  33. data/lib/maruku/{to_html.rb → output/to_html.rb} +151 -132
  34. data/lib/maruku/{to_latex.rb → output/to_latex.rb} +31 -35
  35. data/lib/maruku/{to_latex_entities.rb → output/to_latex_entities.rb} +25 -3
  36. data/lib/maruku/output/to_latex_strings.rb +64 -0
  37. data/lib/maruku/output/to_markdown.rb +164 -0
  38. data/lib/maruku/{to_s.rb → output/to_s.rb} +6 -0
  39. data/lib/maruku/string_utils.rb +12 -181
  40. data/lib/maruku/structures.rb +91 -67
  41. data/lib/maruku/structures_inspect.rb +78 -0
  42. data/lib/maruku/structures_iterators.rb +24 -2
  43. data/lib/maruku/tests/benchmark.rb +41 -9
  44. data/lib/maruku/tests/new_parser.rb +317 -286
  45. data/lib/maruku/tests/tests.rb +20 -0
  46. data/lib/maruku/toc.rb +64 -64
  47. data/lib/maruku/usage/example1.rb +33 -0
  48. data/lib/maruku/version.rb +8 -2
  49. data/tests/unittest/abbreviations.md +27 -16
  50. data/tests/unittest/attributes/attributes.md +89 -0
  51. data/tests/unittest/attributes/circular.md +51 -0
  52. data/tests/unittest/attributes/default.md +47 -0
  53. data/tests/unittest/blank.md +10 -6
  54. data/tests/unittest/blanks_in_code.md +26 -26
  55. data/tests/unittest/code.md +9 -9
  56. data/tests/unittest/code2.md +12 -13
  57. data/tests/unittest/code3.md +34 -34
  58. data/tests/unittest/easy.md +9 -7
  59. data/tests/unittest/email.md +9 -7
  60. data/tests/unittest/encoding/iso-8859-1.md +41 -4
  61. data/tests/unittest/encoding/utf-8.md +6 -5
  62. data/tests/unittest/entities.md +52 -80
  63. data/tests/unittest/escaping.md +47 -35
  64. data/tests/unittest/extra_dl.md +19 -29
  65. data/tests/unittest/extra_header_id.md +31 -24
  66. data/tests/unittest/extra_table1.md +14 -32
  67. data/tests/unittest/footnotes.md +58 -42
  68. data/tests/unittest/headers.md +11 -11
  69. data/tests/unittest/hrule.md +14 -24
  70. data/tests/unittest/images.md +41 -26
  71. data/tests/unittest/inline_html.md +104 -56
  72. data/tests/unittest/inline_html2.md +38 -0
  73. data/tests/unittest/links.md +74 -33
  74. data/tests/unittest/list1.md +18 -15
  75. data/tests/unittest/list2.md +31 -13
  76. data/tests/unittest/list3.md +29 -28
  77. data/tests/unittest/list4.md +103 -12
  78. data/tests/unittest/lists.md +86 -53
  79. data/tests/unittest/lists6.md +53 -0
  80. data/tests/unittest/lists7.md +31 -0
  81. data/tests/unittest/lists_after_paragraph.md +105 -71
  82. data/tests/unittest/lists_ol.md +149 -73
  83. data/tests/unittest/misc_sw.md +366 -326
  84. data/tests/unittest/notyet/escape.md +10 -10
  85. data/tests/unittest/notyet/header_after_par.md +20 -14
  86. data/tests/unittest/notyet/ticks.md +8 -35
  87. data/tests/unittest/notyet/triggering.md +72 -45
  88. data/tests/unittest/olist.md +78 -0
  89. data/tests/unittest/one.md +5 -3
  90. data/tests/unittest/paragraph.md +5 -3
  91. data/tests/unittest/paragraph_rules/dont_merge_ref.md +15 -9
  92. data/tests/unittest/paragraph_rules/tab_is_blank.md +9 -5
  93. data/tests/unittest/paragraphs.md +21 -26
  94. data/tests/unittest/recover/recover_links.md +6 -5
  95. data/tests/unittest/references/long_example.md +39 -30
  96. data/tests/unittest/references/spaces_and_numbers.md +2 -2
  97. data/tests/unittest/syntax_hl.md +33 -31
  98. data/tests/unittest/test.md +4 -6
  99. data/tests/unittest/wrapping.md +43 -26
  100. metadata +160 -139
  101. data/docs/markdown_extra2.html +0 -87
  102. data/docs/markdown_extra2.md +0 -83
  103. data/docs/markdown_syntax_2.html +0 -152
  104. data/lib/maruku/parse_block.rb +0 -564
  105. data/lib/maruku/parse_span.rb +0 -451
  106. data/lib/maruku/to_latex_strings.rb +0 -59
  107. data/lib/maruku/to_markdown.rb +0 -110
  108. data/lib/test.rb +0 -29
@@ -0,0 +1,137 @@
1
+ #--
2
+ # Copyright (C) 2006 Andrea Censi <andrea (at) rubyforge.org>
3
+ #
4
+ # This file is part of Maruku.
5
+ #
6
+ # Maruku is free software; you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation; either version 2 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Maruku is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Maruku; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
+ #++
20
+
21
+ class String
22
+ include MaRuKu::Strings
23
+ def md_type()
24
+ @md_type ||= line_md_type(self)
25
+ end
26
+ end
27
+
28
+ class NilClass
29
+ def md_type() nil end
30
+
31
+ end
32
+
33
+ # This code does the classification of lines for block-level parsing.
34
+ module MaRuKu; module Strings
35
+
36
+ def line_md_type(l)
37
+ # The order of evaluation is important (:text is a catch-all)
38
+ return :text if l =~ /^[a-zA-Z]/
39
+ return :code if number_of_leading_spaces(l)>=4
40
+ return :empty if l =~ /^\s*$/
41
+ return :footnote_text if l =~ FootnoteText
42
+ return :ref_definition if l =~ LinkRegex or l=~ IncompleteLink
43
+ return :abbreviation if l =~ Abbreviation
44
+ return :definition if l =~ Definition
45
+ # I had a bug with emails and urls at the beginning of the
46
+ # line that were mistaken for raw_html
47
+ return :text if l=~EMailAddress or l=~ URL
48
+ # raw html is like PHP Markdown Extra: at most three spaces before
49
+ return :raw_html if l =~ %r{^[ ]?[ ]?[ ]?</?\s*\w+}
50
+ return :raw_html if l =~ %r{[ ]{0,3}<\!\-\-}
51
+ return :ulist if l =~ /^\s?([\*\-\+])\s+.*\w+/
52
+ return :olist if l =~ /^\s?\d+\..*\w+/
53
+ return :header1 if l =~ /^(=)+/
54
+ return :header2 if l =~ /^([-\s])+$/
55
+ return :header3 if l =~ /^(#)+\s*\S+/
56
+ # at least three asterisks on a line, and only whitespace
57
+ return :hrule if l =~ /^(\s*\*\s*){3,1000}$/
58
+ return :hrule if l =~ /^(\s*-\s*){3,1000}$/ # or hyphens
59
+ return :hrule if l =~ /^(\s*_\s*){3,1000}$/ # or underscores
60
+ return :quote if l =~ /^>/
61
+ return :metadata if l =~ /^@/
62
+ # if @@new_meta_data?
63
+ return :ald if l =~ AttributeDefinitionList
64
+ return :ial if l =~ /^\s{0,3}\{.*\}/
65
+ # end
66
+ return :text # else, it's just text
67
+ end
68
+
69
+ # $1 = id $2 = attribute list
70
+ AttributeDefinitionList = /^\s{0,3}\{([\w\d\s]+)\}:\s*(.*)\s*$/
71
+
72
+ # Example:
73
+ # ^:blah blah
74
+ # ^: blah blah
75
+ # ^ : blah blah
76
+ Definition = %r{
77
+ ^ # begin of line
78
+ [ ]{0,3} # up to 3 spaces
79
+ : # colon
80
+ \s* # whitespace
81
+ (\S.*) # the text = $1
82
+ $ # end of line
83
+ }x
84
+
85
+ # Example:
86
+ # *[HTML]: Hyper Text Markup Language
87
+ Abbreviation = %r{
88
+ ^ # begin of line
89
+ \* # one asterisk
90
+ \[ # opening bracket
91
+ ([^\]]+) # any non-closing bracket: id = $1
92
+ \] # closing bracket
93
+ : # colon
94
+ \s* # whitespace
95
+ (\S.*\S)* # definition=$2
96
+ \s* # strip this whitespace
97
+ $ # end of line
98
+ }x
99
+
100
+ FootnoteText = %r{
101
+ ^\s*\[(\^.+)\]: # id = $1 (including '^')
102
+ \s*(\S.*)?$ # text = $2 (not obb.)
103
+ }x
104
+
105
+ # This regex is taken from BlueCloth sources
106
+ # Link defs are in the form: ^[id]: \n? url "optional title"
107
+ LinkRegex = %r{
108
+ ^[ ]*\[([^\]]+)\]: # id = $1
109
+ [ ]*
110
+ <?(\S+)>? # url = $2
111
+ [ ]*
112
+ (?:# Titles are delimited by "quotes" or (parens).
113
+ ["(']
114
+ (.+?) # title = $3
115
+ [")'] # Matching ) or "
116
+ \s*(.+)? # stuff = $4
117
+ )? # title is optional
118
+ }x
119
+
120
+ IncompleteLink = %r{^\s*\[(.+)\]:\s*$}
121
+
122
+ HeaderWithId = /^(.*)\{\#([\w_-]+)\}\s*$/
123
+
124
+ HeaderWithAttributes = /^(.*)\{(.*)\}\s*$/
125
+
126
+
127
+ # if contains a pipe, it could be a table header
128
+ MightBeTableHeader = %r{\|}
129
+ # -------------:
130
+ Sep = /\s*(\:)?\s*-+\s*(\:)?\s*/
131
+ # | -------------:| ------------------------------ |
132
+ TableSeparator = %r{^(\|?#{Sep}\|?)+\s*$}
133
+
134
+
135
+ EMailAddress = /<([^:]+@[^:]+)>/
136
+ URL = /^<http:/
137
+ end end
@@ -0,0 +1,33 @@
1
+ #--
2
+ # Copyright (C) 2006 Andrea Censi <andrea (at) rubyforge.org>
3
+ #
4
+ # This file is part of Maruku.
5
+ #
6
+ # Maruku is free software; you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation; either version 2 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Maruku is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Maruku; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
+ #++
20
+
21
+
22
+ # The Maruku class is the public interface
23
+
24
+ class Maruku
25
+
26
+ def initialize(s=nil, meta={})
27
+ super(nil)
28
+ self.attributes.merge! meta
29
+ if s
30
+ parse_doc(s)
31
+ end
32
+ end
33
+ end
@@ -1,3 +1,4 @@
1
+ #--
1
2
  # Copyright (C) 2006 Andrea Censi <andrea (at) rubyforge.org>
2
3
  #
3
4
  # This file is part of Maruku.
@@ -15,6 +16,7 @@
15
16
  # You should have received a copy of the GNU General Public License
16
17
  # along with Maruku; if not, write to the Free Software
17
18
  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
+ #++
18
20
 
19
21
 
20
22
  require 'rexml/document'
@@ -24,9 +26,27 @@ require 'syntax'
24
26
  require 'syntax/convertors/html'
25
27
 
26
28
 
27
- class Maruku
28
- include REXML
29
+ class String
30
+ # A string is rendered into HTML by creating
31
+ # a REXML::Text node. REXML takes care of all the encoding.
32
+ def to_html
33
+ REXML::Text.new(self)
34
+ end
35
+ end
36
+
37
+
38
+ class REXML::Element
39
+ # We only want to output the children in Maruku::to_html
40
+ public :write_children
41
+ end
42
+
43
+
29
44
 
45
+ # This module groups all functions related to HTML export.
46
+ module MaRuKu; module Out; module HTML
47
+ include REXML
48
+ include MaRuKu::Defaults
49
+
30
50
  # Render as an HTML fragment (no head, just the content of BODY). (returns a string)
31
51
  def to_html(context={})
32
52
  indent = context[:indent] || -1
@@ -38,13 +58,13 @@ class Maruku
38
58
  end
39
59
 
40
60
  # render footnotes
41
- if @doc.meta[:footnotes_used]
61
+ if @doc.footnotes_order.size > 0
42
62
  div << render_footnotes
43
63
  end
44
64
 
45
65
  doc = Document.new(nil,{:respect_whitespace =>:all})
46
66
  doc << div
47
- add_whitespace(doc)
67
+ #add_whitespace(doc)
48
68
 
49
69
  # REXML Bug? if indent!=-1 whitespace is not respected for 'pre' elements
50
70
  # containing code.
@@ -63,10 +83,7 @@ class Maruku
63
83
  # REXML Bug? if indent!=-1 whitespace is not respected for 'pre' elements
64
84
  # containing code.
65
85
  doc.write(xml,indent,transitive=true,ie_hack);
66
-
67
- # encoding = ( (enc=@meta[:encoding]) ?
68
- # "encoding='#{enc}'" : "")
69
-
86
+
70
87
  xhtml10strict = "<?xml version='1.0' encoding='utf-8'?>
71
88
  <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
72
89
  'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>\n"
@@ -81,20 +98,27 @@ class Maruku
81
98
  root = Element.new('html', doc)
82
99
  root.add_namespace('http://www.w3.org/1999/xhtml')
83
100
 
84
- lang = @meta[:lang] || 'en'
101
+ lang = @attributes[:lang] || 'en'
85
102
  root.attributes['lang'] = lang
86
103
  root.attributes['xml:lang'] = lang
87
104
 
88
105
  head = Element.new 'head', root
89
106
 
107
+ #<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
108
+ me = Element.new 'meta', head
109
+ me.attributes['http-equiv'] = 'Content-type'
110
+ me.attributes['content'] = 'text/html; charset=utf-8'
111
+
90
112
  # Create title element
91
- doc_title = @meta[:title] || @meta[:subject] || ""
92
- title = Element.new 'title'
113
+ doc_title = @attributes[:title] || @attributes[:subject] || ""
114
+ title = Element.new 'title', head
93
115
  title << Text.new(doc_title)
94
- head << title
116
+
117
+ # Encoding
95
118
 
96
119
 
97
- css = @meta[:css]
120
+
121
+ css = @attributes[:css]
98
122
  if css
99
123
  # <link type="text/css" rel="stylesheet" href="..." />
100
124
  link = Element.new 'link'
@@ -111,7 +135,7 @@ class Maruku
111
135
  end
112
136
 
113
137
  # render footnotes
114
- if @doc.meta[:footnotes_used]
138
+ if @doc.footnotes_order.size > 0
115
139
  body << render_footnotes
116
140
  end
117
141
 
@@ -121,7 +145,7 @@ class Maruku
121
145
 
122
146
  root << body
123
147
 
124
- add_whitespace(doc)
148
+ # add_whitespace(doc)
125
149
  doc
126
150
  end
127
151
 
@@ -182,17 +206,17 @@ class Maruku
182
206
  div.attributes['class'] = 'footnotes'
183
207
  div << Element.new('hr')
184
208
  ol = Element.new 'ol'
185
- @doc.meta[:footnotes_used].each_with_index do |fid, i| num = i+1
186
- f = @footnotes[fid]
209
+ @doc.footnotes_order.each_with_index do |fid, i| num = i+1
210
+ f = self.footnotes[fid]
187
211
  if f
188
- li = f.wrap_as_element('li')
212
+ li = f.wrap_as_element('li')
189
213
  li.attributes['id'] = "fn:#{num}"
190
214
 
191
215
  a = Element.new 'a'
192
216
  a.attributes['href'] = "#fnref:#{num}"
193
217
  a.attributes['rev'] = 'footnote'
194
218
  a<< Text.new('&#8617;', true, nil, true)
195
- li.children.last << a
219
+ li.insert_after(li.children.last, a)
196
220
  ol << li
197
221
  else
198
222
  maruku_error"Could not find footnote '#{fid}'"
@@ -201,17 +225,7 @@ class Maruku
201
225
  div << ol
202
226
  div
203
227
  end
204
- end
205
-
206
- class String
207
- # A string is rendered into HTML by creating
208
- # a REXML::Text node. REXML takes care of all the encoding.
209
- def to_html
210
- REXML::Text.new(self)
211
- end
212
- end
213
228
 
214
- class MDElement
215
229
 
216
230
  def to_html_hrule; Element.new 'hr' end
217
231
  def to_html_linebreak; Element.new 'br' end
@@ -222,43 +236,47 @@ class MDElement
222
236
  def wrap_as_element(name)
223
237
  m = create_html_element name
224
238
  children_to_html.each do |e| m << e; end
239
+
240
+ # m << Comment.new( "{"+self.al.to_md+"}") if not self.al.empty?
241
+ # m << Comment.new( @attributes.inspect) if not @attributes.empty?
225
242
  m
226
243
  end
227
244
 
228
245
  def create_html_element(name)
229
246
  m = Element.new name
230
- if @meta[:id] then m.attributes['id'] = @meta[:id].to_s end
231
- if @meta[:style] then m.attributes['style'] = @meta[:style].to_s end
232
- if @meta[:class] then m.attributes['class'] = @meta[:class].to_s end
247
+ if (v=@attributes[:id] ) then m.attributes['id' ] = v.to_s end
248
+ if (v=@attributes[:style]) then m.attributes['style'] = v.to_s end
249
+ if (v=@attributes[:class]) then m.attributes['class'] = v.to_s end
233
250
  m
234
251
  end
235
252
 
236
- def to_html_paragraph; wrap_as_element('p') end
237
253
 
238
254
  def to_html_ul
239
- if @meta[:toc]
255
+ if @attributes[:toc]
240
256
  # render toc
241
257
  html_toc = @doc.toc.to_html
242
258
  return html_toc
243
259
  else
244
- wrap_as_element('ul')
260
+ add_ws wrap_as_element('ul')
245
261
  end
246
262
  end
247
263
 
248
264
 
249
- def to_html_ol; wrap_as_element('ol') end
250
- def to_html_li; wrap_as_element('li') end
251
- def to_html_li_span; wrap_as_element('li') end
252
- def to_html_quote; wrap_as_element('blockquote') end
265
+ def to_html_paragraph; add_ws wrap_as_element('p') end
266
+ def to_html_ol; add_ws wrap_as_element('ol') end
267
+ def to_html_li; add_ws wrap_as_element('li') end
268
+ def to_html_li_span; add_ws wrap_as_element('li') end
269
+ def to_html_quote; add_ws wrap_as_element('blockquote') end
253
270
  def to_html_strong; wrap_as_element('strong') end
254
271
  def to_html_emphasis; wrap_as_element('em') end
255
272
 
256
273
  # nil if not applicable, else string
257
274
  def section_number
258
- return nil if not @doc.meta[:use_numbered_headers]
275
+ return nil if not @doc.attributes[:use_numbered_headers]
259
276
 
260
- if (s = @section) and not s.section_number.empty?
261
- s.section_number.join('.')+". "
277
+ n = @attributes[:section_number]
278
+ if n && (not n.empty?)
279
+ n.join('.')+". "
262
280
  else
263
281
  nil
264
282
  end
@@ -278,13 +296,13 @@ class MDElement
278
296
  end
279
297
 
280
298
  def to_html_header
281
- element_name = "h#{@meta[:level]}"
299
+ element_name = "h#{self.level}"
282
300
  h = wrap_as_element element_name
283
301
 
284
302
  if span = render_section_number
285
303
  h.insert_before(h.children.first, span)
286
304
  end
287
- h
305
+ add_ws h
288
306
  end
289
307
 
290
308
  def source2html(source)
@@ -294,9 +312,9 @@ class MDElement
294
312
  end
295
313
 
296
314
  def to_html_code;
297
- source = self.meta[:raw_code]
315
+ source = self.raw_code
298
316
 
299
- lang = self.meta[:lang] || @doc.meta[:code_lang]
317
+ lang = @attributes[:lang] || @doc.attributes[:code_lang]
300
318
 
301
319
  lang = 'xml' if lang=='html'
302
320
  use_syntax = get_setting(:html_use_syntax)
@@ -322,9 +340,9 @@ class MDElement
322
340
  pre
323
341
  rescue Object => e
324
342
  maruku_error"Error while using the syntax library for code:\n#{source.inspect}"+
325
- "Lang is #{lang} object is: "+
326
- @meta.inspect +
327
- "Exception: #{e.class}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
343
+ "Lang is #{lang} object is: \n"+
344
+ self.inspect +
345
+ "\nException: #{e.class}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
328
346
 
329
347
  tell_user("Using normal PRE because the syntax library did not work.")
330
348
  to_html_code_using_pre(source)
@@ -362,7 +380,7 @@ class MDElement
362
380
 
363
381
  def to_html_inline_code;
364
382
  pre = Element.new 'code'
365
- source = self.meta[:raw_code]
383
+ source = self.raw_code
366
384
  pre << source2html(source)
367
385
 
368
386
  color = get_setting(:code_background_color, DEFAULT_CODE_COLOR)
@@ -375,9 +393,8 @@ class MDElement
375
393
 
376
394
  def to_html_immediate_link
377
395
  a = Element.new 'a'
378
- url = @meta[:url]
379
- text = url
380
- text = text.gsub(/^mailto:/,'') # don't show mailto
396
+ url = self.url
397
+ text = url.gsub(/^mailto:/,'') # don't show mailto
381
398
  a << Text.new(text)
382
399
  a.attributes['href'] = url
383
400
  a
@@ -385,38 +402,43 @@ class MDElement
385
402
 
386
403
  def to_html_link
387
404
  a = wrap_as_element 'a'
405
+ id = self.ref_id
406
+ # if empty, use text
407
+ if id.size == 0
408
+ id = children.to_s.downcase
409
+ end
388
410
 
389
- if id = @meta[:ref_id]
390
- # if empty, use text
391
- if id.size == 0
392
- id = children.to_s.downcase
393
- end
394
- ref = @doc.refs[id]
395
- if not ref
396
- maruku_error"Could not find ref_id = #{id.inspect} for #{self.inspect}"
397
- tell_user "Not creating a link for ref_id = #{id.inspect}."
398
- return wrap_as_element('span')
399
- else
400
- url = ref[:url]
401
- title = ref[:title]
402
- a.attributes['href'] = url
403
- a.attributes['title'] = title if title
404
- end
411
+ if ref = @doc.refs[id]
412
+ url = ref[:url]
413
+ title = ref[:title]
414
+ a.attributes['href']=url if url
415
+ a.attributes['title']=title if title
405
416
  else
406
- url = @meta[:url]
407
- title = @meta[:title]
408
- if url
409
- a.attributes['href'] = url
410
- a.attributes['title'] = title if title
411
- else
412
- maruku_error"Could not find url in #{self.inspect}"
413
- tell_user "Not creating a link for ref_id = #{id.inspect}."
414
- return wrap_as_element('span')
415
- end
417
+ maruku_error"Could not find ref_id = #{id.inspect} for #{self.inspect}"
418
+ tell_user "Not creating a link for ref_id = #{id.inspect}."
419
+ return wrap_as_element('span')
416
420
  end
417
- a
421
+ return a
422
+ end
423
+
424
+ def to_html_im_link
425
+ a = wrap_as_element 'a'
426
+ url = self.url
427
+ title = self.title
428
+ if url
429
+ a.attributes['href'] = url
430
+ a.attributes['title'] = title if title
431
+ else
432
+ maruku_error"Could not find url in #{self.inspect}"
433
+ tell_user "Not creating a link for ref_id = #{id.inspect}."
434
+ return wrap_as_element('span')
435
+ end
436
+ return a
418
437
  end
419
438
 
439
+ def add_ws(e)
440
+ [Text.new("\n"), e, Text.new("\n")]
441
+ end
420
442
  ##### Email address
421
443
 
422
444
  def obfuscate(s)
@@ -428,7 +450,7 @@ class MDElement
428
450
  end
429
451
 
430
452
  def to_html_email_address
431
- email = @meta[:email]
453
+ email = self.email
432
454
  a = Element.new 'a'
433
455
  #a.attributes['href'] = Text.new("mailto:"+obfuscate(email),false,nil,true)
434
456
  #a.attributes.add Attribute.new('href',Text.new(
@@ -444,42 +466,43 @@ class MDElement
444
466
 
445
467
  def to_html_image
446
468
  a = Element.new 'img'
447
- if id = @meta[:ref_id]
448
- ref = @doc.refs[id]
449
- if not ref
450
- maruku_error"Could not find id = #{id.inspect} for\n #{self.inspect}"
451
- tell_user "Could not create image with ref_id = #{id.inspect};"+
452
- +" Using SPAN element as replacement."
453
- return wrap_as_element('span')
454
- else
455
- url = ref[:url]
456
- a.attributes['src'] = url
457
- # puts ref.inspect
458
- [:title, :class, :style].each do |s|
459
- if ref[s] then
460
- a.attributes[s.to_s] = ref[s]
461
- end
469
+ id = self.ref_id
470
+ if ref = @doc.refs[id]
471
+ url = ref[:url]
472
+ a.attributes['src'] = url
473
+ [:title, :class, :style].each do |s|
474
+ if ref[s] then
475
+ a.attributes[s.to_s] = ref[s]
462
476
  end
463
477
  end
464
478
  else
465
- url = @meta[:url]
466
- title = @meta[:title]
467
- if not url
468
- maruku_error"Image with no ID or url: #{self.inspect}"
469
- tell_user "Could not create image with ref_id = #{id.inspect};"+
479
+ maruku_error"Could not find id = #{id.inspect} for\n #{self.inspect}"
480
+ tell_user "Could not create image with ref_id = #{id.inspect};"+
470
481
  +" Using SPAN element as replacement."
471
482
  return wrap_as_element('span')
472
- end
473
- a.attributes['src'] = url
474
- a.attributes['title'] = title if title
475
483
  end
476
- a
484
+ return a
485
+ end
486
+
487
+ def to_html_im_image
488
+ a = Element.new 'img'
489
+ url = self.url
490
+ title = self.title
491
+ if not url
492
+ maruku_error"Image with no url: #{self.inspect}"
493
+ tell_user "Could not create image with ref_id = #{id.inspect};"+
494
+ +" Using SPAN element as replacement."
495
+ return wrap_as_element('span')
496
+ end
497
+ a.attributes['src'] = url
498
+ a.attributes['title'] = title if title
499
+ return a
477
500
  end
478
501
 
479
502
  def to_html_raw_html
480
- raw_html = @meta[:raw_html]
503
+ raw_html = self.raw_html
481
504
  if rexml_doc = @parsed_html
482
- root = rexml_doc.root
505
+ root = rexml_doc.root
483
506
  if root.nil?
484
507
  s = "Bug in REXML: root() of Document is nil: \n#{rexml_doc.inspect}\n"+
485
508
  "Raw HTML:\n#{raw_html.inspect}"
@@ -490,10 +513,13 @@ class MDElement
490
513
  return div
491
514
  end
492
515
 
493
- return root
516
+ # copies the @children array (FIXME is it deep?)
517
+ elements = root.to_a
518
+ return elements
494
519
  else # invalid
495
520
  # Creates red box with offending HTML
496
- tell_user 'Wrapping bad html in a PRE with class "markdown-html-error"'
521
+ tell_user "Wrapping bad html in a PRE with class 'markdown-html-error'\n"+
522
+ add_tabs(raw_html,1,'|')
497
523
  pre = Element.new('pre')
498
524
  pre.attributes['style'] = 'border: solid 3px red; background-color: pink'
499
525
  pre.attributes['class'] = 'markdown-html-error'
@@ -502,18 +528,18 @@ class MDElement
502
528
  end
503
529
  end
504
530
 
505
- def to_html_abbreviation
531
+ def to_html_abbr
506
532
  abbr = Element.new 'abbr'
507
533
  abbr << Text.new(children[0])
508
- abbr.attributes['title'] = self.meta[:title] if self.meta[:title]
534
+ abbr.attributes['title'] = self.title if self.title
509
535
  abbr
510
536
  end
511
537
 
512
538
  def to_html_footnote_reference
513
- id = @meta[:footnote_id]
539
+ id = self.footnote_id
514
540
 
515
541
  # save the order of used footnotes
516
- order = (@doc.meta[:footnotes_used] ||= [])
542
+ order = @doc.footnotes_order
517
543
 
518
544
  # take next number
519
545
  order << id
@@ -541,7 +567,7 @@ class MDElement
541
567
 
542
568
  ## Table ###
543
569
  def to_html_table
544
- align = @meta[:align]
570
+ align = self.align
545
571
  num_columns = align.size
546
572
 
547
573
  head = @children.slice(0, num_columns)
@@ -577,18 +603,10 @@ class MDElement
577
603
  def to_html_cell; wrap_as_element('td') end
578
604
 
579
605
  def to_html_entity
580
- entity_name = @meta[:entity_name]
606
+ entity_name = self.entity_name
581
607
  Text.new('&%s;' % [entity_name])
582
608
  end
583
- end
584
609
 
585
- # We only want to output the children in Maruku::to_html
586
- class REXML::Element; public :write_children end
587
-
588
- # Some utilities
589
- class MDElement
590
- include REXML
591
-
592
610
  # Convert each child to html
593
611
  def children_to_html
594
612
  array_to_html(@children)
@@ -599,18 +617,19 @@ class MDElement
599
617
  array.each do |c|
600
618
  method = c.kind_of?(MDElement) ?
601
619
  "to_html_#{c.node_type}" : "to_html"
602
-
620
+
603
621
  if not c.respond_to?(method)
604
- raise "Object does not answer to #{method}: #{c.class} #{c.inspect}"
622
+ #raise "Object does not answer to #{method}: #{c.class} #{c.inspect}"
623
+ next
605
624
  end
606
-
625
+
607
626
  h = c.send(method)
608
-
627
+
609
628
  if h.nil?
610
629
  raise "Nil html created by method #{method}:\n#{h.inspect}\n"+
611
630
  " for object #{c.inspect[0,300]}"
612
631
  end
613
-
632
+
614
633
  if h.kind_of?Array
615
634
  e = e + h #h.each do |hh| e << hh end
616
635
  else
@@ -619,10 +638,10 @@ class MDElement
619
638
  end
620
639
  e
621
640
  end
622
-
641
+
623
642
  def to_html_ref_definition; [] end
624
643
  def to_latex_ref_definition; [] end
625
-
626
- end
627
-
628
644
 
645
+ end # HTML
646
+ end # out
647
+ end # MaRuKu