Included Modules

MaRuKu::Out::HTML

Functions for exporting to HTML.

Constants

HTML4Attributes

Attribute: id Scope: element Output: LaTeX, HTML

It is copied as a standard HTML attribute.

Moreover, it used as a label name for hyperlinks in both HTML and in PDF.

Attribute: class Scope: element Output: HTML

It is copied as a standard HTML attribute.

Attribute: style Scope: element Output: HTML

It is copied as a standard HTML attribute.

METAS

Attribute: css Scope: document Output: HTML Summary: Activates CSS stylesheets for HTML.

`css` should be a space-separated list of urls.

Example:

CSS: style.css math.css
PNG
Xhtml10strict
Xhtml11_mathml2_svg11
Xhtml11strict_mathml2

Public Instance Methods

add_class_to(el, cl) click to toggle source
# File lib/maruku/output/to_html.rb, line 668
def add_class_to(el, cl)
        el.attributes['class'] = 
        if already = el.attributes['class']
                already + " " + cl
        else
                cl
        end
end
add_css_to(head) click to toggle source
# File lib/maruku/output/to_html.rb, line 264
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) click to toggle source
# File lib/maruku/output/to_html.rb, line 739
def add_ws(e)
        [Text.new("\n"), e, Text.new("\n")]
end
adjust_png(png, use_depth) click to toggle source
# 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'] = "$#{self.math.strip}$"
        img
end
array_to_html(array) click to toggle source
# File lib/maruku/output/to_html.rb, line 959
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() click to toggle source

Convert each child to html

# File lib/maruku/output/to_html.rb, line 955
def children_to_html
        array_to_html(@children)
end
convert_to_mathml_blahtex(kind, tex) click to toggle source
# File lib/maruku/ext/math/mathml_engines/blahtex.rb, line 65
    def convert_to_mathml_blahtex(kind, tex)
@@BlahtexCache = PStore.new(get_setting(:latex_cache_file))

            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')
    
                                    options = "--mathml"
                                    cmd = "blahtex #{options} < #{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) click to toggle source
# 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."                    unless $already_warned_itex2mml 
                $already_warned_itex2mml = true
        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) click to toggle source
# 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('&','&amp;')
        mathml = "<code>#{tex}</code>"
        return Document.new(mathml).root
end
convert_to_mathml_ritex(kind, tex) click to toggle source
# 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) click to toggle source
# File lib/maruku/ext/math/mathml_engines/blahtex.rb, line 11
  def convert_to_png_blahtex(kind, tex)
          begin
                  FileUtils::mkdir_p get_setting(:html_png_dir)

                  # first, we check whether this image has already been processed
                  md5sum = Digest::MD5.hexdigest(tex+" params: ")
                  result_file = File.join(get_setting(: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)

                          options = "--png --use-preview-package --shell-dvipng 'dvipng -D #{resolution}' "
                          options += "--displaymath " if kind == :equation
                          options += ("--temp-directory '%s' " % get_setting(:html_png_dir))
                          options += ("--png-directory '%s'" % get_setting(:html_png_dir))

                          cmd = "blahtex #{options} < #{tmp_in.path} > #{result_file}"
                          #$stderr.puts "$ #{cmd}"
  system cmd
                          tmp_in.delete
                  end
                  
result = File.read(result_file)
if result.nil? || result.empty?
  raise "Blahtex error: empty output"
end

                  doc = Document.new(result, {:respect_whitespace =>:all})
                  png = doc.root.elements[1]
                  if png.name != 'png'
                          raise "Blahtex error: \n#{doc}"
                  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 = get_setting(: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) click to toggle source
# 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=[]) click to toggle source
# File lib/maruku/output/to_html.rb, line 425
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) click to toggle source

returns "st","nd","rd" or "th" as appropriate

# File lib/maruku/output/to_html.rb, line 279
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() click to toggle source
# File lib/maruku/output/to_html.rb, line 301
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() click to toggle source

formats a nice date

# File lib/maruku/output/to_html.rb, line 293
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) click to toggle source

Email address

