class Koara::Html::Html5Renderer
Attributes
hard_wrap[RW]
heading_ids[RW]
partial[RW]
Public Class Methods
new()
click to toggle source
# File lib/koara/html/html5renderer.rb, line 9 def initialize @partial = true @hard_wrap = false @heading_ids = false end
Public Instance Methods
escape(text)
click to toggle source
# File lib/koara/html/html5renderer.rb, line 163 def escape(text) return text.gsub(/&/, '&') .gsub(/</, '<') .gsub(/>/, '>') .gsub(/"/, '"') end
escape_url(text)
click to toggle source
# File lib/koara/html/html5renderer.rb, line 178 def escape_url(text) text.gsub(/ /, '%20') .gsub(/"/, '%22') .gsub(/`/, '%60') .gsub(/</, '%3C') .gsub(/>/, '%3E') .gsub(/\[/, '%5B') .gsub(/\]/, '%5D') .gsub(/\\/, '%5C') end
indent()
click to toggle source
# File lib/koara/html/html5renderer.rb, line 189 def indent repeat = @level * 2 str = StringIO.new repeat.times { str << ' ' } str.string end
output()
click to toggle source
# File lib/koara/html/html5renderer.rb, line 198 def output if(!partial) wrapper = "<!DOCTYPE html>\n"; wrapper << "<html>\n"; wrapper << " <body>\n"; wrapper << @out.string.strip.gsub(/^/, " ") + "\n"; wrapper << " </body>\n"; wrapper << "</html>\n"; return wrapper; end @out.string.strip end
visit_block_element(node)
click to toggle source
# File lib/koara/html/html5renderer.rb, line 116 def visit_block_element(node) if node.nested && node.parent.instance_of?(Koara::Ast::ListItem) && node.is_single_child node.children_accept(self) else @out << indent node.children_accept(self) if !node.nested @out << "\n" end end end
visit_blockquote(node)
click to toggle source
# File lib/koara/html/html5renderer.rb, line 40 def visit_blockquote(node) @out << indent + '<blockquote>' if !node.children.nil? && node.children.any? @out << "\n" end @level += 1 node.children_accept(self) @level-=1 @out << indent + "</blockquote>\n" if !node.nested @out << "\n" end end
visit_code(node)
click to toggle source
# File lib/koara/html/html5renderer.rb, line 152 def visit_code(node) @out << '<code>' node.children_accept(self) @out << '</code>' end
visit_codeblock(node)
click to toggle source
# File lib/koara/html/html5renderer.rb, line 93 def visit_codeblock(node) @out << indent + '<pre><code' if node.language @out << " class=\"language-" + escape(node.language) + "\"" end @out << '>' @out << escape(node.value) + "</code></pre>\n" @out << ("\n") if !node.nested end
visit_document(node)
click to toggle source
# File lib/koara/html/html5renderer.rb, line 15 def visit_document(node) @level = 0 @list_sequence = Array.new @out = StringIO.new node.children_accept(self) end
visit_em(node)
click to toggle source
# File lib/koara/html/html5renderer.rb, line 146 def visit_em(node) @out << '<em>' node.children_accept(self) @out << '</em>' end
visit_heading(node)
click to toggle source
# File lib/koara/html/html5renderer.rb, line 22 def visit_heading(node) @out << indent + '<h' + node.value.to_s if @heading_ids id = '' node.children.each do |n| id << n.value.to_s end @out << " id=\"" + id.downcase.gsub(/ /, '_') + "\"" end @out << '>' node.children_accept(self) @out << '</h' + node.value.to_s + ">\n" unless node.nested @out << "\n" end end
visit_image(node)
click to toggle source
# File lib/koara/html/html5renderer.rb, line 128 def visit_image(node) @out << "<img src=\"" + escape_url(node.value) + "\" alt=\"" node.children_accept(self) @out << "\" />" end
visit_linebreak(node)
click to toggle source
# File lib/koara/html/html5renderer.rb, line 170 def visit_linebreak(node) if @hard_wrap || node.explicit @out << "<br>" end @out << "\n" + indent node.children_accept(self) end
visit_link(node)
click to toggle source
# File lib/koara/html/html5renderer.rb, line 134 def visit_link(node) @out << "<a href=\"" + escape_url(node.value) + "\">" node.children_accept(self) @out << '</a>' end
visit_list_block(node)
click to toggle source
# File lib/koara/html/html5renderer.rb, line 54 def visit_list_block(node) @list_sequence.push(0) tag = node.ordered ? 'ol' : 'ul' @out << "#{indent}<#{tag}>\n" @level += 1 node.children_accept(self) @level -= 1 @out << "#{indent}</#{tag}>\n" if !node.nested @out << "\n" end @list_sequence.pop end
visit_list_item(node)
click to toggle source
# File lib/koara/html/html5renderer.rb, line 68 def visit_list_item(node) seq = @list_sequence.last.to_i + 1 @list_sequence[-1] = seq @out << "#{indent}<li" if node.number && seq != node.number.to_i @out << " value=\"#{node.number}\"" @list_sequence.push(node.number) end @out << '>' if !node.children.nil? block = node.children[0].instance_of?(Koara::Ast::Paragraph) || node.children[0].instance_of?(Koara::Ast::BlockElement) if (node.children.length > 1 || !block) @out << "\n" end @level += 1 node.children_accept(self) @level -= 1 if (node.children.length > 1 || !block) @out << indent end end @out << "</li>\n" end
visit_paragraph(node)
click to toggle source
# File lib/koara/html/html5renderer.rb, line 103 def visit_paragraph(node) if node.nested && node.parent.instance_of?(Koara::Ast::ListItem) && node.is_single_child node.children_accept(self) else @out << indent + '<p>' node.children_accept(self) @out << "</p>\n" unless node.nested @out << "\n" end end end
visit_strong(node)
click to toggle source
# File lib/koara/html/html5renderer.rb, line 140 def visit_strong(node) @out << '<strong>' node.children_accept(self) @out << '</strong>' end
visit_text(node)
click to toggle source
# File lib/koara/html/html5renderer.rb, line 158 def visit_text(node) @out << escape(node.value.to_s) end