Class: Asuka::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/asuka/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line_formatter = DEFAULT_LINE_FORMATTER, pre_line_formatter = DEFAULT_PRE_LINE_FORMATTER) ⇒ Parser

Returns a new instance of Parser.



11
12
13
14
# File 'lib/asuka/parser.rb', line 11

def initialize(line_formatter=DEFAULT_LINE_FORMATTER, pre_line_formatter=DEFAULT_PRE_LINE_FORMATTER)
  @line_formatter     = line_formatter
  @pre_line_formatter = pre_line_formatter
end

Instance Attribute Details

#line_formatterObject

Returns the value of attribute line_formatter.



5
6
7
# File 'lib/asuka/parser.rb', line 5

def line_formatter
  @line_formatter
end

#pre_line_formatterObject

Returns the value of attribute pre_line_formatter.



5
6
7
# File 'lib/asuka/parser.rb', line 5

def pre_line_formatter
  @pre_line_formatter
end

Instance Method Details

#parse(content) ⇒ Object



16
17
18
19
# File 'lib/asuka/parser.rb', line 16

def parse(content)
  lines = content.respond_to?(:join) ? content : StringIO.new(content).readlines
  parse_lines(lines)
end

#parse_lines(lines) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/asuka/parser.rb', line 21

def parse_lines(lines)
  result = []
  acc = Accumulator.new

  set = Rules::Set.new(rules.map { |rule| rule.new(acc, result, line_formatter) })

  pstate = set.rules.first
  lines.each do |line|
    line   = pre_line_formatter.format(line)

    cstate = set.rule_for(line)
    pstate.transition if cstate != pstate
    cstate.process(line)

    pstate = cstate
  end
  pstate.transition # flush last state

  result
end

#rulesObject



7
8
9
# File 'lib/asuka/parser.rb', line 7

def rules
  [Rules::Initial, Rules::UnorderedList, Rules::Blockquote, Rules::Rule, Rules::Header, Rules::Paragraph]
end