# encoding: utf-8 module Koara module Xml class XmlRenderer attr_accessor :declaration_tag attr_accessor :hard_wrap def visit_document(node) @level = 0 @out = StringIO.new if @declaration_tag @out << @declaration_tag + "\n" end if node.children && node.children.length > 0 @out << "\n" node.children_accept(self) @out << '' else @out << '' end end def visit_heading(node) @level += 1 indent @out << " 0 @out << ">\n" @level += 1 node.children_accept(self) @level -= 1 indent @out << "\n" else @out << " />\n" end @level -= 1 end def visit_blockquote(node) @level+=1 indent @out << ' 0 @out << ">\n" @level+=1 node.children_accept(self) @level-=1 indent @out << "\n" @level-=1 else @out << " />\n" end end def visit_list_block(node) @level += 1 indent @out << "\n" node.children_accept(self) indent @out << "\n" @level -= 1 end def visit_list_item(node) @level += 1 indent @out << " 0 @out << ">\n" node.children_accept(self) indent @out << "\n" else @out << " />\n" end @level -= 1 end def visit_codeblock(node) @level += 1 indent @out << " 0 @out << ">" @level += 1 @out << escape(node.value) @level -= 1 @out << "\n" else @out << " />\n" end @level -= 1 end def visit_paragraph(node) @level += 1 indent @out << "\n" @level += 1 node.children_accept(self) @level -= 1 indent @out << "\n" @level -= 1 end def visit_block_element(node) end def visit_image(node) indent @out << "\n" @level += 1 node.children_accept(self) @level -= 1 indent() @out << "\n" end def visit_link(node) indent @out << "\n" @level += 1 node.children_accept(self) @level -= 1 indent @out << "\n" end def visit_text(node) indent() @out << '' @out << escape(node.value) @out << "\n" end def visit_strong(node) indent @out << "\n" @level += 1 node.children_accept(self) @level -= 1 indent @out << "\n" end def visit_em(node) indent @out << "\n" @level += 1 node.children_accept(self) @level -= 1 indent @out << "\n" end def visit_code(node) indent @out << "\n" @level += 1 node.children_accept(self) @level -= 1 indent @out << "\n" end def visit_linebreak(node) hard = @hard_wrap || node.explicit indent @out << "\n" end def escape_url(text) return text.gsub(' ', '%20') .gsub("\"", '%22') .gsub('`', '%60') .gsub('<', '%3C') .gsub('>', '%3E') .gsub('[', '%5B') .gsub(']', '%5D') .gsub("\\", '%5C') end def indent repeat = @level * 2 repeat.times { @out << ' ' } end def escape(text) text.gsub('&', '&') .gsub('<', '<') .gsub('>', '>') .gsub('"', '"') end def output @out.string end end end end