Class: Bookie::Parser

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_data, emitter) ⇒ Parser

Returns a new instance of Parser.



17
18
19
20
21
22
# File 'lib/bookie/parser.rb', line 17

def initialize(raw_data, emitter)
  @emitter        = emitter
  @parsed_content = []

  parse_contents(raw_data)
end

Instance Attribute Details

#parsed_contentObject (readonly)

Returns the value of attribute parsed_content.



15
16
17
# File 'lib/bookie/parser.rb', line 15

def parsed_content
  @parsed_content
end

Class Method Details

.parse(raw_data, emitter = Bookie::Emitters::Null.new) ⇒ Object



10
11
12
13
# File 'lib/bookie/parser.rb', line 10

def self.parse(raw_data, emitter=Bookie::Emitters::Null.new)
  parser = new(raw_data, emitter)
  parser.parsed_content
end

Instance Method Details

#extract_inlines(paragraph_text) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bookie/parser.rb', line 24

def extract_inlines(paragraph_text)
  scanner = StringScanner.new(paragraph_text)
  modes   = [NormalText, CodeText].cycle
  output  = []

  current_mode = modes.next
  current_mode = modes.next if scanner.scan(/`/)

  until scanner.eos?
    output << current_mode.new(scanner.scan(/[^`]+/m))
    current_mode = modes.next if scanner.scan(/`/)
  end

  output
end

#extract_list(contents) ⇒ Object



59
60
61
62
63
# File 'lib/bookie/parser.rb', line 59

def extract_list(contents)
  list = List.new(contents.map { |e| e.chomp })
  @emitter.build_list(list)
  parsed_content << list
end

#extract_paragraph(paragraph_text) ⇒ Object



40
41
42
43
44
45
# File 'lib/bookie/parser.rb', line 40

def extract_paragraph(paragraph_text)
  text_segments = extract_inlines(paragraph_text.gsub(/\s+/," "))
  paragraph = Paragraph.new(text_segments)
  @emitter.build_paragraph(paragraph)
  parsed_content << paragraph
end

#extract_raw_text(contents) ⇒ Object



47
48
49
50
51
# File 'lib/bookie/parser.rb', line 47

def extract_raw_text(contents)
  raw_text = RawText.new(contents)
  @emitter.build_raw_text(raw_text)
  parsed_content << raw_text      
end

#extract_section_heading(contents) ⇒ Object



53
54
55
56
57
# File 'lib/bookie/parser.rb', line 53

def extract_section_heading(contents)
  header = SectionHeading.new(contents.chomp)
  @emitter.build_section_heading(header)
  parsed_content << header
end