omf_web 1.2.3 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,87 +21,87 @@
21
21
  require 'rexml/document'
22
22
 
23
23
  begin
24
- require 'rexml/formatters/pretty'
25
- require 'rexml/formatters/default'
26
- $rexml_new_version = true
24
+ require 'rexml/formatters/pretty'
25
+ require 'rexml/formatters/default'
26
+ $rexml_new_version = true
27
27
  rescue LoadError
28
- $rexml_new_version = false
28
+ $rexml_new_version = false
29
29
  end
30
30
 
31
31
  class String
32
- # A string is rendered into HTML by creating
33
- # a REXML::Text node. REXML takes care of all the encoding.
34
- def to_html
35
- REXML::Text.new(self)
36
- end
32
+ # A string is rendered into HTML by creating
33
+ # a REXML::Text node. REXML takes care of all the encoding.
34
+ def to_html
35
+ REXML::Text.new(self)
36
+ end
37
37
  end
38
38
 
39
39
 
40
40
  # This module groups all functions related to HTML export.
41
41
  module MaRuKu; module Out; module HTML
42
- include REXML
43
-
44
- # Render as an HTML fragment (no head, just the content of BODY). (returns a string)
45
- def to_html(context={})
46
- Thread.current['maruku_context'] = context
47
- indent = context[:indent] || -1
48
- ie_hack = context[:ie_hack] || true
49
-
50
- div = Element.new 'dummy'
51
- children_to_html.each do |e|
52
- div << e
53
- end
54
-
55
- # render footnotes
56
- if @doc.footnotes_order.size > 0
57
- div << render_footnotes
58
- end
59
-
60
- doc = Document.new(nil,{:respect_whitespace =>:all})
61
- doc << div
62
-
63
- # REXML Bug? if indent!=-1 whitespace is not respected for 'pre' elements
64
- # containing code.
65
- xml =""
66
-
67
- if $rexml_new_version
68
- formatter = if indent > -1
69
- REXML::Formatters::Pretty.new( indent, ie_hack )
70
- else
71
- REXML::Formatters::Default.new( ie_hack )
72
- end
73
- formatter.write( div, xml)
74
- else
75
- div.write(xml,indent,transitive=true,ie_hack)
76
- end
77
-
78
- xml.gsub!(/\A<dummy>\s*/,'')
79
- xml.gsub!(/\s*<\/dummy>\Z/,'')
80
- xml.gsub!(/\A<dummy\s*\/>/,'')
81
- xml
82
- end
83
-
84
- # Render to a complete HTML document (returns a string)
85
- def to_html_document(context={})
86
- indent = context[:indent] || -1
87
- ie_hack = context[:ie_hack] ||true
88
- doc = to_html_document_tree
89
- xml = ""
90
-
91
- # REXML Bug? if indent!=-1 whitespace is not respected for 'pre' elements
92
- # containing code.
93
- doc.write(xml,indent,transitive=true,ie_hack);
94
-
95
- Xhtml11_mathml2_svg11 + xml
96
- end
97
-
98
- unless defined? Xhtml10strict
99
- Xhtml10strict =
42
+ include REXML
43
+
44
+ # Render as an HTML fragment (no head, just the content of BODY). (returns a string)
45
+ def to_html(context={})
46
+ Thread.current['maruku_context'] = context
47
+ indent = context[:indent] || -1
48
+ ie_hack = context[:ie_hack] || true
49
+
50
+ div = Element.new 'dummy'
51
+ children_to_html.each do |e|
52
+ div << e
53
+ end
54
+
55
+ # render footnotes
56
+ if @doc.footnotes_order.size > 0
57
+ div << render_footnotes
58
+ end
59
+
60
+ doc = Document.new(nil,{:respect_whitespace =>:all})
61
+ doc << div
62
+
63
+ # REXML Bug? if indent!=-1 whitespace is not respected for 'pre' elements
64
+ # containing code.
65
+ xml =""
66
+
67
+ if $rexml_new_version
68
+ formatter = if indent > -1
69
+ REXML::Formatters::Pretty.new( indent, ie_hack )
70
+ else
71
+ REXML::Formatters::Default.new( ie_hack )
72
+ end
73
+ formatter.write( div, xml)
74
+ else
75
+ div.write(xml,indent,transitive=true,ie_hack)
76
+ end
77
+
78
+ xml.gsub!(/\A<dummy>\s*/,'')
79
+ xml.gsub!(/\s*<\/dummy>\Z/,'')
80
+ xml.gsub!(/\A<dummy\s*\/>/,'')
81
+ xml
82
+ end
83
+
84
+ # Render to a complete HTML document (returns a string)
85
+ def to_html_document(context={})
86
+ indent = context[:indent] || -1
87
+ ie_hack = context[:ie_hack] ||true
88
+ doc = to_html_document_tree
89
+ xml = ""
90
+
91
+ # REXML Bug? if indent!=-1 whitespace is not respected for 'pre' elements
92
+ # containing code.
93
+ doc.write(xml,indent,transitive=true,ie_hack);
94
+
95
+ Xhtml11_mathml2_svg11 + xml
96
+ end
97
+
98
+ unless defined? Xhtml10strict
99
+ Xhtml10strict =
100
100
  "<?xml version='1.0' encoding='utf-8'?>
101
101
  <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
102
102
  'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>\n"
103
103
 
