Module: MaRuKu::Out::HTML

Includes:
REXML
Defined in:
lib/omf-web/widget/text/maruku/output/to_html.rb

Constant Summary collapse

Xhtml10strict =
"<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>\n"
Xhtml11strict_mathml2 =
'<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN"
               "http://www.w3.org/TR/MathML2/dtd/xhtml-math11-f.dtd" [
  <!ENTITY mathml "http://www.w3.org/1998/Math/MathML">
]>
'
Xhtml11_mathml2_svg11 =
'<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"
    "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd">
'
METAS =
%w{description keywords author revised}
HTML4Attributes =
{}

Instance Method Summary collapse

Instance Method Details

#add_class_to(el, cl) ⇒ Object



694
695
696
697
698
699
700
701
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 694

def add_class_to(el, cl)
  el.attributes['class'] =
  if already = el.attributes['class']
    already + " " + cl
  else
    cl
  end
end


703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 703

def add_class_to_link(a)
  return # not ready yet

  # url = a.attributes['href']
  # return if not url
  #
  # if url =~ /^#/
  #  add_class_to(a, 'maruku-link-samedoc')
  # elsif url =~ /^http:/
  #  add_class_to(a, 'maruku-link-external')
  # else
  #  add_class_to(a, 'maruku-link-local')
  # end
  #
#   puts a.attributes['class']
end

#add_css_to(head) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 268

def add_css_to(head)
  if css_list = self.attributes[:css]
    css_list.split.each do |css|
    # <link type="text/css" rel="stylesheet" href="..." />
    link = Element.new 'link'
    link.attributes['type'] = 'text/css'
    link.attributes['rel'] = 'stylesheet'
    link.attributes['href'] = css
    head << link
    head << xml_newline
    end
  end
end

#add_ws(e) ⇒ Object



766
767
768
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 766

def add_ws(e)
  [Text.new("\n"), e, Text.new("\n")]
end

#array_to_html(array) ⇒ Object



991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 991

def array_to_html(array)
  res = []
               res << (e = [])
#          puts "init: #{res.inspect}"
               first_header = true
  array.each do |c|
    method = c.kind_of?(MDElement) ?
       "to_html_#{c.node_type}" : "to_html"

    if not c.respond_to?(method)
      #raise "Object does not answer to #{method}: #{c.class} #{c.inspect}"
      next
    end


     Thread.current['line_no'] = c.line_no if c.respond_to? :line_no
    h =  c.send(method)
#            puts "HHHHH: #{h.inspect} e: #{e.inspect}"

    if h.nil?
      raise "Nil html created by method  #{method}:\n#{h.inspect}\n"+
      " for object #{c.inspect[0,300]}"
    end

                       if method == 'to_html_header'
#                            puts "INNER HEADER res: #{res.inspect} -- e: #{e.inspect}"

                         if res.length > 1
#                            puts "INNER FIND res: #{res.inspect} -- e: #{e.inspect}"
                           res.pop
                           s = res[-1][-1]
                           e.each do |el| s << el end
                           e = res[-1]
                         end
                         e << (s = Element.new('section'))
#puts c.line_no
                         s.attributes['line_no'] = c.line_no
                         s.attributes['level'] = c.level
                         res << (e = [])
#                          puts "AFTER SECTION res: #{res.inspect} -- e: #{e.inspect}"
                       end

    if h.kind_of?Array
      e.concat(h) #h.each do |hh| e << hh end
    else
      e << h
    end
#                   puts "LOOP ENDN res: #{res.inspect} -- e: #{e.inspect}"
  end
#                puts "after: #{res.inspect} e: #{e.inspect}"
               if res.length > 1
                 res.pop
                 s = res[-1][-1]
                 e.each do |el| s << el end
                 e = res[-1]
               end
#puts e.inspect
  e
end

#children_to_htmlObject

Convert each child to html



987
988
989
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 987

def children_to_html
  array_to_html(@children)
end

#create_html_element(name, attributes_to_copy = []) ⇒ Object



431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 431

def create_html_element(name, attributes_to_copy=[])
  m = Element.new name
    if atts = HTML4Attributes[name] then
      atts.each do |att|
        if v = @attributes[att] then
          m.attributes[att.to_s] = v.to_s
        end
      end
    else
    # puts "not atts for #{name.inspect}"
    end
  m
