Module: Accio::Parser

Defined in:
lib/accio/parser.rb

Overview

Public: The snippet parser module is responsible for parsing markdown based snippet documents.

Defined Under Namespace

Classes: NoConfigError, NoSnippetFileError

Class Method Summary collapse

Class Method Details

.readObject

Public: Reads the snippet markdown file, parses the codelines and collects groups and snippets.

Returns a Content object.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/accio/parser.rb', line 18

def read
  content   = Accio::Content.new
  code_area = false

  while (line = file.gets)
    header_count = line.count("#")

    # Whenever a code area starts we flip this code_area.
    if line.count("`") == 3
      code_area = !code_area
    end

    # If markdown header character found and not a code group.
    if header_count == 1 && code_area == false
      # Main Group. Sets the group globally so that snippets get
      # associated with this group.
      group = Accio::Group.new(parse_headline(line))
      content.groups << group
    elsif header_count == 2 && code_area == false
      # Snippet.
      snippet = Accio::Snippet.new(parse_headline(line))
      group.snippets << snippet
    else
      # Code text or comments.
      if snippet
        if line.count("*") == 4
          snippet.comment.add(parse_comment(line))
        else
          unless line.length < 2 || line == "\n"
            snippet.code.add(parse_code(line))
          end
        end
      end
    end
  end
  file.close
  content
end