class PseudoHiki::MarkDownFormat

Constants

Formatters
GFM_STRIPPED_CHARS
GFM_STRIPPED_CHARS_PAT

Public Class Methods

convert_into_gfm_id_format(heading) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 37
def self.convert_into_gfm_id_format(heading)
  heading.gsub(GFM_STRIPPED_CHARS_PAT) do |char|
    /\A\s+\Z/o.match?(char) ? '-'.freeze : ''.freeze
  end.downcase
end
create(options={ :strict_mode => false }) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 120
def self.create(options={ :strict_mode => false })
  formatter = {}

  new(formatter, options).tap do |main_formatter|
    formatter.default = main_formatter

    [[InlineLeaf, InlineLeafFormatter],
     [LinkNode, LinkNodeFormatter],
     [EmNode, EmNodeFormatter],
     [StrongNode, StrongNodeFormatter],
     [DelNode, DelNodeFormatter],
     [LiteralNode, LiteralNodeFormatter],
     [PluginNode, PluginNodeFormatter],
     [VerbatimLeaf, VerbatimLeafFormatter],
     [CommentOutLeaf, CommentOutLeafFormatter],
     [HeadingLeaf, HeadingLeafFormatter],
     [HrLeaf, HrLeafFormatter],
     [DescNode, DescNodeFormatter],
     [VerbatimNode, VerbatimNodeFormatter],
     [QuoteNode, QuoteNodeFormatter],
     [TableNode, TableNodeFormatter],
     [HeadingNode, HeadingNodeFormatter],
     [ParagraphNode, ParagraphNodeFormatter],
     [ListNode, ListNodeFormatter],
     [EnumNode, EnumNodeFormatter],
     [ListWrapNode, ListWrapNodeFormatter],
     [EnumWrapNode, EnumWrapNodeFormatter]
    ].each  do |node, formatter_class|
      formatter[node] = formatter_class.new(formatter, options)
    end
  end
end
default_options() click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 23
def self.default_options
  @default_options
end
format(tree, options=MarkDownFormat.default_options) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 27
def self.format(tree, options=MarkDownFormat.default_options)
  if Formatters.empty?
    default_options = MarkDownFormat.default_options
    Formatters[default_options] = create(default_options)
  end

  Formatters[options] ||= create(options)
  Formatters[options].format(tree)
end
new(formatter={}, options=MarkDownFormat.default_options) { || ... } click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 43
def initialize(formatter={}, options=MarkDownFormat.default_options)
  @formatter = formatter
  if block_given?
    options_given_via_block = yield
    options.merge!(options_given_via_block)
  end
  @options = OpenStruct.new(options)
end

Public Instance Methods

collect_headings(tree) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 98
def collect_headings(tree)
  PseudoHiki::Utils::NodeCollector.select(tree) do |node|
    node.kind_of? PseudoHiki::BlockParser::HeadingLeaf
  end
end
create_self_element(tree=nil) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 52
def create_self_element(tree=nil)
  HtmlElement::Children.new
end
enclose_in(element, mark) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 89
def enclose_in(element, mark)
  element.push mark
  element.unshift mark
end
format(tree) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 77
def format(tree)
  formatter = get_plain
  prepare_id_conv_table(tree) if @options.gfm_style
  tree.accept(formatter).join
end
get_plain() click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 73
def get_plain
  @formatter[PlainNode]
end
heading_to_gfm_id(heading) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 104
def heading_to_gfm_id(heading)
  heading_text = PlainTextFormat.format(heading).strip
  MarkDownFormat.convert_into_gfm_id_format(heading_text)
end
list_mark(tree, mark) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 83
def list_mark(tree, mark)
  mark = mark.dup
  mark << " " unless /^ /o.match? tree.join
  " " * (tree.level - 1) * 2 + mark
end
prepare_id_conv_table(tree) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 109
def prepare_id_conv_table(tree)
  {}.tap do |table|
    collect_headings(tree).each do |heading|
      if node_id = heading.node_id
        table[node_id] = heading_to_gfm_id(heading)
      end
    end
    @formatter[LinkNode].id_conv_table = table
  end
end
push_visited_results(element, tree, memo) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 61
def push_visited_results(element, tree, memo)
  tree.each {|token| element.push visited_result(token, memo) }
end
remove_trailing_newlines_in_html_element(element) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 94
def remove_trailing_newlines_in_html_element(element)
  element.to_s.gsub(/([^>])\r?\n/, "\\1") << $/
end
tap_element_in_visit(elm, tree, memo) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 71
def tap_element_in_visit(elm, tree, memo); end
visit(tree, memo) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 65
def visit(tree, memo)
  element = create_self_element(tree)
  push_visited_results(element, tree, memo)
  element.tap {|elm| tap_element_in_visit(elm, tree, memo) }
end
visited_result(node, memo) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 56
def visited_result(node, memo)
  visitor = @formatter[node.class] || @formatter[PlainNode]
  node.accept(visitor, memo)
end