# File lib/maruku/output/to_html.rb, line 744
def obfuscate(s)
        res = ''
        s.each_byte do |char|
                res +=  "&#%03d;" % char
        end
        res
end
pixels_per_ex() click to toggle source
# 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() click to toggle source
# File lib/maruku/output/to_html.rb, line 316
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_mathml(kind, tex) click to toggle source

Creates an xml Mathml document of self.math

# 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) click to toggle source

Creates an xml Mathml document of self.math

# 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() click to toggle source

nil if not applicable, else SPAN element

# File lib/maruku/output/to_html.rb, line 483
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() click to toggle source

Attribute: use_numbered_headers Scope: document Summary: Activates the numbering of headers.

If `true`, section headers will be numbered.

In LaTeX export, the numbering of headers is managed by Maruku, to have the same results in both HTML and LaTeX.

# nil if not applicable, else string

# File lib/maruku/output/to_html.rb, line 471
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) click to toggle source
# File lib/maruku/output/to_html.rb, line 505
        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={}) click to toggle source

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

# File lib/maruku/output/to_html.rb, line 45
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 =""

        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_abbr() click to toggle source
# File lib/maruku/output/to_html.rb, line 837
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() click to toggle source
# File lib/maruku/output/to_html.rb, line 917
def to_html_cell
        if @attributes[:scope]
                wrap_as_element('th', [:scope])
        else
                wrap_as_element('td')
        end
end
to_html_code() click to toggle source
# File lib/maruku/output/to_html.rb, line 547
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) click to toggle source

Attribute: code_background_color Scope: global, document, element Summary: Background color for code blocks.

The format is either a named color (`green`, `red`) or a CSS color of the form `ff00ff`.

  • for **HTML output**, the value is put straight in the `background-color` CSS property of the block.

  • for **LaTeX output**, if it is a named color, it must be a color accepted by the LaTeX `color` packages. If it is of the form `ff00ff`, Maruku defines a color using the `color[rgb]{r,g,b}` macro.

    For example, for `#0000ff`, the macro is called as: `color[rgb]{0,0,1}`.

# File lib/maruku/output/to_html.rb, line 627
        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_definition() click to toggle source
# File lib/maruku/output/to_html.rb, line 878
def to_html_definition() children_to_html end
to_html_definition_data() click to toggle source
# File lib/maruku/output/to_html.rb, line 880
def to_html_definition_data() add_ws wrap_as_element('dd') end
to_html_definition_list() click to toggle source

Definition lists ###

# File lib/maruku/output/to_html.rb, line 877
def to_html_definition_list() add_ws wrap_as_element('dl') end
to_html_definition_term() click to toggle source
# File lib/maruku/output/to_html.rb, line 879
def to_html_definition_term() add_ws wrap_as_element('dt') end
to_html_div() click to toggle source
# File lib/maruku/ext/div.rb, line 129
def to_html_div
        add_ws  wrap_as_element('div')
end
to_html_divref() click to toggle source
# File lib/maruku/ext/math/to_html.rb, line 167
def to_html_divref
        ref= nil
        self.doc.refid2ref.each_value { |h|
                ref = h[self.refid] if h.has_key?(self.refid)                        
        }
        if ref
                num = ref.num
                a = Element.new 'a'
                a.attributes['class'] = 'maruku-ref'
                a.attributes['href'] = "#" + self.refid
                a << Text.new(num.to_s)
                a
        else
                maruku_error "Cannot find div #{self.refid.inspect}"
                Text.new "\\ref{#{self.refid}}"
        end
end
to_html_document(context={}) click to toggle source

Render to a complete HTML document (returns a string)

# File lib/maruku/output/to_html.rb, line 84
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_tree() click to toggle source

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

# File lib/maruku/output/to_html.rb, line 194
        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_address() click to toggle source
# File lib/maruku/output/to_html.rb, line 752
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() click to toggle source
# File lib/maruku/output/to_html.rb, line 457
def to_html_emphasis;  wrap_as_element('em')               end
to_html_entity() click to toggle source
# File lib/maruku/output/to_html.rb, line 925
        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_eqref() click to toggle source
