class Fictive::Syntax::Parser

Public Class Methods

new(input) click to toggle source
# File lib/fictive/syntax.rb, line 23
def initialize(input)
  @input = clean_input(input)
end

Public Instance Methods

parse() click to toggle source
# File lib/fictive/syntax.rb, line 27
def parse
  reset_parser

  document = Element.new(:document)

  while !@scanner.eos?
    document.children << parse_block_level
  end

  document
end

Private Instance Methods

clean_input(input) click to toggle source
# File lib/fictive/syntax.rb, line 101
def clean_input(input)
  input.gsub(/\r\n?/, EOL).chomp + EOL
end
parse_atx_header() click to toggle source
# File lib/fictive/syntax.rb, line 57
def parse_atx_header
  level = 1
  level += 1 while @scanner.scan(/#/)

  if @scanner.scan(/\s/)
    Element.new("h#{level}".to_sym, scan_to_eol)
  else
    Element.new(:id, scan_to_eol)
  end
end
parse_blank_line() click to toggle source
# File lib/fictive/syntax.rb, line 53
def parse_blank_line
  Element.new(:blank_line)
end
parse_block_level() click to toggle source
# File lib/fictive/syntax.rb, line 45
def parse_block_level
  return parse_atx_header if @scanner.scan(/#/)
  return parse_blank_line if @scanner.scan(/#{EOL}/)
  return parse_list_item if @scanner.scan(/-/)

  parse_paragraph
end
parse_list_item() click to toggle source
# File lib/fictive/syntax.rb, line 68
def parse_list_item
  if @scanner.scan(/\s/)
    Element.new(:list_item, scan_to_eol)
  else
    parse_paragraph
  end
end
parse_paragraph() click to toggle source
# File lib/fictive/syntax.rb, line 76
def parse_paragraph
  parts = []
  text = scan_paragraph_line

  if text
    parts << text.rstrip

    while !@scanner.match?(/\n/)
      next_text = scan_paragraph_line
      break unless next_text
      parts << next_text.rstrip
    end

    Element.new(:paragraph, parts.join(' '))
  end
end
reset_parser() click to toggle source
# File lib/fictive/syntax.rb, line 41
def reset_parser
  @scanner = StringScanner.new(@input)
end
scan_paragraph_line() click to toggle source
# File lib/fictive/syntax.rb, line 93
def scan_paragraph_line
  @scanner.scan_until(/\n/)
end
scan_to_eol() click to toggle source
# File lib/fictive/syntax.rb, line 97
def scan_to_eol
  @scanner.scan_until(/#{EOL}/).rstrip
end