lib/polites/html_formatter.rb
# Apply formatting to the given AST.
#
# @param [Object] obj
# @param [Bool] indent whether to apply indentation when formatting. Used internally.
# @param [String] join how to join multiple elements together. Used internally.
# @return [String]
Apply formatting to the given AST.
Apply formatting to the given AST .
def call(obj, indent: false, join: '')
      case obj
      when Sheet
        content = call(obj.content, indent: true, join: NL)
        content << NL << tag(:ol, NL + footnotes + NL, id: 'footnotes') << NL if @footnotes.any?
        content
      when ListIndenter::List
        nl = obj.children.first.level.positive? ? NL : ''
        tag_name = case obj.children.first
                   when Block::OrderedList then 'ol'
                   when Block::UnorderedList then 'ul'
                   end
        nl + tag(tag_name, NL + call(obj.children, join: NL) + NL) + nl
      when Array
        coll = indent ? @code_grouper.call(@indenter.call(obj)) : obj
        coll.map { |c| call(c) }.join(join)
      when Block::UnorderedList, Block::OrderedList
        tag(:li, call(obj.children))
      when Block::Paragraph
        tag(:p, call(obj.children))
      when Block::Heading1
        tag(:h1, call(obj.children))
      when Block::Heading2
        tag(:h2, call(obj.children))
      when Block::Heading3
        tag(:h3, call(obj.children))
      when Block::Heading4
        tag(:h4, call(obj.children))
      when Block::Heading5
        tag(:h5, call(obj.children))
      when Block::Heading6
        tag(:h6, call(obj.children))
      when Block::Blockquote
        tag(:blockquote, tag(:p, call(obj.children)))
      when Block::CodeBlock
        tag(:pre, tag(:code, CGI.escape_html(call(obj.children)), class: "language-#{obj.syntax}"))
      when Block::Divider
        tag(:hr)
      when Span::Strong
        tag(:strong, call(obj.children))
      when Span::Emph
        tag(:em, call(obj.children))
      when Span::Delete
        tag(:del, call(obj.children))
      when Span::Mark
        tag(:mark, call(obj.children))
      when Span::Code
        tag(:code, call(obj.children))
      when Span::Annotation
        call(obj.children)
      when Span::Footnote
        @footnotes << obj.text
        n = @footnotes.count
        tag(:sup, tag(:a, n, id: "ffn#{n}", href: "#fn#{n}", class: 'footnote'))
      when Span::Image
        figcaption = obj.description ? tag(:figcaption, obj.description) : ''
        tag(:figure, tag(:img, nil, src: image_src(obj), title: obj.title, alt: obj.description, width: obj.width, height: obj.height) + figcaption)
      when Span::Link
        tag(:a, call(obj.children), href: obj.url, title: obj.title)
      when Text
        obj.text
      else
        raise FormattingError, "Unknown obj #{obj.inspect}"
      end
    end
def call ( obj , indent : false , join : '' ) case obj when Sheet content = call ( obj . content , indent : true , join : NL ) content << NL << tag ( :ol , NL + footnotes + NL , id : 'footnotes' ) << NL if @footnotes . any? content when ListIndenter :: List nl = obj . children . first . level . positive? ? NL : '' tag_name = case obj . children . first when Block :: OrderedList then 'ol' when Block :: UnorderedList then 'ul' end nl + tag ( tag_name , NL + call ( obj . children , join : NL ) + NL ) + nl when Array coll = indent ? @code_grouper . call ( @indenter . call ( obj ) ) : obj coll . map { | c | call ( c ) } . join ( join ) when Block :: UnorderedList , Block :: OrderedList tag ( :li , call ( obj . children ) ) when Block :: Paragraph tag ( :p , call ( obj . children ) ) when Block :: Heading1 tag ( :h1 , call ( obj . children ) ) when Block :: Heading2 tag ( :h2 , call ( obj . children ) ) when Block :: Heading3 tag ( :h3 , call ( obj . children ) ) when Block :: Heading4 tag ( :h4 , call ( obj . children ) ) when Block :: Heading5 tag ( :h5 , call ( obj . children ) ) when Block :: Heading6 tag ( :h6 , call ( obj . children ) ) when Block :: Blockquote tag ( :blockquote , tag ( :p , call ( obj . children ) ) ) when Block :: CodeBlock tag ( :pre , tag ( :code , CGI . escape_html ( call ( obj . children ) ) , class : "language-#{obj.syntax}" ) ) when Block :: Divider tag ( :hr ) when Span :: Strong tag ( :strong , call ( obj . children ) ) when Span :: Emph tag ( :em , call ( obj . children ) ) when Span :: Delete tag ( :del , call ( obj . children ) ) when Span :: Mark tag ( :mark , call ( obj . children ) ) when Span :: Code tag ( :code , call ( obj . children ) ) when Span :: Annotation call ( obj . children ) when Span :: Footnote @footnotes << obj . text n = @footnotes . count tag ( :sup , tag ( :a , n , id : "ffn#{n}" , href : "#fn#{n}" , class : 'footnote' ) ) when Span :: Image figcaption = obj . description ? tag ( :figcaption , obj . description ) : '' tag ( :figure , tag ( :img , nil , src : image_src ( obj ) , title : obj . title , alt : obj . description , width : obj . width , height : obj . height ) + figcaption ) when Span :: Link tag ( :a , call ( obj . children ) , href : obj . url , title : obj . title ) when Text obj . text else raise FormattingError , "Unknown obj #{obj.inspect}" end end
Apply formatting to the given AST.
Apply formatting to the given AST .
None
String
obj
<not_specific>
indent
<not_specific>
join
<not_specific>
<not_specific>
join how to join multiple elements together. Used internally.
join how to join multiple elements together . Used internally .