# 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() click to toggle source
# File lib/maruku/ext/math/to_html.rb, line 118
def to_html_equation
        mathml  = get_setting(:html_math_output_mathml) && render_mathml(:equation, self.math)
        png     = get_setting(:html_math_output_png)    && render_png(:equation, self.math)
        
        div = create_html_element 'div'
        add_class_to(div, 'maruku-equation')
                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_span = Element.new 'span'
                add_class_to(source_span, 'maruku-eq-tex')
                code = convert_to_mathml_none(:equation, self.math.strip)    
                code.attributes['style'] = 'display: none'
                source_span << code
                div << source_span

                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
        div
end
to_html_footnote_reference() click to toggle source
# File lib/maruku/output/to_html.rb, line 844
    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_cell() click to toggle source
# File lib/maruku/output/to_html.rb, line 916
def to_html_head_cell; wrap_as_element('th') end
to_html_header() click to toggle source
# File lib/maruku/output/to_html.rb, line 495
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() click to toggle source
# File lib/maruku/output/to_html.rb, line 343
def to_html_hrule; create_html_element 'hr' end
to_html_im_image() click to toggle source
# File lib/maruku/output/to_html.rb, line 784
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.to_s
                a.attributes['alt'] = children_to_s 
        return a
end
to_html_image() click to toggle source

Images

# File lib/maruku/output/to_html.rb, line 767
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
        return a
end
to_html_inline_code() click to toggle source
# File lib/maruku/output/to_html.rb, line 655
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_inline_math() click to toggle source
# File lib/maruku/ext/math/to_html.rb, line 97
def to_html_inline_math
        mathml  = get_setting(:html_math_output_mathml) && render_mathml(:inline, self.math)
        png = get_setting(:html_math_output_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')
                        return 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() click to toggle source
# File lib/maruku/output/to_html.rb, line 453
def to_html_li;        add_ws wrap_as_element('li')        end
to_html_li_span() click to toggle source
# File lib/maruku/output/to_html.rb, line 454
def to_html_li_span;   add_ws wrap_as_element('li')        end
to_html_linebreak() click to toggle source
# File lib/maruku/output/to_html.rb, line 344
def to_html_linebreak; Element.new 'br' end
to_html_ol() click to toggle source
# File lib/maruku/output/to_html.rb, line 452
def to_html_ol;        add_ws wrap_as_element('ol')        end
to_html_paragraph() click to toggle source
# File lib/maruku/output/to_html.rb, line 451
def to_html_paragraph; add_ws wrap_as_element('p')                end
to_html_quote() click to toggle source
# File lib/maruku/output/to_html.rb, line 455
def to_html_quote;     add_ws wrap_as_element('blockquote')  end
to_html_raw_html() click to toggle source

Attribute: filter_html Scope: document

If true, raw HTML is discarded from the output.

# File lib/maruku/output/to_html.rb, line 806
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_definition() click to toggle source
# File lib/maruku/output/to_html.rb, line 986
def to_html_ref_definition; [] end
to_html_strong() click to toggle source
# File lib/maruku/output/to_html.rb, line 456
def to_html_strong;    wrap_as_element('strong')           end
to_html_table() click to toggle source

FIXME: Ugly code

# File lib/maruku/output/to_html.rb, line 883
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_tree() click to toggle source

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

# File lib/maruku/output/to_html.rb, line 161
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_ul() click to toggle source
# File lib/maruku/output/to_html.rb, line 440
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() click to toggle source
# File lib/maruku/output/to_html.rb, line 948
def to_html_xml_instr
        target = self.target || ''
        code = self.code || ''
        REXML::Instruction.new(target, code)
end
to_latex_ref_definition() click to toggle source
# File lib/maruku/output/to_html.rb, line 987
def to_latex_ref_definition; [] end
wrap_as_element(name, attributes_to_copy=[]) click to toggle source

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

Sets 'id' if meta is set

# File lib/maruku/output/to_html.rb, line 349
        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() click to toggle source
# File lib/maruku/output/to_html.rb, line 118
def xml_newline() Text.new("\n") end

[Validate]

Generated with the Darkfish Rdoc Generator 2.