end

#day_suffix(day) ⇒ Object

returns “st”,“nd”,“rd” or “th” as appropriate



283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 283

def day_suffix(day)
  s = {
    1 => 'st',
    2 => 'nd',
    3 => 'rd',
    21 => 'st',
    22 => 'nd',
    23 => 'rd',
    31 => 'st'
  }
  return s[day] || 'th';
end

#maruku_html_signatureObject



305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 305

def maruku_html_signature
  div = Element.new 'div'
    div.attributes['class'] = 'maruku_signature'
    Element.new 'hr', div
    span = Element.new 'span', div
      span.attributes['style'] = 'font-size: small; font-style: italic'
      span << Text.new('Created by ')
      a = Element.new('a', span)
        a.attributes['href'] = 'http://maruku.rubyforge.org'
        a.attributes['title'] = 'Maruku: a Markdown-superset interpreter for Ruby'
        a << Text.new('Maruku')
      span << Text.new(nice_date+".")
  div
end

#nice_dateObject

formats a nice date



297
298
299
300
301
302
303
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 297

def nice_date
  t = Time.now
  t.strftime(" at %H:%M on ")+
  t.strftime("%A, %B %d")+
  day_suffix(t.day)+
  t.strftime(", %Y")
end

#obfuscate(s) ⇒ Object

Email address



771
772
773
774
775
776
777
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 771

def obfuscate(s)
  res = ''
  s.each_byte do |char|
    res +=  "&#%03d;" % char
  end
  res
end

#render_footnotesObject



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 320

def render_footnotes()
  div = Element.new 'div'
  div.attributes['class'] = 'footnotes'
  div <<  Element.new('hr')
    ol = Element.new 'ol'
    @doc.footnotes_order.each_with_index do |fid, i| num = i+1
      f = self.footnotes[fid]
      if f
        li = f.wrap_as_element('li')
        li.attributes['id'] = "#{get_setting(:doc_prefix)}fn:#{num}"

        a = Element.new 'a'
          a.attributes['href'] = "\##{get_setting(:doc_prefix)}fnref:#{num}"
          a.attributes['rev'] = 'footnote'
          a<< Text.new('&#8617;', true, nil, true)
        li.insert_after(li.children.last, a)
        ol << li
      else
        maruku_error "Could not find footnote id '#{fid}' among ["+
         self.footnotes.keys.map{|s|"'"+s+"'"}.join(', ')+"]."
      end
    end
  div << ol
  div
end

#render_section_numberObject

nil if not applicable, else SPAN element



498
499
500
501
502
503
504
505
506
507
508
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 498

def render_section_number
  # if we are bound to a section, add section number
  if num = section_number
    span = Element.new 'span'
    span.attributes['class'] = 'maruku_section_number'
    span << Text.new(section_number)
    span
  else
    nil
  end
end

#section_numberObject

nil if not applicable, else string



486
487
488
489
490
491
492
493
494
495
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 486

def section_number
  return nil if not get_setting(:use_numbered_headers)

  n = @attributes[:section_number]
  if n && (not n.empty?)
     n.join('.')+". "
  else
    nil
  end
end

#source2html(source) ⇒ Object



531
532
533
534
535
536
537
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 531

