Module: MaRuKu::Out::HTML
- Includes:
- REXML
- Included in:
- MDElement
- Defined in:
- lib/maruku.rb,
lib/maruku/ext/math/to_html.rb,
lib/maruku/ext/math/mathml_engines/none.rb,
lib/maruku/ext/math/mathml_engines/ritex.rb,
lib/maruku/ext/math/mathml_engines/blahtex.rb,
lib/maruku/ext/math/mathml_engines/itex2mml.rb,
lib/maruku/output/to_html.rb
Overview
Functions for exporting to HTML.
Defined Under Namespace
Classes: PNG
Constant Summary collapse
- BlahtexCache =
PStore.new("blahtex_cache.pstore")
- StandardAttributes =
Attribute: style Scope: element Output: HTML
It is copied as a standard HTML attribute.
[:id, :style, :class]
Instance Method Summary collapse
- #add_class_to(el, cl) ⇒ Object
- #add_class_to_link(a) ⇒ Object
- #add_ws(e) ⇒ Object
- #adjust_png(png, use_depth) ⇒ Object
- #array_to_html(array) ⇒ Object
-
#children_to_html ⇒ Object
Convert each child to html.
- #convert_to_mathml_blahtex(kind, tex) ⇒ Object
- #convert_to_mathml_itex2mml(kind, tex) ⇒ Object
- #convert_to_mathml_none(kind, tex) ⇒ Object
- #convert_to_mathml_ritex(kind, tex) ⇒ Object
- #convert_to_png_blahtex(kind, tex) ⇒ Object
- #convert_to_png_none(kind, tex) ⇒ Object
- #create_html_element(name, attributes_to_copy = []) ⇒ Object
-
#day_suffix(day) ⇒ Object
returns “st”,“nd”,“rd” or “th” as appropriate.
- #maruku_html_signature ⇒ Object
-
#nice_date ⇒ Object
formats a nice date.
-
#obfuscate(s) ⇒ Object
Email address.
- #pixels_per_ex ⇒ Object
- #render_footnotes ⇒ Object
-
#render_mathml(kind, tex) ⇒ Object
Creates an xml Mathml document of self.math.
-
#render_png(kind, tex) ⇒ Object
Creates an xml Mathml document of self.math.
-
#render_section_number ⇒ Object
nil if not applicable, else SPAN element.
-
#section_number ⇒ Object
nil if not applicable, else string.
- #source2html(source) ⇒ Object
-
#to_html(context = {}) ⇒ Object
Render as an HTML fragment (no head, just the content of BODY).
- #to_html_abbr ⇒ Object
- #to_html_cell ⇒ Object
-
#to_html_code ⇒ Object
Attribute: html_use_syntax Scope: global, document, element Output: HTML Summary: Enables the use of the ‘syntax` package.
- #to_html_code_using_pre(source) ⇒ Object
- #to_html_definition ⇒ Object
- #to_html_definition_data ⇒ Object
-
#to_html_definition_list ⇒ Object
Definition lists ###.
- #to_html_definition_term ⇒ Object
-
#to_html_document(context = {}) ⇒ Object
Render to a complete HTML document (returns a string).
-
#to_html_document_tree ⇒ Object
Render to a complete HTML document (returns a REXML document tree).
- #to_html_email_address ⇒ Object
- #to_html_emphasis ⇒ Object
- #to_html_entity ⇒ Object
- #to_html_eqref ⇒ Object
- #to_html_equation ⇒ Object
- #to_html_footnote_reference ⇒ Object
- #to_html_head_cell ⇒ Object
- #to_html_header ⇒ Object
- #to_html_hrule ⇒ Object
- #to_html_im_image ⇒ Object
- #to_html_im_link ⇒ Object
-
#to_html_image ⇒ Object
Images.
- #to_html_immediate_link ⇒ Object
- #to_html_inline_code ⇒ Object
- #to_html_inline_math ⇒ Object
- #to_html_li ⇒ Object
- #to_html_li_span ⇒ Object
- #to_html_linebreak ⇒ Object
- #to_html_link ⇒ Object
- #to_html_ol ⇒ Object
- #to_html_paragraph ⇒ Object
- #to_html_quote ⇒ Object
- #to_html_raw_html ⇒ Object
- #to_html_ref_definition ⇒ Object
- #to_html_strong ⇒ Object
-
#to_html_table ⇒ Object
FIXME: Ugly code.
- #to_html_ul ⇒ Object
- #to_html_xml_instr ⇒ Object
- #to_latex_ref_definition ⇒ Object
-
#wrap_as_element(name, attributes_to_copy = []) ⇒ Object
renders children as html and wraps into an element of given name.
- #xml_newline ⇒ Object
Instance Method Details
#add_class_to(el, cl) ⇒ Object
556 557 558 559 560 561 562 563 |
# File 'lib/maruku/output/to_html.rb', line 556 def add_class_to(el, cl) el.attributes['class'] = if already = el.attributes['class'] already + " " + cl else cl end end |
#add_class_to_link(a) ⇒ Object
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
# File 'lib/maruku/output/to_html.rb', line 565 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_ws(e) ⇒ Object
627 628 629 |
# File 'lib/maruku/output/to_html.rb', line 627 def add_ws(e) [Text.new("\n"), e, Text.new("\n")] end |
#adjust_png(png, use_depth) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/maruku/ext/math/to_html.rb', line 79 def adjust_png(png, use_depth) src = png.src height_in_px = png.height depth_in_px = png.depth height_in_ex = height_in_px / pixels_per_ex depth_in_ex = depth_in_px / pixels_per_ex total_height_in_ex = height_in_ex + depth_in_ex style = "" style += "vertical-align: -#{depth_in_ex}ex;" if use_depth style += "height: #{total_height_in_ex}ex;" img = Element.new 'img' img.attributes['src'] = src img.attributes['style'] = style img.attributes['alt'] = "equation" img end |
#array_to_html(array) ⇒ Object
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 |
# File 'lib/maruku/output/to_html.rb', line 829 def array_to_html(array) e = [] 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 h = c.send(method) if h.nil? raise "Nil html created by method #{method}:\n#{h.inspect}\n"+ " for object #{c.inspect[0,300]}" end if h.kind_of?Array e = e + h #h.each do |hh| e << hh end else e << h end end e end |
#children_to_html ⇒ Object
Convert each child to html
825 826 827 |
# File 'lib/maruku/output/to_html.rb', line 825 def children_to_html array_to_html(@children) end |
#convert_to_mathml_blahtex(kind, tex) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/maruku/ext/math/mathml_engines/blahtex.rb', line 68 def convert_to_mathml_blahtex(kind, tex) begin BlahtexCache.transaction do if BlahtexCache[tex].nil? tmp_in = Tempfile.new('maruku_blahtex') f = tmp_in.open f.write tex f.close tmp_out = Tempfile.new('maruku_blahtex') = "--mathml" cmd = "blahtex #{} < #{tmp_in.path} > #{tmp_out.path}" $stderr.puts "$ #{cmd}" system cmd tmp_in.delete result = nil File.open(tmp_out.path) do |f| result=f.read end puts result BlahtexCache[tex] = result end blahtex = BlahtexCache[tex] doc = Document.new(blahtex, {:respect_whitespace =>:all}) mathml = doc.root.elements['mathml'] if not mathml maruku_error "Blahtex error: \n#{doc}" return nil else return mathml end end rescue Exception => e maruku_error "Error: #{e}" end nil end |
#convert_to_mathml_itex2mml(kind, tex) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/maruku/ext/math/mathml_engines/itex2mml.rb', line 4 def convert_to_mathml_itex2mml(kind, tex) begin if not $itex2mml_parser require 'itextomml' $itex2mml_parser = Itex2MML::Parser.new end itex_method = {:equation=>:block_filter,:inline=>:inline_filter} mathml = $itex2mml_parser.send(itex_method[kind], tex) doc = Document.new(mathml, {:respect_whitespace =>:all}).root return doc rescue LoadError => e maruku_error "Could not load package 'itex2mml'.\n"+ "Please install it." rescue REXML::ParseException => e maruku_error "Invalid MathML TeX: \n#{add_tabs(tex,1,'tex>')}"+ "\n\n #{e.inspect}" rescue maruku_error "Could not produce MathML TeX: \n#{tex}"+ "\n\n #{e.inspect}" end nil end |
#convert_to_mathml_none(kind, tex) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/maruku/ext/math/mathml_engines/none.rb', line 3 def convert_to_mathml_none(kind, tex) # You can: either return a REXML::Element # return Element.new 'div' # or return an empty array on error # return [] # or have a string parsed by REXML: tex = tex.gsub('&','&') mathml = "<code>#{tex}</code>" return Document.new(mathml).root end |
#convert_to_mathml_ritex(kind, tex) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/maruku/ext/math/mathml_engines/ritex.rb', line 3 def convert_to_mathml_ritex(kind, tex) begin if not $ritex_parser require 'ritex' $ritex_parser = Ritex::Parser.new end mathml = $ritex_parser.parse(tex.strip) doc = Document.new(mathml, {:respect_whitespace =>:all}).root return doc rescue LoadError => e maruku_error "Could not load package 'ritex'.\n"+ "Please install it using:\n"+ " $ gem install ritex\n\n"+e.inspect rescue Racc::ParseError => e maruku_error "Could not parse TeX: \n#{tex}"+ "\n\n #{e.inspect}" end nil end |
#convert_to_png_blahtex(kind, tex) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/maruku/ext/math/mathml_engines/blahtex.rb', line 11 def convert_to_png_blahtex(kind, tex) begin FileUtils::mkdir_p MaRuKu::Globals[:html_png_dir] # first, we check whether this image has already been processed md5sum = Digest::MD5.hexdigest(tex+" params: ") result_file = File.join(MaRuKu::Globals[:html_png_dir], md5sum+".txt") if not File.exists?(result_file) tmp_in = Tempfile.new('maruku_blahtex') f = tmp_in.open f.write tex f.close resolution = get_setting(:html_png_resolution) = "--png --use-preview-package --shell-dvipng 'dvipng -D #{resolution}' " += ("--png-directory '%s'" % MaRuKu::Globals[:html_png_dir]) cmd = "blahtex #{} < #{tmp_in.path} > #{result_file}" $stderr.puts "$ #{cmd}" system cmd tmp_in.delete end result = nil f = File.open(result_file) result = f.read f.close doc = Document.new(result, {:respect_whitespace =>:all}) png = doc.root.elements[1] if png.name != 'png' maruku_error "Blahtex error: \n#{doc}" return nil end depth = png.elements['depth'] || (raise "No depth element in:\n #{doc}") height = png.elements['height'] || (raise "No height element in:\n #{doc}") md5 = png.elements['md5'] || (raise "No md5 element in:\n #{doc}") depth = depth.text.to_f height = height.text.to_f # XXX check != 0 md5 = md5.text dir_url = MaRuKu::Globals[:html_png_url] return PNG.new("#{dir_url}#{md5}.png", depth, height) rescue Exception => e maruku_error "Error: #{e}" end nil end |
#convert_to_png_none(kind, tex) ⇒ Object
14 15 16 |
# File 'lib/maruku/ext/math/mathml_engines/none.rb', line 14 def convert_to_png_none(kind, tex) return nil end |
#create_html_element(name, attributes_to_copy = []) ⇒ Object
320 321 322 323 324 325 326 |
# File 'lib/maruku/output/to_html.rb', line 320 def create_html_element(name, attributes_to_copy=[]) m = Element.new name (StandardAttributes+attributes_to_copy).each do |a| if v = @attributes[a] then m.attributes[a.to_s] = v.to_s end end m end |
#day_suffix(day) ⇒ Object
returns “st”,“nd”,“rd” or “th” as appropriate
213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/maruku/output/to_html.rb', line 213 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_signature ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/maruku/output/to_html.rb', line 235 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_date ⇒ Object
formats a nice date
227 228 229 230 231 232 233 |
# File 'lib/maruku/output/to_html.rb', line 227 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
632 633 634 635 636 637 638 |
# File 'lib/maruku/output/to_html.rb', line 632 def obfuscate(s) res = '' s.each_byte do |char| res += "&#%03d;" % char end res end |
#pixels_per_ex ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/maruku/ext/math/to_html.rb', line 71 def pixels_per_ex if not $pixels_per_ex x = render_png(:inline, "x") $pixels_per_ex = x.height # + x.depth end $pixels_per_ex end |
#render_footnotes ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/maruku/output/to_html.rb', line 250 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'] = "fn:#{num}" a = Element.new 'a' a.attributes['href'] = "#fnref:#{num}" a.attributes['rev'] = 'footnote' a<< Text.new('↩', true, nil, true) li.insert_after(li.children.last, a) ol << li else maruku_error"Could not find footnote '#{fid}'" end end div << ol div end |
#render_mathml(kind, tex) ⇒ Object
Creates an xml Mathml document of self.math
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/maruku/ext/math/to_html.rb', line 47 def render_mathml(kind, tex) engine = get_setting(:html_math_engine) method = "convert_to_mathml_#{engine}".to_sym if self.respond_to? method mathml = self.send(method, kind, tex) return mathml || convert_to_mathml_none(kind, tex) else puts "A method called #{method} should be defined." return convert_to_mathml_none(kind, tex) end end |
#render_png(kind, tex) ⇒ Object
Creates an xml Mathml document of self.math
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/maruku/ext/math/to_html.rb', line 60 def render_png(kind, tex) engine = get_setting(:html_png_engine) method = "convert_to_png_#{engine}".to_sym if self.respond_to? method return self.send(method, kind, tex) else puts "A method called #{method} should be defined." return nil end end |
#render_section_number ⇒ Object
nil if not applicable, else SPAN element
372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/maruku/output/to_html.rb', line 372 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_number ⇒ Object
nil if not applicable, else string
360 361 362 363 364 365 366 367 368 369 |
# File 'lib/maruku/output/to_html.rb', line 360 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
394 395 396 397 398 399 400 |
# File 'lib/maruku/output/to_html.rb', line 394 def source2html(source) source = source.gsub(/&/,'&') source = Text.normalize(source) source = source.gsub(/\'/,''') # IE bug source = source.gsub(/'/,''') # 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)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/maruku/output/to_html.rb', line 43 def to_html(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 ="" div.write_children(xml,indent,transitive=true,ie_hack) xml end |
#to_html_abbr ⇒ Object
718 719 720 721 722 723 |
# File 'lib/maruku/output/to_html.rb', line 718 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_cell ⇒ Object
787 788 789 790 791 792 793 |
# File 'lib/maruku/output/to_html.rb', line 787 def to_html_cell if @attributes[:scope] wrap_as_element('th', [:scope]) else wrap_as_element('td') end end |
#to_html_code ⇒ Object
Attribute: html_use_syntax Scope: global, document, element Output: HTML Summary: Enables the use of the ‘syntax` package. Related: lang, code_lang Default: <?mrk md_code(Globals.to_s) ?>
If true, the ‘syntax` package is used. It supports the `ruby` and `xml` languages. Remember to set the `lang` attribute of the code block.
Examples:
require ‘maruku’ html_use_syntax=true
and <div style=“text-align:center”>Div</div> html_use_syntax=true
produces:
require ‘maruku’ html_use_syntax=true
and
<div style=“text-align:center”>Div</div> html_use_syntax=true
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
# File 'lib/maruku/output/to_html.rb', line 435 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(/\'/,''') # IE bug html = html.gsub(/'/,''') # IE bug # html = html.gsub(/&/,'&') 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.}\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
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
# File 'lib/maruku/output/to_html.rb', line 515 def to_html_code_using_pre(source) pre = create_html_element 'pre' code = Element.new 'code', pre s = source s = s.gsub(/&/,'&') s = Text.normalize(s) s = s.gsub(/\'/,''') # IE bug s = s.gsub(/'/,''') # IE bug if get_setting(:code_show_spaces) # 187 = raquo # 160 = nbsp # 172 = not s.gsub!(/\t/,'»'+' '*3) s.gsub!(/ /,'¬') 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_definition ⇒ Object
748 |
# File 'lib/maruku/output/to_html.rb', line 748 def to_html_definition() children_to_html end |
#to_html_definition_data ⇒ Object
750 |
# File 'lib/maruku/output/to_html.rb', line 750 def to_html_definition_data() add_ws wrap_as_element('dd') end |
#to_html_definition_list ⇒ Object
Definition lists ###
747 |
# File 'lib/maruku/output/to_html.rb', line 747 def to_html_definition_list() add_ws wrap_as_element('dl') end |
#to_html_definition_term ⇒ Object
749 |
# File 'lib/maruku/output/to_html.rb', line 749 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)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/maruku/output/to_html.rb', line 68 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); 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"> ' xhtml11_mathml2_svg11 + xml end |
#to_html_document_tree ⇒ Object
Render to a complete HTML document (returns a REXML document tree)
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/maruku/output/to_html.rb', line 149 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' # Create title element doc_title = self.attributes[:title] || self.attributes[:subject] || "" title = Element.new 'title', head title << Text.new(doc_title) 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 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_address ⇒ Object
640 641 642 643 644 645 646 647 648 649 650 651 |
# File 'lib/maruku/output/to_html.rb', line 640 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_emphasis ⇒ Object
346 |
# File 'lib/maruku/output/to_html.rb', line 346 def to_html_emphasis; wrap_as_element('em') end |
#to_html_entity ⇒ Object
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 |
# File 'lib/maruku/output/to_html.rb', line 795 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]) end end |
#to_html_eqref ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/maruku/ext/math/to_html.rb', line 153 def to_html_eqref if eq = self.doc.eqid2eq[self.eqid] num = eq.num a = Element.new 'a' a.attributes['class'] = 'maruku-eqref' a.attributes['href'] = "#eq:#{self.eqid}" a << Text.new("(#{num})") a else maruku_error "Cannot find equation #{self.eqid.inspect}" Text.new "(eq:#{self.eqid})" end end |
#to_html_equation ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/maruku/ext/math/to_html.rb', line 118 def to_html_equation mathml = render_mathml(:equation, self.math) png = render_png(:equation, self.math) div = create_html_element 'div' add_class_to(div, 'maruku-equation') if self.label # then numerate span = Element.new 'span' span.attributes['class'] = 'maruku-eq-number' num = self.num span << Text.new("(#{num})") div << span div.attributes['id'] = "eq:#{self.label}" end if mathml add_class_to(mathml, 'maruku-mathml') div << mathml end if png img = adjust_png(png, use_depth=false) add_class_to(img, 'maruku-png') div << img end source_div = Element.new 'div' add_class_to(source_div, 'maruku-eq-tex') code = convert_to_mathml_none(:equation, self.math) code.attributes['style'] = 'display: none' source_div << code div << source_div div end |
#to_html_footnote_reference ⇒ Object
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 |
# File 'lib/maruku/output/to_html.rb', line 725 def to_html_footnote_reference id = self.footnote_id # save the order of used footnotes order = @doc.footnotes_order # take next number order << id num = order.size; sup = Element.new 'sup' sup.attributes['id'] = "fnref:#{num}" a = Element.new 'a' a << Text.new(num.to_s) a.attributes['href'] = "\#fn:#{num}" a.attributes['rel'] = 'footnote' sup << a sup end |
#to_html_head_cell ⇒ Object
786 |
# File 'lib/maruku/output/to_html.rb', line 786 def to_html_head_cell; wrap_as_element('th') end |
#to_html_header ⇒ Object
384 385 386 387 388 389 390 391 392 |
# File 'lib/maruku/output/to_html.rb', line 384 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_hrule ⇒ Object
276 |
# File 'lib/maruku/output/to_html.rb', line 276 def to_html_hrule; create_html_element 'hr' end |
#to_html_im_image ⇒ Object
675 676 677 678 679 680 681 682 683 684 685 686 687 |
# File 'lib/maruku/output/to_html.rb', line 675 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 title = self.title a = create_html_element 'img' a.attributes['src'] = url a.attributes['alt'] = title.to_s return a end |
#to_html_im_link ⇒ Object
613 614 615 616 617 618 619 620 621 622 623 624 625 |
# File 'lib/maruku/output/to_html.rb', line 613 def to_html_im_link if url = self.url title = self.title a = wrap_as_element 'a' a.attributes['href'] = url 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_image ⇒ Object
Images
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 |
# File 'lib/maruku/output/to_html.rb', line 655 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'] = title.to_s [:title, :class, :style].each do |s| a.attributes[s.to_s] = ref[s] if ref[s] end 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 return a end |
#to_html_immediate_link ⇒ Object
583 584 585 586 587 588 589 590 591 |
# File 'lib/maruku/output/to_html.rb', line 583 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_code ⇒ Object
543 544 545 546 547 548 549 550 551 552 553 554 |
# File 'lib/maruku/output/to_html.rb', line 543 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};" end pre end |
#to_html_inline_math ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/maruku/ext/math/to_html.rb', line 97 def to_html_inline_math mathml = render_mathml(:inline, self.math) png = render_png(:inline, self.math) span = create_html_element 'span' add_class_to(span, 'maruku-inline') if mathml add_class_to(mathml, 'maruku-mathml') span << mathml end if png img = adjust_png(png, use_depth=true) add_class_to(img, 'maruku-png') span << img end span end |
#to_html_li ⇒ Object
342 |
# File 'lib/maruku/output/to_html.rb', line 342 def to_html_li; add_ws wrap_as_element('li') end |
#to_html_li_span ⇒ Object
343 |
# File 'lib/maruku/output/to_html.rb', line 343 def to_html_li_span; add_ws wrap_as_element('li') end |
#to_html_linebreak ⇒ Object
277 |
# File 'lib/maruku/output/to_html.rb', line 277 def to_html_linebreak; Element.new 'br' end |
#to_html_link ⇒ Object
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
# File 'lib/maruku/output/to_html.rb', line 593 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_ol ⇒ Object
341 |
# File 'lib/maruku/output/to_html.rb', line 341 def to_html_ol; add_ws wrap_as_element('ol') end |
#to_html_paragraph ⇒ Object
340 |
# File 'lib/maruku/output/to_html.rb', line 340 def to_html_paragraph; add_ws wrap_as_element('p') end |
#to_html_quote ⇒ Object
344 |
# File 'lib/maruku/output/to_html.rb', line 344 def to_html_quote; add_ws wrap_as_element('blockquote') end |
#to_html_raw_html ⇒ Object
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 |
# File 'lib/maruku/output/to_html.rb', line 689 def to_html_raw_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("HTML parse error: \n#{raw_html}", true) return pre end end |
#to_html_ref_definition ⇒ Object
856 |
# File 'lib/maruku/output/to_html.rb', line 856 def to_html_ref_definition; [] end |
#to_html_strong ⇒ Object
345 |
# File 'lib/maruku/output/to_html.rb', line 345 def to_html_strong; wrap_as_element('strong') end |
#to_html_table ⇒ Object
FIXME: Ugly code
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 |
# File 'lib/maruku/output/to_html.rb', line 753 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_ul ⇒ Object
329 330 331 332 333 334 335 336 337 |
# File 'lib/maruku/output/to_html.rb', line 329 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_instr ⇒ Object
818 819 820 821 822 |
# File 'lib/maruku/output/to_html.rb', line 818 def to_html_xml_instr target = self.target || '' code = self.code || '' REXML::Instruction.new(target, code) end |
#to_latex_ref_definition ⇒ Object
857 |
# File 'lib/maruku/output/to_html.rb', line 857 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
282 283 284 285 286 287 288 289 |
# File 'lib/maruku/output/to_html.rb', line 282 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_newline ⇒ Object
100 |
# File 'lib/maruku/output/to_html.rb', line 100 def xml_newline() Text.new("\n") end |