class Koara::KoaraRenderer

Public Instance Methods

escape(text) click to toggle source
# File lib/koara/koararenderer.rb, line 192
def escape(text)
  return text.gsub(/\[/, "\\\\[")
             .gsub(/\]/, "\\\\]")
             .gsub(/\*/, "\\\\*")
             .gsub(/_/, "\\\\_")
             .sub(/`/, "\\\\`")
             .sub(/=/, "\\\\=")
             .sub(/>/, "\\\\>")
             .sub(/-/, "\\\\-")
             .sub(/(\d+)\./) { |m| "\\#{$1}." }
end
escape_url(text) click to toggle source
# File lib/koara/koararenderer.rb, line 187
def escape_url(text)
  return text.gsub(/\(/, "\\\\(")
             .gsub(/\)/, "\\\\)")
end
indent() click to toggle source
# File lib/koara/koararenderer.rb, line 204
def indent
  @left.each { |s|
    @out << s
  }
end
output() click to toggle source
# File lib/koara/koararenderer.rb, line 210
def output
  @out.string.strip
end
visit_blockelement(node) click to toggle source
# File lib/koara/koararenderer.rb, line 117
def visit_blockelement(node)
  if !node.is_first_child
    indent
  end
  node.children_accept(self)
  @out << "\n"
  if !node.nested || (node.parent.instance_of? ListItem && (node.next.instance_of? Paragraph) && !node.is_last_child())
    @out << "\n"
  elsif node.parent.instance_of?(BlockQuote) && node.next.instance_of?(Paragraph)
    indent
    @out << "\n"
  end
end
visit_blockquote(node) click to toggle source
# File lib/koara/koararenderer.rb, line 35
def visit_blockquote(node)
  if !node.is_first_child
    indent
  end
  if node.has_children
    @out << '> '
    @left.push('> ')
    node.children_accept(self)
    @left.pop
  else
    @out << ">\n"
  end
  if !node.nested
    @out << "\n"
  end
end
visit_code(node) click to toggle source
# File lib/koara/koararenderer.rb, line 173
def visit_code(node)
  @out << '`'
  node.children_accept(self)
  @out << '`'
end
visit_codeblock(node) click to toggle source
# File lib/koara/koararenderer.rb, line 83
def visit_codeblock(node)
  str = StringIO.new
  @left.each { |s| str << s }
  @out << '```'
  if node.language
    @out << node.language
  end
  @out << "\n"
  @out << node.value.gsub(/^/, str.string)
  @out << "\n"
  indent
  @out << '```'
  @out << "\n"
  if !node.is_last_child
    indent
    @out << "\n"
  end
end
visit_document(node) click to toggle source
# File lib/koara/koararenderer.rb, line 10
def visit_document(node)
  @left = Array.new
  @out = StringIO.new
  @hard_wrap = false
  node.children_accept(self)
end
visit_em(node) click to toggle source
# File lib/koara/koararenderer.rb, line 167
def visit_em(node)
  @out << '_'
  node.children_accept(self)
  @out << '_'
end
visit_heading(node) click to toggle source
# File lib/koara/koararenderer.rb, line 17
def visit_heading(node)
  if !node.is_first_child
    indent
  end
  node.value.times {
    @out << '='
  }
  if node.has_children
    @out << ' '
    node.children_accept(self)
  end
  @out << "\n"
  if !node.is_last_child
    indent
    @out << "\n"
  end
end
visit_image(node) click to toggle source
# File lib/koara/koararenderer.rb, line 131
def visit_image(node)
  @out << '[image: '
  node.children_accept(self)
  @out << ']'
  if node.value && node.value.strip.length > 0
    @out << '('
    @out << escape_url(node.value)
    @out << ')'
  end
end
visit_linebreak(node) click to toggle source
# File lib/koara/koararenderer.rb, line 179
def visit_linebreak(node)
  if @hard_wrap || node.explicit
    @out << '  '
  end
  @out << "\n"
  indent
end
visit_list_block(node) click to toggle source
# File lib/koara/koararenderer.rb, line 52
def visit_list_block(node)
  node.children_accept(self)
  if !node.is_last_child
    indent
    @out << "\n"
    next_node = node.next
    if next_node.instance_of?(Ast::ListBlock) && next_node.ordered == node.ordered
      @out << "\n"
    end
  end
end
visit_list_item(node) click to toggle source
# File lib/koara/koararenderer.rb, line 64
def visit_list_item(node)
  if !node.parent.nested || !node.is_first_child || !node.parent.is_first_child
    indent
  end
  @left.push('  ')
  if node.number
    @out << node.number + '.'
  else
    @out << '-'
  end
  if node.has_children
    @out << ' '
    node.children_accept(self)
  else
    @out << "\n"
  end
  @left.pop
end
visit_paragraph(node) click to toggle source
# File lib/koara/koararenderer.rb, line 102
def visit_paragraph(node)
  if !node.is_first_child
    indent
  end

  node.children_accept(self)
  @out << "\n"
  if !node.nested || ((node.parent.instance_of?(Ast::ListItem) && node.next.instance_of?(Ast::Paragraph)) && !node.is_last_child)
    @out << "\n"
  elsif node.parent.instance_of?(Ast::BlockQuote) && node.next.instance_of?(Ast::Paragraph)
    indent
    @out << "\n"
  end
end
visit_strong(node) click to toggle source
# File lib/koara/koararenderer.rb, line 161
def visit_strong(node)
  @out << '*'
  node.children_accept(self)
  @out << '*'
end
visit_text(node) click to toggle source
# File lib/koara/koararenderer.rb, line 153
def visit_text(node)
  if node.parent.instance_of? Ast::Code
    @out << node.value.to_s;
  else
    @out << escape(node.value.to_s);
  end
end