104
- Xhtml11strict_mathml2 = '<?xml version="1.0" encoding="utf-8"?>
104
+ Xhtml11strict_mathml2 = '<?xml version="1.0" encoding="utf-8"?>
105
105
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN"
106
106
  "http://www.w3.org/TR/MathML2/dtd/xhtml-math11-f.dtd" [
107
107
  <!ENTITY mathml "http://www.w3.org/1998/Math/MathML">
@@ -117,7 +117,7 @@ module MaRuKu; module Out; module HTML
117
117
  end
118
118
 
119
119
 
120
- def xml_newline() Text.new("\n") end
120
+ def xml_newline() Text.new("\n") end
121
121
 
122
122
 
123
123
  =begin maruku_doc
@@ -129,16 +129,16 @@ If a title is not specified, the first header will be used.
129
129
 
130
130
  These should be equivalent:
131
131
 
132
- Title: my document
132
+ Title: my document
133
133
 
134
- Content
134
+ Content
135
135
 
136
136
  and
137
137
 
138
- my document
139
- ===========
138
+ my document
139
+ ===========
140
140
 
141
- Content
141
+ Content
142
142
 
143
143
  In both cases, the title is set to "my document".
144
144
  =end
@@ -159,22 +159,22 @@ Synonim for `title`.
159
159
  =end
160
160
 
161
161
 
162
- # Render to an HTML fragment (returns a REXML document tree)
163
- def to_html_tree
164
- div = Element.new 'div'
165
- div.attributes['class'] = 'maruku_wrapper_div'
166
- children_to_html.each do |e|
167
- div << e
168
- end
162
+ # Render to an HTML fragment (returns a REXML document tree)
163
+ def to_html_tree
164
+ div = Element.new 'div'
165
+ div.attributes['class'] = 'maruku_wrapper_div'
166
+ children_to_html.each do |e|
167
+ div << e
168
+ end
169
169
 
170
- # render footnotes
171
- if @doc.footnotes_order.size > 0
172
- div << render_footnotes
173
- end
170
+ # render footnotes
171
+ if @doc.footnotes_order.size > 0
172
+ div << render_footnotes
173
+ end
174
174
 
175
- doc = Document.new(nil,{:respect_whitespace =>:all})
176
- doc << div
177
- end
175
+ doc = Document.new(nil,{:respect_whitespace =>:all})
176
+ doc << div
177
+ end
178
178
 
179
179
  =begin maruku_doc
180
180
  Attribute: css
@@ -186,178 +186,179 @@ Summary: Activates CSS stylesheets for HTML.
186
186
 
187
187
  Example:
188
188
 
189
- CSS: style.css math.css
189
+ CSS: style.css math.css
190
190
 
191
191
  =end
192
192
 
193
193
  unless defined? METAS
194
- METAS = %w{description keywords author revised}
195
- end
196
-
197
- # Render to a complete HTML document (returns a REXML document tree)
198
- def to_html_document_tree
199
- doc = Document.new(nil,{:respect_whitespace =>:all})
200
- # doc << XMLDecl.new
201
-
202
- root = Element.new('html', doc)
203
- root.add_namespace('http://www.w3.org/1999/xhtml')
204
- root.add_namespace('svg', "http://www.w3.org/2000/svg" )
205
- lang = self.attributes[:lang] || 'en'
206
- root.attributes['xml:lang'] = lang
207
-
208
- root << xml_newline
209
- head = Element.new 'head', root
210
-
211
- #<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
212
- me = Element.new 'meta', head
213
- me.attributes['http-equiv'] = 'Content-type'
194
+ METAS = %w{description keywords author revised}
195
+ end
196
+
197
+ # Render to a complete HTML document (returns a REXML document tree)
198
+ def to_html_document_tree
199
+ doc = Document.new(nil,{:respect_whitespace =>:all})
200
+ # doc << XMLDecl.new
201
+
202
+ root = Element.new('html', doc)
203
+ root.add_namespace('http://www.w3.org/1999/xhtml')
204
+ root.add_namespace('svg', "http://www.w3.org/2000/svg" )
205
+ lang = self.attributes[:lang] || 'en'
206
+ root.attributes['xml:lang'] = lang
207
+
208
+ root << xml_newline
209
+ head = Element.new 'head', root
210
+
211
+ #<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
212
+ me = Element.new 'meta', head
213
+ me.attributes['http-equiv'] = 'Content-type'
214
214
  # me.attributes['content'] = 'text/html;charset=utf-8'
215
- me.attributes['content'] = 'application/xhtml+xml;charset=utf-8'
216
-
217
- METAS.each do |m|
218
- if value = self.attributes[m.to_sym]
219
- meta = Element.new 'meta', head
220
- meta.attributes['name'] = m
221
- meta.attributes['content'] = value.to_s
222
- end
223
- end
224
-
225
-
226
- self.attributes.each do |k,v|
227
- if k.to_s =~ /\Ameta-(.*)\Z/
228
- meta = Element.new 'meta', head
229
- meta.attributes['name'] = $1
230
- meta.attributes['content'] = v.to_s
231
- end
232
- end
233
-
234
-
235
-
236
- # Create title element
237
- doc_title = self.attributes[:title] || self.attributes[:subject] || ""
238
- title = Element.new 'title', head
239
- title << Text.new(doc_title)
240
-
241
- add_css_to(head)
242
-
243
-
244
- root << xml_newline
245
-
246
- body = Element.new 'body'
247
-
248
- children_to_html.each do |e|
249
- body << e
250
- end
251
-
252
- # render footnotes
253
- if @doc.footnotes_order.size > 0
254
- body << render_footnotes
255
- end
256
-
257
- # When we are rendering a whole document, we add a signature
258
- # at the bottom.
259
- if get_setting(:maruku_signature)
260
- body << maruku_html_signature
261
- end
262
-
263
- root << body
264
-
265
- doc
266
- end
267
-
268
- def add_css_to(head)
269
- if css_list = self.attributes[:css]
270
- css_list.split.each do |css|
271
- # <link type="text/css" rel="stylesheet" href="..." />
272
- link = Element.new 'link'
273
- link.attributes['type'] = 'text/css'
274
- link.attributes['rel'] = 'stylesheet'
275
- link.attributes['href'] = css
276
- head << link
277
- head << xml_newline
278
- end
279
- end
280
- end
281
-
282
- # returns "st","nd","rd" or "th" as appropriate
283
- def day_suffix(day)
284
- s = {
285
- 1 => 'st',
286
- 2 => 'nd',
287
- 3 => 'rd',
288
- 21 => 'st',
289
- 22 => 'nd',
290
- 23 => 'rd',
291
- 31 => 'st'
292
- }
293
- return s[day] || 'th';
294
- end
295
-
296
- # formats a nice date
297
- def nice_date
298
- t = Time.now
299
- t.strftime(" at %H:%M on ")+
300
- t.strftime("%A, %B %d")+
301
- day_suffix(t.day)+
302
- t.strftime(", %Y")
303
- end
304
-
305
- def maruku_html_signature
306
- div = Element.new 'div'
307
- div.attributes['class'] = 'maruku_signature'
308
- Element.new 'hr', div
309
- span = Element.new 'span', div
310
- span.attributes['style'] = 'font-size: small; font-style: italic'
311
- span << Text.new('Created by ')
312
- a = Element.new('a', span)
313
- a.attributes['href'] = 'http://maruku.rubyforge.org'
314
- a.attributes['title'] = 'Maruku: a Markdown-superset interpreter for Ruby'
315
- a << Text.new('Maruku')
316
- span << Text.new(nice_date+".")
317
- div
318
- end
319
-
320
- def render_footnotes()
321
- div = Element.new 'div'
322
- div.attributes['class'] = 'footnotes'
323
- div << Element.new('hr')
324
- ol = Element.new 'ol'
325
- @doc.footnotes_order.each_with_index do |fid, i| num = i+1
326
- f = self.footnotes[fid]
327
- if f
328
- li = f.wrap_as_element('li')
329
- li.attributes['id'] = "#{get_setting(:doc_prefix)}fn:#{num}"
330
-
331
- a = Element.new 'a'
332
- a.attributes['href'] = "\##{get_setting(:doc_prefix)}fnref:#{num}"
333
- a.attributes['rev'] = 'footnote'
334
- a<< Text.new('&#8617;', true, nil, true)
335
- li.insert_after(li.children.last, a)
336
- ol << li
337
- else
338
- maruku_error "Could not find footnote id '#{fid}' among ["+
339
- self.footnotes.keys.map{|s|"'"+s+"'"}.join(', ')+"]."
340
- end
341
- end
342
- div << ol
343
- div
344
- end
345
-
346
-
347
- def to_html_hrule; create_html_element 'hr' end
348
- def to_html_linebreak; Element.new 'br' end
349
-
350
- # renders children as html and wraps into an element of given name
351
- #
352
- # Sets 'id' if meta is set
353
- def wrap_as_element(name, attributes_to_copy=[])
354
- m = create_html_element(name, attributes_to_copy)
355
- children_to_html.each do |e| m << e; end
215
+ me.attributes['content'] = 'application/xhtml+xml;charset=utf-8'
216
+
217
+ METAS.each do |m|
218
+ if value = self.attributes[m.to_sym]
219
+ meta = Element.new 'meta', head
220
+ meta.attributes['name'] = m
221
+ meta.attributes['content'] = value.to_s
222
+ end
223
+ end
224
+
225
+
226
+ self.attributes.each do |k,v|
227
+ if k.to_s =~ /\Ameta-(.*)\Z/
228
+ meta = Element.new 'meta', head
229
+ meta.attributes['name'] = $1
230
+ meta.attributes['content'] = v.to_s
231
+ end
232
+ end
233
+
234
+
235
+
236
+ # Create title element
237
+ doc_title = self.attributes[:title] || self.attributes[:subject] || ""
238
+ title = Element.new 'title', head
239
+ title << Text.new(doc_title)
240
+
241
+ add_css_to(head)
242
+
243
+
244
+ root << xml_newline
245
+
246
+ body = Element.new 'body'
247
+
248
+ children_to_html.each do |e|
249
+ body << e
250
+ end
251
+
252
+ # render footnotes
253
+ if @doc.footnotes_order.size > 0
254
+ body << render_footnotes
255
+ end
256
+
257
+ # When we are rendering a whole document, we add a signature
258
+ # at the bottom.
259
+ if get_setting(:maruku_signature)
260
+ body << maruku_html_signature
261
+ end
262
+
263
+ root << body
264
+
265
+ doc
266
+ end
267
+
268
+ def add_css_to(head)
269
+ if css_list = self.attributes[:css]
270
+ css_list.split.each do |css|
271
+ # <link type="text/css" rel="stylesheet" href="..." />
272
+ link = Element.new 'link'
273
+ link.attributes['type'] = 'text/css'
274
+ link.attributes['rel'] = 'stylesheet'
275
+ link.attributes['href'] = css
276
+ head << link
277
+ head << xml_newline
278
+ end
279
+ end
280
+ end
281
+
282
+ # returns "st","nd","rd" or "th" as appropriate
283
+ def day_suffix(day)
284
+ s = {
285
+ 1 => 'st',
286
+ 2 => 'nd',
287
+ 3 => 'rd',
288
+ 21 => 'st',
289
+ 22 => 'nd',
290
+ 23 => 'rd',
291
+ 31 => 'st'
292
+ }
293
+ return s[day] || 'th';
294
+ end
295
+
296
+ # formats a nice date
297
+ def nice_date
298
+ t = Time.now
299
+ t.strftime(" at %H:%M on ")+
300
+ t.strftime("%A, %B %d")+
301
+ day_suffix(t.day)+
302
+ t.strftime(", %Y")
303
+ end
304
+
305
+ def maruku_html_signature
306
+ div = Element.new 'div'
307
+ div.attributes['class'] = 'maruku_signature'
308
+ Element.new 'hr', div
309
+ span = Element.new 'span', div
310
+ span.attributes['style'] = 'font-size: small; font-style: italic'
311
+ span << Text.new('Created by ')
312
+ a = Element.new('a', span)
313
+ a.attributes['href'] = 'http://maruku.rubyforge.org'
314
+ a.attributes['title'] = 'Maruku: a Markdown-superset interpreter for Ruby'
315
+ a << Text.new('Maruku')
316
+ span << Text.new(nice_date+".")
317
+ div
318
+ end
319
+
320
+ def render_footnotes()
321
+ div = Element.new 'div'
322
+ div.attributes['class'] = 'footnotes'
323
+ div << Element.new('hr')
324
+ ol = Element.new 'ol'
325
+ @doc.footnotes_order.each_with_index do |fid, i| num = i+1
326
+ f = self.footnotes[fid]
327
+ if f
328
+ li = f.wrap_as_element('li')
329
+ li.attributes['id'] = "#{get_setting(:doc_prefix)}fn:#{num}"
330
+
331
+ a = Element.new 'a'
332
+ a.attributes['href'] = "\##{get_setting(:doc_prefix)}fnref:#{num}"
333
+ a.attributes['rev'] = 'footnote'
334
+ a<< Text.new('&#8617;', true, nil, true)
335
+ li.insert_after(li.children.last, a)
336
+ ol << li
337
+ else
338
+ maruku_error "Could not find footnote id '#{fid}' among ["+
339
+ self.footnotes.keys.map{|s|"'"+s+"'"}.join(', ')+"]."
340
+ end
341
+ end
342
+ div << ol
343
+ div
344
+ end
345
+
346
+
347
+ def to_html_hrule; create_html_element 'hr' end
348
+ def to_html_linebreak; Element.new 'br' end
349
+
350
+ # renders children as html and wraps into an element of given name
351
+ #
352
+ # Sets 'id' if meta is set
353
+ def wrap_as_element(name, attributes_to_copy=[])
354
+ m = create_html_element(name, attributes_to_copy)
355
+ #puts "MMM>>> #{m} - #{attributes_to_copy}"
356
+ children_to_html.each do |e| m << e; end
356
357
 
357
358
  # m << Comment.new( "{"+self.al.to_md+"}") if not self.al.empty?
358
359
  # m << Comment.new( @attributes.inspect) if not @attributes.empty?
359
- m
360
- end
360
+ m
361
+ end
361
362
 
362
363
  =begin maruku_doc
363
364
  Attribute: id
@@ -391,85 +392,87 @@ It is copied as a standard HTML attribute.
391
392
 
392
393
 
393
394
 
394
- unless defined? HTML4Attributes
395
- HTML4Attributes = {}
396
- end
397
-
398
- coreattrs = [:id, :class, :style, :title]
399
- i18n = [:lang, 'xml:lang'.to_sym]
400
- events = [
401
- :onclick, :ondblclick, :onmousedown, :onmouseup, :onmouseover,
402
- :onmousemove, :onmouseout,
403
- :onkeypress, :onkeydown, :onkeyup]
404
- attrs = coreattrs + i18n + events
405
- cellhalign = [:align, :char, :charoff]
406
- cellvalign = [:valign]
407
- [
408
- ['body', attrs + [:onload, :onunload]],
409
- ['address', attrs],
410
- ['div', attrs],
411
- ['a', attrs+[:charset, :type, :name, :rel, :rev, :accesskey, :shape, :coords, :tabindex,
412
- :onfocus,:onblur]],
413
- ['img', attrs + [:longdesc, :name, :height, :width, :alt] ],
414
- ['p', attrs],
415
- [['h1','h2','h3','h4','h5','h6'], attrs],
416
- [['pre'], attrs],
417
- [['q', 'blockquote'], attrs+[:cite]],
418
- [['ins','del'], attrs+[:cite,:datetime]],
419
- [['ol','ul','li'], attrs],
420
- ['table',attrs+[:summary, :width, :frame, :rules, :border, :cellspacing, :cellpadding]],
421
- ['caption',attrs],
422
- [['colgroup','col'],attrs+[:span, :width]+cellhalign+cellvalign],
423
- [['thead','tbody','tfoot'], attrs+cellhalign+cellvalign],
424
- [['td','td','th'], attrs+[:abbr, :axis, :headers, :scope, :rowspan, :colspan, :cellvalign, :cellhalign]],
425
-
426
- # altri
427
- [['em','code','strong','hr','span','dl','dd','dt'], attrs]
428
- ].each do |el, a| [*el].each do |e| HTML4Attributes[e] = a end end
429
-
430
-
431
- def create_html_element(name, attributes_to_copy=[])
432
- m = Element.new name
433
- if atts = HTML4Attributes[name] then
434
- atts.each do |att|
435
- if v = @attributes[att] then
436
- m.attributes[att.to_s] = v.to_s
437
- end
438
- end
439
- else
440
- # puts "not atts for #{name.inspect}"
441
- end
442
- m
443
- end
444
-
445
-
446
- def to_html_ul
447
- if @attributes[:toc]
448
- # render toc
449
- html_toc = @doc.toc.to_html
450
- return html_toc
451
- else
452
- add_ws wrap_as_element('ul')
453
- end
454
- end
455
-
456
-
457
- def to_html_paragraph
458
- #add_ws wrap_as_element('p')
459
- dt = Element.new 'p'
395
+ unless defined? HTML4Attributes
396
+ HTML4Attributes = {}
397
+ end
398
+
399
+ coreattrs = [:id, :class, :style, :title]
400
+ i18n = [:lang, 'xml:lang'.to_sym]
401
+ events = [
402
+ :onclick, :ondblclick, :onmousedown, :onmouseup, :onmouseover,
403
+ :onmousemove, :onmouseout,
404
+ :onkeypress, :onkeydown, :onkeyup]
405
+ attrs = coreattrs + i18n + events
406
+ cellhalign = [:align, :char, :charoff]
407
+ cellvalign = [:valign]
408
+ [
409
+ ['body', attrs + [:onload, :onunload]],
410
+ ['address', attrs],
411
+ ['div', attrs],
412
+ ['a', attrs+[:charset, :type, :name, :rel, :rev, :accesskey, :shape, :coords, :tabindex,
413
+ :onfocus,:onblur]],
414
+ ['img', attrs + [:longdesc, :name, :height, :width, :alt] ],
415
+ ['p', attrs],
416
+ [['h1','h2','h3','h4','h5','h6'], attrs],
417
+ [['pre'], attrs],
418
+ [['q', 'blockquote'], attrs+[:cite]],
419
+ [['ins','del'], attrs+[:cite,:datetime]],
420
+ [['ol','ul','li'], attrs],
421
+ ['table',attrs+[:summary, :width, :frame, :rules, :border, :cellspacing, :cellpadding]],
422
+ ['caption',attrs],
423
+ [['colgroup','col'],attrs+[:span, :width]+cellhalign+cellvalign],
424
+ [['thead','tbody','tfoot'], attrs+cellhalign+cellvalign],
425
+ [['td','td','th'], attrs+[:abbr, :axis, :headers, :scope, :rowspan, :colspan, :cellvalign, :cellhalign]],
426
+
427
+ # altri
428
+ [['em','code','strong','hr','span','dl','dd','dt'], attrs]
429
+ ].each do |el, a| [*el].each do |e| HTML4Attributes[e] = a end end
430
+
431
+
432
+ def create_html_element(name, attributes_to_copy=[])
433
+ m = Element.new name
434
+ #puts "ATTR>>> #{@attributes}"
435
+ if atts = HTML4Attributes[name] then
436
+ atts.each do |att|
437
+ if v = @attributes[att] then
438
+ m.attributes[att.to_s] = v.to_s
439
+ end
440
+ end
441
+ else
442
+ # puts "not atts for #{name.inspect}"
443
+ end
444
+ m
445
+ end
446
+
447
+
448
+ def to_html_ul
449
+ if @attributes[:toc]
450
+ # render toc
451
+ html_toc = @doc.toc.to_html
452
+ return html_toc
453
+ else
454
+ add_ws wrap_as_element('ul')
455
+ end
456
+ end
457
+
458
+
459
+ def to_html_paragraph
460
+ #add_ws wrap_as_element('p')
461
+ dt = Element.new 'p'
460
462
  dt.attributes['class'] = 'drop-target'
461
- dt.attributes['line_no'] = Thread.current['line_no']
463
+ dt.attributes['line_no'] = Thread.current['maruku.line_no']
462
464
  dt.attributes['delegate'] = 'plan' # should most likely go into the js column handler
463
- p = wrap_as_element('p')
464
- p.attributes['class'] = 'content'
465
- [Text.new("\n"), p, dt, Text.new("\n")]
466
- end
467
- def to_html_ol; add_ws wrap_as_element('ol') end
468
- def to_html_li; add_ws wrap_as_element('li') end
469
- def to_html_li_span; add_ws wrap_as_element('li') end
470
- def to_html_quote; add_ws wrap_as_element('blockquote') end
471
- def to_html_strong; wrap_as_element('strong') end
472
- def to_html_emphasis; wrap_as_element('em') end
465
+ dt.text = ' ';
466
+ p = wrap_as_element('p')
467
+ p.attributes['class'] = 'content'
468
+ [Text.new("\n"), p, dt, Text.new("\n")]
469
+ end
470
+ def to_html_ol; add_ws wrap_as_element('ol') end
471
+ def to_html_li; add_ws wrap_as_element('li') end
472
+ def to_html_li_span; add_ws wrap_as_element('li') end
473
+ def to_html_quote; add_ws wrap_as_element('blockquote') end
474
+ def to_html_strong; wrap_as_element('strong') end
475
+ def to_html_emphasis; wrap_as_element('em') end
473
476
 
474
477
  =begin maruku_doc
475
478
  Attribute: use_numbered_headers
@@ -482,59 +485,60 @@ In LaTeX export, the numbering of headers is managed
482
485
  by Maruku, to have the same results in both HTML and LaTeX.
483
486
  =end
484
487
 
485
- # nil if not applicable, else string
486
- def section_number
487
- return nil if not get_setting(:use_numbered_headers)
488
-
489
- n = @attributes[:section_number]
490
- if n && (not n.empty?)
491
- n.join('.')+". "
492
- else
493
- nil
494
- end
495
- end
496
-
497
- # nil if not applicable, else SPAN element
498
- def render_section_number
499
- # if we are bound to a section, add section number
500
- if num = section_number
501
- span = Element.new 'span'
502
- span.attributes['class'] = 'maruku_section_number'
503
- span << Text.new(section_number)
504
- span
505
- else
506
- nil
507
- end
508
- end
509
-
510
- def to_html_header_orig
511
- element_name = "h#{self.level}"
512
- h = wrap_as_element element_name
513
-
514
- if span = render_section_number
515
- h.insert_before(h.children.first, span)
516
- end
517
- add_ws h
518
- end
519
-
520
- def to_html_header
521
- element_name = "h#{self.level}"
522
- h = wrap_as_element element_name
523
-
524
- if span = render_section_number
525
- h.insert_before(h.children.first, span)
526
- end
527
- add_ws h
528
- end
529
-
530
-
531
- def source2html(source)
488
+ # nil if not applicable, else string
489
+ def section_number
490
+ return nil if not get_setting(:use_numbered_headers)
491
+
492
+ n = @attributes[:section_number]
493
+ if n && (not n.empty?)
494
+ n.join('.')+". "
495
+ else
496
+ nil
497
+ end
498
+ end
499
+
500
+ # nil if not applicable, else SPAN element
501
+ def render_section_number
502
+ # if we are bound to a section, add section number
503
+ if num = section_number
504
+ span = Element.new 'span'
505
+ span.attributes['class'] = 'maruku_section_number'
506
+ span << Text.new(section_number)
507
+ span
508
+ else
509
+ nil
510
+ end
511
+ end
512
+
513
+ def to_html_header_orig
514
+ element_name = "h#{self.level}"
515
+ h = wrap_as_element element_name
516
+
517
+ if span = render_section_number
518
+ h.insert_before(h.children.first, span)
519
+ end
520
+ add_ws h
521
+ end
522
+
523
+ def to_html_header
524
+ element_name = "h#{self.level}"
525
+ h = wrap_as_element element_name
526
+
527
+ if span = render_section_number
528
+ h.insert_before(h.children.first, span)
529
+ end
530
+ #puts "h>>> #{h}"
531
+ add_ws h
532
+ end
533
+
534
+
535
+ def source2html(source)
532
536
  # source = source.gsub(/&/,'&amp;')
533
- source = Text.normalize(source)
534
- source = source.gsub(/\&apos;/,'&#39;') # IE bug
535
- source = source.gsub(/'/,'&#39;') # IE bug
536
- Text.new(source, true, nil, true )
537
- end
537
+ source = Text.normalize(source)
538
+ source = source.gsub(/\&apos;/,'&#39;') # IE bug
539
+ source = source.gsub(/'/,'&#39;') # IE bug
540
+ Text.new(source, true, nil, true )
541
+ end
538
542
 
539
543
  =begin maruku_doc
540
544
  Attribute: html_use_syntax
@@ -549,86 +553,86 @@ languages. Remember to set the `lang` attribute of the code block.
549
553
 
550
554
  Examples:
551
555
 
552
- require 'maruku'
553
- {:lang=ruby html_use_syntax=true}
556
+ require 'maruku'
557
+ {:lang=ruby html_use_syntax=true}
554
558
 
555
559
  and
556
560
 
557
- <div style="text-align:center">Div</div>
558
- {:lang=html html_use_syntax=true}
561
+ <div style="text-align:center">Div</div>
562
+ {:lang=html html_use_syntax=true}
559
563
 
560
564
  produces:
561
565
 
562
- require 'maruku'
566
+ require 'maruku'
563
567
  {:lang=ruby html_use_syntax=true}
564
568
 
565
569
  and
566
570
 
567
- <div style="text-align:center">Div</div>
571
+ <div style="text-align:center">Div</div>
568
572
  {:lang=html html_use_syntax=true}
569
573
 
570
574
  =end
571
575
 
572
- $syntax_loaded = false
573
- def to_html_code;
574
- source = self.raw_code
575
-
576
- lang = self.attributes[:lang] || @doc.attributes[:code_lang]
577
-
578
- lang = 'xml' if lang=='html'
579
-
580
- use_syntax = get_setting :html_use_syntax
581
-
582
- element =
583
- if use_syntax && lang
584
- begin
585
- if not $syntax_loaded
586
- require 'rubygems'
587
- require 'syntax'
588
- require 'syntax/convertors/html'
589
- $syntax_loaded = true
590
- end
591
- convertor = Syntax::Convertors::HTML.for_syntax lang
592
-
593
- # eliminate trailing newlines otherwise Syntax crashes
594
- source = source.gsub(/\n*\Z/,'')
595
-
596
- html = convertor.convert( source )
597
- html = html.gsub(/\&apos;/,'&#39;') # IE bug
598
- html = html.gsub(/'/,'&#39;') # IE bug
599
- # html = html.gsub(/&/,'&amp;')
600
-
601
- code = Document.new(html, {:respect_whitespace =>:all}).root
602
- code.name = 'code'
603
- code.attributes['class'] = lang
604
- code.attributes['lang'] = lang
605
-
606
- pre = Element.new 'pre'
607
- pre << code
608
- pre
609
- rescue LoadError => e
610
- maruku_error "Could not load package 'syntax'.\n"+
611
- "Please install it, for example using 'gem install syntax'."
612
- to_html_code_using_pre(source)
613
- rescue Object => e
614
- maruku_error"Error while using the syntax library for code:\n#{source.inspect}"+
615
- "Lang is #{lang} object is: \n"+
616
- self.inspect +
617
- "\nException: #{e.class}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
618
-
619
- tell_user("Using normal PRE because the syntax library did not work.")
620
- to_html_code_using_pre(source)
621
- end
622
- else
623
- to_html_code_using_pre(source)
624
- end
625
-
626
- color = get_setting(:code_background_color)
627
- if color != Globals[:code_background_color]
628
- element.attributes['style'] = "background-color: #{color};"
629
- end
630
- add_ws element
631
- end
576
+ $syntax_loaded = false
577
+ def to_html_code;
578
+ source = self.raw_code
579
+
580
+ lang = self.attributes[:lang] || @doc.attributes[:code_lang]
581
+
582
+ lang = 'xml' if lang=='html'
583
+
584
+ use_syntax = get_setting :html_use_syntax
585
+
586
+ element =
587
+ if use_syntax && lang
588
+ begin
589
+ if not $syntax_loaded
590
+ require 'rubygems'
591
+ require 'syntax'
592
+ require 'syntax/convertors/html'
593
+ $syntax_loaded = true
594
+ end
595
+ convertor = Syntax::Convertors::HTML.for_syntax lang
596
+
597
+ # eliminate trailing newlines otherwise Syntax crashes
598
+ source = source.gsub(/\n*\Z/,'')
599
+
600
+ html = convertor.convert( source )
601
+ html = html.gsub(/\&apos;/,'&#39;') # IE bug
602
+ html = html.gsub(/'/,'&#39;') # IE bug
603
+ # html = html.gsub(/&/,'&amp;')
604
+
605
+ code = Document.new(html, {:respect_whitespace =>:all}).root
606
+ code.name = 'code'
607
+ code.attributes['class'] = lang
608
+ code.attributes['lang'] = lang
609
+
610
+ pre = Element.new 'pre'
611
+ pre << code
612
+ pre
613
+ rescue LoadError => e
614
+ maruku_error "Could not load package 'syntax'.\n"+
615
+ "Please install it, for example using 'gem install syntax'."
616
+ to_html_code_using_pre(source)
617
+ rescue Object => e
618
+ maruku_error"Error while using the syntax library for code:\n#{source.inspect}"+
619
+ "Lang is #{lang} object is: \n"+
620
+ self.inspect +
621
+ "\nException: #{e.class}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
622
+
623
+ tell_user("Using normal PRE because the syntax library did not work.")
624
+ to_html_code_using_pre(source)
625
+ end
626
+ else
627
+ to_html_code_using_pre(source)
628
+ end
629
+
630
+ color = get_setting(:code_background_color)
631
+ if color != Globals[:code_background_color]
632
+ element.attributes['style'] = "background-color: #{color};"
633
+ end
634
+ add_ws element
635
+ end
632
636
 
633
637
  =begin maruku_doc
634
638
  Attribute: code_background_color
@@ -650,182 +654,182 @@ of the form `#ff00ff`.
650
654
  =end
651
655
 
652
656
 
653
- def to_html_code_using_pre(source)
654
- pre = create_html_element 'pre'
655
- code = Element.new 'code', pre
656
- s = source
657
+ def to_html_code_using_pre(source)
658
+ pre = create_html_element 'pre'
659
+ code = Element.new 'code', pre
660
+ s = source
657
661
 
658
662
  # s = s.gsub(/&/,'&amp;')
659
- s = Text.normalize(s)
660
- s = s.gsub(/\&apos;/,'&#39;') # IE bug
661
- s = s.gsub(/'/,'&#39;') # IE bug
662
-
663
- if get_setting(:code_show_spaces)
664
- # 187 = raquo
665
- # 160 = nbsp
666
- # 172 = not
667
- s.gsub!(/\t/,'&#187;'+'&#160;'*3)
668
- s.gsub!(/ /,'&#172;')
669
- end
670
-
671
- text = Text.new(s, respect_ws=true, parent=nil, raw=true )
672
-
673
- if lang = self.attributes[:lang]
674
- code.attributes['lang'] = lang
675
- code.attributes['class'] = lang
676
- end
677
- code << text
678
- pre
679
- end
680
-
681
- def to_html_inline_code;
682
- pre = create_html_element 'code'
683
- source = self.raw_code
684
- pre << source2html(source)
685
-
686
- color = get_setting(:code_background_color)
687
- if color != Globals[:code_background_color]
688
- pre.attributes['style'] = "background-color: #{color};"+(pre.attributes['style']||"")
689
- end
690
-
691
- pre
692
- end
693
-
694
- def add_class_to(el, cl)
695
- el.attributes['class'] =
696
- if already = el.attributes['class']
697
- already + " " + cl
698
- else
699
- cl
700
- end
701
- end
702
-
703
- def add_class_to_link(a)
704
- return # not ready yet
705
-
706
- # url = a.attributes['href']
707
- # return if not url
708
- #
709
- # if url =~ /^#/
710
- # add_class_to(a, 'maruku-link-samedoc')
711
- # elsif url =~ /^http:/
712
- # add_class_to(a, 'maruku-link-external')
713
- # else
714
- # add_class_to(a, 'maruku-link-local')
715
- # end
716
- #
663
+ s = Text.normalize(s)
664
+ s = s.gsub(/\&apos;/,'&#39;') # IE bug
665
+ s = s.gsub(/'/,'&#39;') # IE bug
666
+
667
+ if get_setting(:code_show_spaces)
668
+ # 187 = raquo
669
+ # 160 = nbsp
670
+ # 172 = not
671
+ s.gsub!(/\t/,'&#187;'+'&#160;'*3)
672
+ s.gsub!(/ /,'&#172;')
673
+ end
674
+
675
+ text = Text.new(s, respect_ws=true, parent=nil, raw=true )
676
+
677
+ if lang = self.attributes[:lang]
678
+ code.attributes['lang'] = lang
679
+ code.attributes['class'] = lang
680
+ end
681
+ code << text
682
+ pre
683
+ end
684
+
685
+ def to_html_inline_code;
686
+ pre = create_html_element 'code'
687
+ source = self.raw_code
688
+ pre << source2html(source)
689
+
690
+ color = get_setting(:code_background_color)
691
+ if color != Globals[:code_background_color]
692
+ pre.attributes['style'] = "background-color: #{color};"+(pre.attributes['style']||"")
693
+ end
694
+
695
+ pre
696
+ end
697
+
698
+ def add_class_to(el, cl)
699
+ el.attributes['class'] =
700
+ if already = el.attributes['class']
701
+ already + " " + cl
702
+ else
703
+ cl
704
+ end
705
+ end
706
+
707
+ def add_class_to_link(a)
708
+ return # not ready yet
709
+
710
+ # url = a.attributes['href']
711
+ # return if not url
712
+ #
713
+ # if url =~ /^#/
714
+ # add_class_to(a, 'maruku-link-samedoc')
715
+ # elsif url =~ /^http:/
716
+ # add_class_to(a, 'maruku-link-external')
717
+ # else
718
+ # add_class_to(a, 'maruku-link-local')
719
+ # end
720
+ #
717
721
  # puts a.attributes['class']
718
- end
719
-
720
-
721
- def to_html_immediate_link
722
- a = create_html_element 'a'
723
- url = self.url
724
- text = url.gsub(/^mailto:/,'') # don't show mailto
725
- a << Text.new(text)
726
- a.attributes['href'] = url
727
- add_class_to_link(a)
728
- a
729
- end
730
-
731
- def to_html_link
732
- a = wrap_as_element 'a'
733
- id = self.ref_id
734
-
735
- if ref = @doc.refs[id]
736
- url = ref[:url]
737
- title = ref[:title]
738
- a.attributes['href'] = url if url
739
- a.attributes['title'] = title if title
740
- else
741
- maruku_error "Could not find ref_id = #{id.inspect} for #{self.inspect}\n"+
742
- "Available refs are #{@doc.refs.keys.inspect}"
743
- tell_user "Not creating a link for ref_id = #{id.inspect}."
744
- return wrap_as_element('span')
745
- end
722
+ end
723
+
724
+
725
+ def to_html_immediate_link
726
+ a = create_html_element 'a'
727
+ url = self.url
728
+ text = url.gsub(/^mailto:/,'') # don't show mailto
729
+ a << Text.new(text)
730
+ a.attributes['href'] = url
731
+ add_class_to_link(a)
732
+ a
733
+ end
734
+
735
+ def to_html_link
736
+ a = wrap_as_element 'a'
737
+ id = self.ref_id
738
+
739
+ if ref = @doc.refs[id]
740
+ url = ref[:url]
741
+ title = ref[:title]
742
+ a.attributes['href'] = url if url
743
+ a.attributes['title'] = title if title
744
+ else
745
+ maruku_error "Could not find ref_id = #{id.inspect} for #{self.inspect}\n"+
746
+ "Available refs are #{@doc.refs.keys.inspect}"
747
+ tell_user "Not creating a link for ref_id = #{id.inspect}."
748
+ return wrap_as_element('span')
749
+ end
746
750
 
747
751
  # add_class_to_link(a)
748
- return a
749
- end
750
-
751
- def to_html_im_link
752
- if url = self.url
753
- title = self.title
754
- a = wrap_as_element 'a'
755
- a.attributes['href'] = url
752
+ return a
753
+ end
754
+
755
+ def to_html_im_link
756
+ if url = self.url
757
+ title = self.title
758
+ a = wrap_as_element 'a'
759
+ a.attributes['href'] = url
756
760
  a.attributes['xhref'] = url # BUG ALERT: Strange behaviour of jquery's replaceWith related to href
757
- a.attributes['title'] = title if title
758
- return a
759
- else
760
- maruku_error"Could not find url in #{self.inspect}"
761
- tell_user "Not creating a link for ref_id = #{id.inspect}."
762
- return wrap_as_element('span')
763
- end
764
- end
765
-
766
- def add_ws(e)
767
- [Text.new("\n"), e, Text.new("\n")]
768
- end
761
+ a.attributes['title'] = title if title
762
+ return a
763
+ else
764
+ maruku_error"Could not find url in #{self.inspect}"
765
+ tell_user "Not creating a link for ref_id = #{id.inspect}."
766
+ return wrap_as_element('span')
767
+ end
768
+ end
769
+
770
+ def add_ws(e)
771
+ [Text.new("\n"), e, Text.new("\n")]
772
+ end
769
773
  ##### Email address
770
774
 
771
- def obfuscate(s)
772
- res = ''
773
- s.each_byte do |char|
774
- res += "&#%03d;" % char
775
- end
776
- res
777
- end
778
-
779
- def to_html_email_address
780
- email = self.email
781
- a = create_html_element 'a'
782
- #a.attributes['href'] = Text.new("mailto:"+obfuscate(email),false,nil,true)
783
- #a.attributes.add Attribute.new('href',Text.new(
784
- #"mailto:"+obfuscate(email),false,nil,true))
785
- # Sorry, for the moment it doesn't work
786
- a.attributes['href'] = "mailto:#{email}"
787
-
788
- a << Text.new(obfuscate(email),false,nil,true)
789
- a
790
- end
775
+ def obfuscate(s)
776
+ res = ''
777
+ s.each_byte do |char|
778
+ res += "&#%03d;" % char
779
+ end
780
+ res
781
+ end
782
+
783
+ def to_html_email_address
784
+ email = self.email
785
+ a = create_html_element 'a'
786
+ #a.attributes['href'] = Text.new("mailto:"+obfuscate(email),false,nil,true)
787
+ #a.attributes.add Attribute.new('href',Text.new(
788
+ #"mailto:"+obfuscate(email),false,nil,true))
789
+ # Sorry, for the moment it doesn't work
790
+ a.attributes['href'] = "mailto:#{email}"
791
+
792
+ a << Text.new(obfuscate(email),false,nil,true)
793
+ a
794
+ end
791
795
 
792
796
  ##### Images
793
797
 
794
- def to_html_image
795
- a = create_html_element 'img'
796
- id = self.ref_id
797
- if ref = @doc.refs[id]
798
- url = ref[:url]
799
- title = ref[:title]
800
- a.attributes['src'] = url.to_s
801
- a.attributes['alt'] = children_to_s
802
- else
803
- maruku_error"Could not find id = #{id.inspect} for\n #{self.inspect}"
804
- tell_user "Could not create image with ref_id = #{id.inspect};"+
805
- " Using SPAN element as replacement."
806
- return wrap_as_element('span')
807
- end
798
+ def to_html_image
799
+ a = create_html_element 'img'
800
+ id = self.ref_id
801
+ if ref = @doc.refs[id]
802
+ url = ref[:url]
803
+ title = ref[:title]
804
+ a.attributes['src'] = url.to_s
805
+ a.attributes['alt'] = children_to_s
806
+ else
807
+ maruku_error"Could not find id = #{id.inspect} for\n #{self.inspect}"
808
+ tell_user "Could not create image with ref_id = #{id.inspect};"+
809
+ " Using SPAN element as replacement."
810
+ return wrap_as_element('span')
811
+ end
808
812
  raise "IMAGE: #{url}"
809
- return a
810
- end
811
-
812
- def to_html_im_image
813
- if not url = self.url
814
- maruku_error "Image with no url: #{self.inspect}"
815
- tell_user "Could not create image with ref_id = #{id.inspect};"+
816
- " Using SPAN element as replacement."
817
- return wrap_as_element('span')
818
- end
819
- if url_resolver = (Thread.current['maruku_context'] || {})[:img_url_resolver]
820
- url = url_resolver.call(url)
821
- end
822
-
823
- title = self.title
824
- a = create_html_element 'img'
825
- a.attributes['src'] = url.to_s
826
- a.attributes['alt'] = children_to_s
827
- return a
828
- end
813
+ return a
814
+ end
815
+
816
+ def to_html_im_image
817
+ if not url = self.url
818
+ maruku_error "Image with no url: #{self.inspect}"
819
+ tell_user "Could not create image with ref_id = #{id.inspect};"+
820
+ " Using SPAN element as replacement."
821
+ return wrap_as_element('span')
822
+ end
823
+ if url_resolver = (Thread.current['maruku_context'] || {})[:img_url_resolver]
824
+ url = url_resolver.call(url)
825
+ end
826
+
827
+ title = self.title
828
+ a = create_html_element 'img'
829
+ a.attributes['src'] = url.to_s
830
+ a.attributes['alt'] = children_to_s
831
+ return a
832
+ end
829
833
 
830
834
  =begin maruku_doc
831
835
  Attribute: filter_html
@@ -835,208 +839,206 @@ If true, raw HTML is discarded from the output.
835
839
 
836
840
  =end
837
841
 
838
- def to_html_raw_html
839
- return [] if get_setting(:filter_html)
840
-
841
- raw_html = self.raw_html
842
- if rexml_doc = @parsed_html
843
- root = rexml_doc.root
844
- if root.nil?
845
- s = "Bug in REXML: root() of Document is nil: \n#{rexml_doc.inspect}\n"+
846
- "Raw HTML:\n#{raw_html.inspect}"
847
- maruku_error s
848
- tell_user 'The REXML version you have has a bug, omitting HTML'
849
- div = Element.new 'div'
850
- #div << Text.new(s)
851
- return div
852
- end
853
-
854
- # copies the @children array (FIXME is it deep?)
855
- elements = root.to_a
856
- return elements
857
- else # invalid
858
- # Creates red box with offending HTML
859
- tell_user "Wrapping bad html in a PRE with class 'markdown-html-error'\n"+
860
- add_tabs(raw_html,1,'|')
861
- pre = Element.new('pre')
862
- pre.attributes['style'] = 'border: solid 3px red; background-color: pink'
863
- pre.attributes['class'] = 'markdown-html-error'
864
- pre << Text.new("REXML could not parse this XML/HTML: \n#{raw_html}", true)
865
- return pre
866
- end
867
- end
868
-
869
- def to_html_abbr
870
- abbr = Element.new 'abbr'
871
- abbr << Text.new(children[0])
872
- abbr.attributes['title'] = self.title if self.title
873
- abbr
874
- end
875
-
876
- def to_html_footnote_reference
877
- id = self.footnote_id
878
-
879
- # save the order of used footnotes
880
- order = @doc.footnotes_order
881
-
882
- if order.include? id
883
- # footnote has already been used
884
- return []
885
- end
886
-
887
- if not @doc.footnotes[id]
888
- return []
842
+ def to_html_raw_html
843
+ return [] if get_setting(:filter_html)
844
+
845
+ raw_html = self.raw_html
846
+ if rexml_doc = @parsed_html
847
+ root = rexml_doc.root
848
+ if root.nil?
849
+ s = "Bug in REXML: root() of Document is nil: \n#{rexml_doc.inspect}\n"+
850
+ "Raw HTML:\n#{raw_html.inspect}"
851
+ maruku_error s
852
+ tell_user 'The REXML version you have has a bug, omitting HTML'
853
+ div = Element.new 'div'
854
+ #div << Text.new(s)
855
+ return div
856
+ end
857
+
858
+ # copies the @children array (FIXME is it deep?)
859
+ elements = root.to_a
860
+ return elements
861
+ else # invalid
862
+ # Creates red box with offending HTML
863
+ tell_user "Wrapping bad html in a PRE with class 'markdown-html-error'\n"+
864
+ add_tabs(raw_html,1,'|')
865
+ pre = Element.new('pre')
866
+ pre.attributes['style'] = 'border: solid 3px red; background-color: pink'
867
+ pre.attributes['class'] = 'markdown-html-error'
868
+ pre << Text.new("REXML could not parse this XML/HTML: \n#{raw_html}", true)
869
+ return pre
870
+ end
871
+ end
872
+
873
+ def to_html_abbr
874
+ abbr = Element.new 'abbr'
875
+ abbr << Text.new(children[0])
876
+ abbr.attributes['title'] = self.title if self.title
877
+ abbr
878
+ end
879
+
880
+ def to_html_footnote_reference
881
+ id = self.footnote_id
882
+
883
+ # save the order of used footnotes
884
+ order = @doc.footnotes_order
885
+
886
+ if order.include? id
887
+ # footnote has already been used
888
+ return []
889
889
  end
890
890
 
891
- # take next number
892
- order << id
891
+ if not @doc.footnotes[id]
892
+ return []
893
+ end
893
894
 
894
- #num = order.size;
895
- num = order.index(id) + 1
895
+ # take next number
896
+ order << id
896
897
 
897
- sup = Element.new 'sup'
898
- sup.attributes['id'] = "#{get_setting(:doc_prefix)}fnref:#{num}"
899
- a = Element.new 'a'
900
- a << Text.new(num.to_s)
901
- a.attributes['href'] = "\##{get_setting(:doc_prefix)}fn:#{num}"
902
- a.attributes['rel'] = 'footnote'
903
- sup << a
898
+ #num = order.size;
899
+ num = order.index(id) + 1
904
900
 
905
- sup
906
- end
901
+ sup = Element.new 'sup'
902
+ sup.attributes['id'] = "#{get_setting(:doc_prefix)}fnref:#{num}"
903
+ a = Element.new 'a'
904
+ a << Text.new(num.to_s)
905
+ a.attributes['href'] = "\##{get_setting(:doc_prefix)}fn:#{num}"
906
+ a.attributes['rel'] = 'footnote'
907
+ sup << a
908
+
909
+ sup
910
+ end
907
911
 
908
912
  ## Definition lists ###
909
- def to_html_definition_list() add_ws wrap_as_element('dl') end
910
- def to_html_definition() children_to_html end
911
- def to_html_definition_term() add_ws wrap_as_element('dt') end
912
- def to_html_definition_data() add_ws wrap_as_element('dd') end
913
-
914
- # FIXME: Ugly code
915
- def to_html_table
916
- align = self.align
917
- num_columns = align.size
918
-
919
- head = @children.slice(0, num_columns)
920
- rows = []
921
- i = num_columns
922
- while i<@children.size
923
- rows << @children.slice(i, num_columns)
924
- i += num_columns
925
- end
926
-
927
- table = create_html_element 'table'
928
- thead = Element.new 'thead'
929
- tr = Element.new 'tr'
930
- array_to_html(head).each do |x| tr<<x end
931
- thead << tr
932
- table << thead
933
-
934
- tbody = Element.new 'tbody'
935
- rows.each do |row|
936
- tr = Element.new 'tr'
937
- array_to_html(row).each_with_index do |x,i|
938
- x.attributes['style'] ="text-align: #{align[i].to_s};"
939
- tr<<x
940
- end
941
-
942
- tbody << tr << Text.new("\n")
943
- end
944
- table << tbody
945
- table
946
- end
947
-
948
- def to_html_head_cell; wrap_as_element('th') end
949
- def to_html_cell
950
- if @attributes[:scope]
951
- wrap_as_element('th', [:scope])
952
- else
953
- wrap_as_element('td')
954
- end
955
- end
956
-
957
- def to_html_entity
958
- MaRuKu::Out::Latex.need_entity_table
959
-
960
- entity_name = self.entity_name
961
-
962
- if (e = MaRuKu::Out::Latex::ENTITY_TABLE[entity_name]) && e.html_num
963
- entity_name = e.html_num
964
- end
965
-
966
- # Fix for Internet Explorer
967
- if entity_name == 'apos'
968
- entity_name = 39
969
- end
970
-
971
-
972
- if entity_name.kind_of? Fixnum
913
+ def to_html_definition_list() add_ws wrap_as_element('dl') end
914
+ def to_html_definition() children_to_html end
915
+ def to_html_definition_term() add_ws wrap_as_element('dt') end
916
+ def to_html_definition_data() add_ws wrap_as_element('dd') end
917
+
918
+ # FIXME: Ugly code
919
+ def to_html_table
920
+ align = self.align
921
+ num_columns = align.size
922
+
923
+ head = @children.slice(0, num_columns)
924
+ rows = []
925
+ i = num_columns
926
+ while i<@children.size
927
+ rows << @children.slice(i, num_columns)
928
+ i += num_columns
929
+ end
930
+
931
+ table = create_html_element 'table'
932
+ thead = Element.new 'thead'
933
+ tr = Element.new 'tr'
934
+ array_to_html(head).each do |x| tr<<x end
935
+ thead << tr
936
+ table << thead
937
+
938
+ tbody = Element.new 'tbody'
939
+ rows.each do |row|
940
+ tr = Element.new 'tr'
941
+ array_to_html(row).each_with_index do |x,i|
942
+ x.attributes['style'] ="text-align: #{align[i].to_s};"
943
+ tr<<x
944
+ end
945
+
946
+ tbody << tr << Text.new("\n")
947
+ end
948
+ table << tbody
949
+ table
950
+ end
951
+
952
+ def to_html_head_cell; wrap_as_element('th') end
953
+ def to_html_cell
954
+ if @attributes[:scope]
955
+ wrap_as_element('th', [:scope])
956
+ else
957
+ wrap_as_element('td')
958
+ end
959
+ end
960
+
961
+ def to_html_entity
962
+ MaRuKu::Out::Latex.need_entity_table
963
+
964
+ entity_name = self.entity_name
965
+
966
+ if (e = MaRuKu::Out::Latex::ENTITY_TABLE[entity_name]) && e.html_num
967
+ entity_name = e.html_num
968
+ end
969
+
970
+ # Fix for Internet Explorer
971
+ if entity_name == 'apos'
972
+ entity_name = 39
973
+ end
974
+
975
+
976
+ if entity_name.kind_of? Fixnum
973
977
  # Entity.new(entity_name)
974
- Text.new('&#%d;' % [entity_name], false, nil, true)
975
- else
976
- Text.new('&%s;' % [entity_name], false, nil, true)
977
- end
978
- end
979
-
980
- def to_html_xml_instr
981
- target = self.target || ''
982
- code = self.code || ''
983
- REXML::Instruction.new(target, code)
984
- end
985
-
986
- # Convert each child to html
987
- def children_to_html
988
- array_to_html(@children)
989
- end
990
-
991
- def array_to_html(array)
992
- res = []
993
- res << (e = [])
994
- # puts "init: #{res.inspect}"
995
- first_header = true
996
- array.each do |c|
997
- method = c.kind_of?(MDElement) ?
998
- "to_html_#{c.node_type}" : "to_html"
999
-
1000
- if not c.respond_to?(method)
1001
- #raise "Object does not answer to #{method}: #{c.class} #{c.inspect}"
1002
- next
1003
- end
1004
-
1005
-
1006
- Thread.current['line_no'] = c.line_no if c.respond_to? :line_no
1007
- h = c.send(method)
1008
- # puts "HHHHH: #{h.inspect} e: #{e.inspect}"
1009
-
1010
- if h.nil?
1011
- raise "Nil html created by method #{method}:\n#{h.inspect}\n"+
1012
- " for object #{c.inspect[0,300]}"
1013
- end
1014
-
1015
- if method == 'to_html_header'
1016
- # puts "INNER HEADER res: #{res.inspect} -- e: #{e.inspect}"
1017
-
1018
- if res.length > 1
1019
- # puts "INNER FIND res: #{res.inspect} -- e: #{e.inspect}"
1020
- res.pop
1021
- s = res[-1][-1]
1022
- e.each do |el| s << el end
1023
- e = res[-1]
1024
- end
1025
- e << (s = Element.new('section'))
1026
- #puts c.line_no
1027
- s.attributes['line_no'] = c.line_no
1028
- s.attributes['level'] = c.level
1029
- res << (e = [])
1030
- # puts "AFTER SECTION res: #{res.inspect} -- e: #{e.inspect}"
1031
- end
1032
-
1033
- if h.kind_of?Array
1034
- e.concat(h) #h.each do |hh| e << hh end
1035
- else
1036
- e << h
1037
- end
978
+ Text.new('&#%d;' % [entity_name], false, nil, true)
979
+ else
980
+ Text.new('&%s;' % [entity_name], false, nil, true)
981
+ end
982
+ end
983
+
984
+ def to_html_xml_instr
985
+ target = self.target || ''
986
+ code = self.code || ''
987
+ REXML::Instruction.new(target, code)
988
+ end
989
+
990
+ # Convert each child to html
991
+ def children_to_html
992
+ array_to_html(@children)
993
+ end
994
+
995
+ def array_to_html(array)
996
+ #array.each {|a| puts a.inspect }
997
+ res = []
998
+ res << (e = [])
999
+ first_header = true
1000
+ array.each do |c|
1001
+ method = c.kind_of?(MDElement) ? "to_html_#{c.node_type}" : "to_html"
1002
+ if not c.respond_to?(method)
1003
+ #raise "Object does not answer to #{method}: #{c.class} #{c.inspect}"
1004
+ next
1005
+ end
1006
+
1007
+ Thread.current['maruku.line_no'] = c.line_no if c.respond_to? :line_no
1008
+ h = c.send(method)
1009
+ if h.nil?
1010
+ raise "Nil html created by method #{method}:\n#{h.inspect}\n"+
1011
+ " for object #{c.inspect[0,300]}"
1012
+ end
1013
+
1014
+ if method == 'to_html_header'
1015
+ if res.length > 1
1016
+ res.pop
1017
+ s = res[-1][-1]
1018
+ e.each do |el| s << el end
1019
+ e = res[-1]
1020
+ end
1021
+ e << (s = Element.new('section'))
1022
+ #puts c.inspect
1023
+ s.attributes['line_no'] = c.line_no
1024
+ s.attributes['level'] = c.level
1025
+
1026
+ toc = Thread.current['maruku.toc'] ||= []
1027
+ li = c.level - 1
1028
+ toc[li] = toc[li] ? (toc[li] + 1) : 1
1029
+ s.attributes['toc'] = toc.map {|l| l ? l : 1 }.join('.')
1030
+
1031
+ res << (e = [])
1032
+
1033
+ end
1034
+
1035
+ if h.kind_of?Array
1036
+ e.concat(h) #h.each do |hh| e << hh end
1037
+ else
1038
+ e << h
1039
+ end
1038
1040
  # puts "LOOP ENDN res: #{res.inspect} -- e: #{e.inspect}"
1039
- end
1041
+ end
1040
1042
  # puts "after: #{res.inspect} e: #{e.inspect}"
1041
1043
  if res.length > 1
1042
1044
  res.pop
@@ -1045,11 +1047,11 @@ If true, raw HTML is discarded from the output.
1045
1047
  e = res[-1]
1046
1048
  end
1047
1049
  #puts e.inspect
1048
- e
1049
- end
1050
+ e
1051
+ end
1050
1052
 
1051
- def to_html_ref_definition; [] end
1052
- def to_latex_ref_definition; [] end
1053
+ def to_html_ref_definition; [] end
1054
+ def to_latex_ref_definition; [] end
1053
1055
 
1054
1056
  end # HTML
1055
1057
  end # out