def source2html(source)
#   source = source.gsub(/&/,'&amp;')
  source = Text.normalize(source)
  source = source.gsub(/\&apos;/,'&#39;') # IE bug
  source = source.gsub(/'/,'&#39;') # IE bug
  Text.new(source, true, nil, true )
end

#to_html(context = {}) ⇒ Object

Render as an HTML fragment (no head, just the content of BODY). (returns a string)



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
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 45

def to_html(context={})
  Thread.current['maruku_context'] = context
  indent = context[:indent] || -1
  ie_hack = context[:ie_hack] || true

  div = Element.new 'dummy'
    children_to_html.each do |e|
      div << e
    end

    # render footnotes
    if @doc.footnotes_order.size > 0
      div << render_footnotes
    end

  doc = Document.new(nil,{:respect_whitespace =>:all})
  doc << div

  # REXML Bug? if indent!=-1 whitespace is not respected for 'pre' elements
  # containing code.
  xml =""

  if $rexml_new_version
    formatter = if indent > -1
          REXML::Formatters::Pretty.new( indent, ie_hack )
        else
          REXML::Formatters::Default.new( ie_hack )
        end
    formatter.write( div, xml)
  else
    div.write(xml,indent,transitive=true,ie_hack)
  end

  xml.gsub!(/\A<dummy>\s*/,'')
  xml.gsub!(/\s*<\/dummy>\Z/,'')
  xml.gsub!(/\A<dummy\s*\/>/,'')
  xml
end

#to_html_abbrObject



869
870
871
872
873
874
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 869

def to_html_abbr
  abbr = Element.new 'abbr'
  abbr << Text.new(children[0])
  abbr.attributes['title'] = self.title if self.title
  abbr
end

#to_html_cellObject



949
950
951
952
953
954
955
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 949

def to_html_cell
if @attributes[:scope]
  wrap_as_element('th', [:scope])
else
  wrap_as_element('td')
end
end

#to_html_codeObject



573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 573

def to_html_code;
  source = self.raw_code

  lang = self.attributes[:lang] || @doc.attributes[:code_lang]

  lang = 'xml' if lang=='html'

  use_syntax = get_setting :html_use_syntax

  element =
  if use_syntax && lang
    begin
      if not $syntax_loaded
        require 'rubygems'
        require 'syntax'
        require 'syntax/convertors/html'
        $syntax_loaded = true
      end
      convertor = Syntax::Convertors::HTML.for_syntax lang

      # eliminate trailing newlines otherwise Syntax crashes
      source = source.gsub(/\n*\Z/,'')

      html = convertor.convert( source )
      html = html.gsub(/\&apos;/,'&#39;') # IE bug
      html = html.gsub(/'/,'&#39;') # IE bug
#     html = html.gsub(/&/,'&amp;')

      code = Document.new(html, {:respect_whitespace =>:all}).root
      code.name = 'code'
      code.attributes['class'] = lang
      code.attributes['lang'] = lang

      pre = Element.new 'pre'
      pre << code
      pre
    rescue LoadError => e
      maruku_error "Could not load package 'syntax'.\n"+
        "Please install it, for example using 'gem install syntax'."
      to_html_code_using_pre(source)
    rescue Object => e
      maruku_error"Error while using the syntax library for code:\n#{source.inspect}"+
       "Lang is #{lang} object is: \n"+
        self.inspect +
      "\nException: #{e.class}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"

      tell_user("Using normal PRE because the syntax library did not work.")
      to_html_code_using_pre(source)
    end
  else
    to_html_code_using_pre(source)
  end

  color = get_setting(:code_background_color)
  if color != Globals[:code_background_color]
    element.attributes['style'] = "background-color: #{color};"
  end
  add_ws element
end

#to_html_code_using_pre(source) ⇒ Object



653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 653

def to_html_code_using_pre(source)
  pre = create_html_element  'pre'
  code = Element.new 'code', pre
  s = source

#   s  = s.gsub(/&/,'&amp;')
  s = Text.normalize(s)
  s  = s.gsub(/\&apos;/,'&#39;') # IE bug
  s  = s.gsub(/'/,'&#39;') # IE bug

  if get_setting(:code_show_spaces)
    # 187 = raquo
    # 160 = nbsp
    # 172 = not
    s.gsub!(/\t/,'&#187;'+'&#160;'*3)
    s.gsub!(/ /,'&#172;')
  end

  text = Text.new(s, respect_ws=true, parent=nil, raw=true )

  if lang = self.attributes[:lang]
    code.attributes['lang'] = lang
    code.attributes['class'] = lang
  end
  code << text
  pre
end

#to_html_definitionObject



910
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 910

def to_html_definition() children_to_html end

#to_html_definition_dataObject



912
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 912

def to_html_definition_data() add_ws wrap_as_element('dd') end

#to_html_definition_listObject

Definition lists ###



909
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 909

def to_html_definition_list() add_ws wrap_as_element('dl') end

#to_html_definition_termObject



911
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 911

def to_html_definition_term() add_ws wrap_as_element('dt') end

#to_html_document(context = {}) ⇒ Object

Render to a complete HTML document (returns a string)



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 85

def to_html_document(context={})
  indent = context[:indent] || -1
  ie_hack = context[:ie_hack] ||true
  doc = to_html_document_tree
  xml  = ""

  # REXML Bug? if indent!=-1 whitespace is not respected for 'pre' elements
  # containing code.
  doc.write(xml,indent,transitive=true,ie_hack);

  Xhtml11_mathml2_svg11 + xml
end

#to_html_document_treeObject

Render to a complete HTML document (returns a REXML document tree)



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
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 198

def to_html_document_tree
  doc = Document.new(nil,{:respect_whitespace =>:all})
# doc << XMLDecl.new

  root = Element.new('html', doc)
  root.add_namespace('http://www.w3.org/1999/xhtml')
  root.add_namespace('svg', "http://www.w3.org/2000/svg" )
  lang = self.attributes[:lang] || 'en'
  root.attributes['xml:lang'] = lang

  root << xml_newline
  head = Element.new 'head', root

    #<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
    me = Element.new 'meta', head
    me.attributes['http-equiv'] = 'Content-type'
#     me.attributes['content'] = 'text/html;charset=utf-8'
    me.attributes['content'] = 'application/xhtml+xml;charset=utf-8'

    METAS.each do |m|
      if value = self.attributes[m.to_sym]
        meta = Element.new 'meta', head
        meta.attributes['name'] = m
        meta.attributes['content'] = value.to_s
      end
    end


    self.attributes.each do |k,v|
      if k.to_s =~ /\Ameta-(.*)\Z/
        meta = Element.new 'meta', head
        meta.attributes['name'] = $1
        meta.attributes['content'] = v.to_s
      end
    end



    # Create title element
    doc_title = self.attributes[:title] || self.attributes[:subject] || ""
    title = Element.new 'title', head
      title << Text.new(doc_title)

    add_css_to(head)


  root << xml_newline

  body = Element.new 'body'

    children_to_html.each do |e|
      body << e
    end

    # render footnotes
    if @doc.footnotes_order.size > 0
      body << render_footnotes
    end

    # When we are rendering a whole document, we add a signature
    # at the bottom.
    if get_setting(:maruku_signature)
      body << maruku_html_signature
    end

  root << body

  doc
end

#to_html_email_addressObject



779
780
781
782
783
784
785
786
787
788
789
790
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 779

def to_html_email_address
  email = self.email
  a = create_html_element 'a'
    #a.attributes['href'] = Text.new("mailto:"+obfuscate(email),false,nil,true)
    #a.attributes.add Attribute.new('href',Text.new(
    #"mailto:"+obfuscate(email),false,nil,true))
    # Sorry, for the moment it doesn't work
    a.attributes['href'] = "mailto:#{email}"

    a << Text.new(obfuscate(email),false,nil,true)
  a
end

#to_html_emphasisObject



472
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 472

def to_html_emphasis;  wrap_as_element('em')               end

#to_html_entityObject



957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 957

def to_html_entity
  MaRuKu::Out::Latex.need_entity_table

  entity_name = self.entity_name

  if (e = MaRuKu::Out::Latex::ENTITY_TABLE[entity_name]) && e.html_num
    entity_name = e.html_num
  end

  # Fix for Internet Explorer
  if entity_name == 'apos'
    entity_name = 39
  end


  if entity_name.kind_of? Fixnum
#     Entity.new(entity_name)
    Text.new('&#%d;' % [entity_name],  false, nil, true)
  else
    Text.new('&%s;' % [entity_name],  false, nil, true)
  end
end

#to_html_footnote_referenceObject



876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 876

def to_html_footnote_reference
  id = self.footnote_id

  # save the order of used footnotes
  order = @doc.footnotes_order

  if order.include? id
    # footnote has already been used
    return []
  end

  if not @doc.footnotes[id]
    return []
   end

  # take next number
  order << id

  #num = order.size;
  num = order.index(id) + 1

  sup = Element.new 'sup'
  sup.attributes['id'] = "#{get_setting(:doc_prefix)}fnref:#{num}"
    a = Element.new 'a'
    a << Text.new(num.to_s)
    a.attributes['href'] = "\##{get_setting(:doc_prefix)}fn:#{num}"
    a.attributes['rel'] = 'footnote'
  sup << a

  sup
end

#to_html_head_cellObject



948
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 948

def to_html_head_cell; wrap_as_element('th') end

#to_html_headerObject



520
521
522
523
524
525
526
527
528
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 520

def to_html_header
  element_name = "h#{self.level}"
  h = wrap_as_element element_name

  if span = render_section_number
    h.insert_before(h.children.first, span)
  end
  add_ws h
end

#to_html_header_origObject



510
511
512
513
514
515
516
517
518
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 510

def to_html_header_orig
  element_name = "h#{self.level}"
  h = wrap_as_element element_name

  if span = render_section_number
    h.insert_before(h.children.first, span)
  end
  add_ws h
end

#to_html_hruleObject



347
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 347

def to_html_hrule; create_html_element 'hr' end

#to_html_im_imageObject



812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 812

def to_html_im_image
  if not url = self.url
    maruku_error "Image with no url: #{self.inspect}"
    tell_user "Could not create image with ref_id = #{id.inspect};"+
      " Using SPAN element as replacement."
    return wrap_as_element('span')
  end
  if url_resolver = (Thread.current['maruku_context'] || {})[:img_url_resolver]
    url = url_resolver.call(url)
  end

  title = self.title
  a =  create_html_element 'img'
  a.attributes['src'] = url.to_s
  a.attributes['alt'] = children_to_s
  return a
end


751
752
753
754
755
756
757
758
759
760
761
762
763
764
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 751

def to_html_im_link
  if url = self.url
    title = self.title
    a =  wrap_as_element 'a'
    a.attributes['href'] = url
     a.attributes['xhref'] = url # BUG ALERT: Strange behaviour of jquery's replaceWith related to href
    a.attributes['title'] = title if title
    return a
  else
    maruku_error"Could not find url in #{self.inspect}"
    tell_user "Not creating a link for ref_id = #{id.inspect}."
    return wrap_as_element('span')
  end
end

#to_html_imageObject

Images



794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 794

def to_html_image
  a =  create_html_element 'img'
  id = self.ref_id
  if ref = @doc.refs[id]
    url = ref[:url]
    title = ref[:title]
    a.attributes['src'] = url.to_s
    a.attributes['alt'] = children_to_s
  else
    maruku_error"Could not find id = #{id.inspect} for\n #{self.inspect}"
    tell_user "Could not create image with ref_id = #{id.inspect};"+
       " Using SPAN element as replacement."
      return wrap_as_element('span')
  end
   raise "IMAGE: #{url}"
  return a
end


721
722
723
724
725
726
727
728
729
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 721

def to_html_immediate_link
  a =  create_html_element 'a'
  url = self.url
  text = url.gsub(/^mailto:/,'') # don't show mailto
  a << Text.new(text)
  a.attributes['href'] = url
  add_class_to_link(a)
  a
end

#to_html_inline_codeObject



681
682
683
684
685
686
687
688
689
690
691
692
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 681

def to_html_inline_code;
  pre =  create_html_element 'code'
    source = self.raw_code
    pre << source2html(source)

    color = get_setting(:code_background_color)
    if color != Globals[:code_background_color]
      pre.attributes['style'] = "background-color: #{color};"+(pre.attributes['style']||"")
    end

  pre
end

#to_html_liObject



468
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 468

def to_html_li;        add_ws wrap_as_element('li')        end

#to_html_li_spanObject



469
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 469

def to_html_li_span;   add_ws wrap_as_element('li')        end

#to_html_linebreakObject



348
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 348

def to_html_linebreak; Element.new 'br' end


731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 731

def to_html_link
  a =  wrap_as_element 'a'
  id = self.ref_id

  if ref = @doc.refs[id]
    url = ref[:url]
    title = ref[:title]
    a.attributes['href'] = url if url
    a.attributes['title'] = title if title
  else
    maruku_error "Could not find ref_id = #{id.inspect} for #{self.inspect}\n"+
      "Available refs are #{@doc.refs.keys.inspect}"
    tell_user "Not creating a link for ref_id = #{id.inspect}."
    return wrap_as_element('span')
  end

#   add_class_to_link(a)
  return a
end

#to_html_olObject



467
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 467

def to_html_ol;        add_ws wrap_as_element('ol')        end

#to_html_paragraphObject



457
458
459
460
461
462
463
464
465
466
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 457

def to_html_paragraph
  #add_ws wrap_as_element('p')
  dt = Element.new 'p'
   dt.attributes['class'] = 'drop-target'
   dt.attributes['line_no'] = Thread.current['line_no']
   dt.attributes['delegate'] = 'plan'  # should most likely go into the js column handler
  p = wrap_as_element('p')
  p.attributes['class'] = 'content'
  [Text.new("\n"), p, dt, Text.new("\n")]
end

#to_html_quoteObject



470
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 470

def to_html_quote;     add_ws wrap_as_element('blockquote')  end

#to_html_raw_htmlObject

Attribute: filter_html Scope: document

If true, raw HTML is discarded from the output.



838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 838

def to_html_raw_html
  return [] if get_setting(:filter_html)

  raw_html = self.raw_html
  if rexml_doc = @parsed_html
    root = rexml_doc.root
    if root.nil?
      s = "Bug in REXML: root() of Document is nil: \n#{rexml_doc.inspect}\n"+
      "Raw HTML:\n#{raw_html.inspect}"
      maruku_error s
      tell_user 'The REXML version you have has a bug, omitting HTML'
      div = Element.new 'div'
      #div << Text.new(s)
      return div
    end

    # copies the @children array (FIXME is it deep?)
    elements =  root.to_a
    return elements
  else # invalid
    # Creates red box with offending HTML
    tell_user "Wrapping bad html in a PRE with class 'markdown-html-error'\n"+
      add_tabs(raw_html,1,'|')
    pre = Element.new('pre')
    pre.attributes['style'] = 'border: solid 3px red; background-color: pink'
    pre.attributes['class'] = 'markdown-html-error'
    pre << Text.new("REXML could not parse this XML/HTML: \n#{raw_html}", true)
    return pre
  end
end

#to_html_ref_definitionObject



1051
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 1051

def to_html_ref_definition; [] end

#to_html_strongObject



471
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 471

def to_html_strong;    wrap_as_element('strong')           end

#to_html_tableObject

FIXME: Ugly code



915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 915

def to_html_table
  align = self.align
  num_columns = align.size

  head = @children.slice(0, num_columns)
  rows = []
  i = num_columns
  while i<@children.size
    rows << @children.slice(i, num_columns)
    i += num_columns
  end

  table = create_html_element 'table'
    thead = Element.new 'thead'
    tr = Element.new 'tr'
      array_to_html(head).each do |x| tr<<x end
    thead << tr
    table << thead

    tbody = Element.new 'tbody'
    rows.each do |row|
      tr = Element.new 'tr'
        array_to_html(row).each_with_index do |x,i|
          x.attributes['style'] ="text-align: #{align[i].to_s};"
          tr<<x
        end

      tbody << tr << Text.new("\n")
    end
    table << tbody
  table
end

#to_html_treeObject

Render to an HTML fragment (returns a REXML document tree)



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 163

def to_html_tree
  div = Element.new 'div'
    div.attributes['class'] = 'maruku_wrapper_div'
      children_to_html.each do |e|
            div << e
      end

      # render footnotes
      if @doc.footnotes_order.size > 0
            div << render_footnotes
      end

   doc = Document.new(nil,{:respect_whitespace =>:all})
   doc << div
end

#to_html_ulObject



446
447
448
449
450
451
452
453
454
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 446

def to_html_ul
  if @attributes[:toc]
    # render toc
    html_toc = @doc.toc.to_html
    return html_toc
  else
    add_ws  wrap_as_element('ul')
  end
end

#to_html_xml_instrObject



980
981
982
983
984
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 980

def to_html_xml_instr
  target = self.target || ''
  code = self.code || ''
  REXML::Instruction.new(target, code)
end

#to_latex_ref_definitionObject



1052
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 1052

def to_latex_ref_definition; [] end

#wrap_as_element(name, attributes_to_copy = []) ⇒ Object

renders children as html and wraps into an element of given name

Sets ‘id’ if meta is set



353
354
355
356
357
358
359
360
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 353

def wrap_as_element(name, attributes_to_copy=[])
  m = create_html_element(name, attributes_to_copy)
    children_to_html.each do |e| m << e; end

#     m << Comment.new( "{"+self.al.to_md+"}") if not self.al.empty?
#     m << Comment.new( @attributes.inspect) if not @attributes.empty?
  m
end

#xml_newlineObject



120
# File 'lib/omf-web/widget/text/maruku/output/to_html.rb', line 120

def xml_newline() Text.new("\n") end