class PseudoHiki::PlainTextFormat

Constants

DescSep
Formatters

Public Class Methods

create(options={ :verbose_mode => false }) click to toggle source
# File lib/pseudohiki/plaintextformat.rb, line 78
def self.create(options={ :verbose_mode => false })
  formatter = {}

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

    [[InlineLeaf, InlineLeafFormatter],
     [LinkNode, LinkNodeFormatter],
     [DelNode, DelNodeFormatter],
     [DescLeaf, DescLeafFormatter],
     [VerbatimNode, VerbatimNodeFormatter],
     [TableNode, TableNodeFormatter],
     [CommentOutNode, CommentOutNodeFormatter],
     [ParagraphNode, ParagraphNodeFormatter],
     [PluginNode, PluginNodeFormatter]
    ].each do |node, formatter_class|
      formatter[node] = formatter_class.new(formatter, options)
    end
  end
end
format(tree, options={ :verbose_mode => false }) click to toggle source
# File lib/pseudohiki/plaintextformat.rb, line 20
def self.format(tree, options={ :verbose_mode => false })
  if Formatters.empty?
    default_options = { :verbose_mode => false }
    Formatters[default_options] = create(default_options)
  end

  Formatters[options] ||= create(options)
  Formatters[options].format(tree)
end
new(formatter={}, options={ :verbose_mode => false }) { || ... } click to toggle source
# File lib/pseudohiki/plaintextformat.rb, line 30
def initialize(formatter={}, options={ :verbose_mode => false })
  @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

choose_expander_of_col_and_row() click to toggle source
# File lib/pseudohiki/plaintextformat.rb, line 228
def choose_expander_of_col_and_row
  @options.verbose_mode ? ["||", "=="] : ["", ""]
end
create_self_element(tree=nil) click to toggle source
# File lib/pseudohiki/plaintextformat.rb, line 39
def create_self_element(tree=nil)
  Node.new
end
format(tree) click to toggle source
# File lib/pseudohiki/plaintextformat.rb, line 62
def format(tree)
  formatter = get_plain
  tree.accept(formatter).join
end
format_table(table, tree) click to toggle source
# File lib/pseudohiki/plaintextformat.rb, line 232
def format_table(table, tree)
  table.map {|row| row.join("\t") + $/ }.join
end
get_plain() click to toggle source
# File lib/pseudohiki/plaintextformat.rb, line 58
def get_plain
  @formatter[PlainNode]
end
push_visited_results(element, tree, memo) click to toggle source
# File lib/pseudohiki/plaintextformat.rb, line 48
def push_visited_results(element, tree, memo)
  tree.each {|token| element.push visited_result(token, memo) }
end
split_into_parts(tree, separator) click to toggle source
# File lib/pseudohiki/plaintextformat.rb, line 67
def split_into_parts(tree, separator)
  tree = tree.dup
  first_part = nil
  sep_index = tree.index(separator)
  if sep_index
    first_part = tree.shift(sep_index)
    tree.shift
  end
  return first_part, tree
end
visit(tree, memo) click to toggle source
# File lib/pseudohiki/plaintextformat.rb, line 52
def visit(tree, memo)
  element = create_self_element(tree)
  push_visited_results(element, tree, memo)
  element
end
visited_result(node, memo) click to toggle source
# File lib/pseudohiki/plaintextformat.rb, line 43
def visited_result(node, memo)
  visitor = @formatter[node.class] || @formatter[PlainNode]
  node.accept(visitor, memo)
end