Class: PseudoHiki::PlainTextFormat

Inherits:
Object
  • Object
show all
Includes:
BlockParser::BlockElement, InlineParser::InlineElement
Defined in:
lib/pseudohiki/plaintextformat.rb

Defined Under Namespace

Classes: CommentOutNodeFormatter, DelNodeFormatter, DescLeafFormatter, InlineLeafFormatter, LinkNodeFormatter, Node, ParagraphNodeFormatter, PluginNodeFormatter, TableNodeFormatter, VerbatimNodeFormatter

Constant Summary collapse

DescSep =
[InlineParser::DescSep]
Formatters =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(formatter = {}, options = { :verbose_mode => false }) ⇒ PlainTextFormat

Returns a new instance of PlainTextFormat.



30
31
32
33
34
35
36
37
# 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

Class Method Details

.create(options = { :verbose_mode => false }) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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 }) ⇒ Object



20
21
22
23
24
25
26
27
28
# 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

Instance Method Details

#choose_expander_of_col_and_rowObject



228
229
230
# File 'lib/pseudohiki/plaintextformat.rb', line 228

def choose_expander_of_col_and_row
  @options.verbose_mode ? ["||", "=="] : ["", ""]
end

#create_self_element(tree = nil) ⇒ Object



39
40
41
# File 'lib/pseudohiki/plaintextformat.rb', line 39

def create_self_element(tree=nil)
  Node.new
end

#format(tree) ⇒ Object



62
63
64
65
# File 'lib/pseudohiki/plaintextformat.rb', line 62

def format(tree)
  formatter = get_plain
  tree.accept(formatter).join
end

#format_table(table, tree) ⇒ Object



232
233
234
# File 'lib/pseudohiki/plaintextformat.rb', line 232

def format_table(table, tree)
  table.map {|row| row.join("\t") + $/ }.join
end

#get_plainObject



58
59
60
# File 'lib/pseudohiki/plaintextformat.rb', line 58

def get_plain
  @formatter[PlainNode]
end

#push_visited_results(element, tree, memo) ⇒ Object



48
49
50
# 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) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Object



52
53
54
55
56
# 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) ⇒ Object



43
44
45
46
# File 'lib/pseudohiki/plaintextformat.rb', line 43

def visited_result(node, memo)
  visitor = @formatter[node.class] || @formatter[PlainNode]
  node.accept(visitor, memo)
end