Class: Elisp::Parser

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

Constant Summary collapse

HEADER_LINE =
%r{
  ;;;[ ]
  (?<title>[a-z]+[.]el)[ ]---[ ](?<desc>.*?)
  (?:[ ]+-[*]-[ ]*(?<vars>.*?)[ ]*-[*]-)?
  \n+
}x
COMMENT =
/ *;; (?<comment>.*)\n/
CODE =
%r{
  (?<content>
    (?:
      [A-Za-z0-9&,.:=?_#'`<>()\[\]\t +*/-]
      | [?]\\.
      | "(?:[^\\"]|\\(?:.|\n))*"
    )+?
    (?:[ ]*;.*)?
    | ;;;[#][#][#]autoload.*
  )
  \n
}x
BLANKLINES =
/\n+/
CODE_OR_BLANKLINES =
Regexp.union(CODE, BLANKLINES)
DEFAULT_CSS =
"<style>#{css}</style>"
HEADING =
%r{
  ;;;(?<level>;*)[ ](?<title>.+)\n
  (?:(?:;;)?\n)*
}x

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Parser

Returns a new instance of Parser.



50
51
52
53
54
# File 'lib/elisp/parser.rb', line 50

def initialize(input)
  @scanner = StringScanner.new(input.read)
  @state = :start
  @nodes = []
end

Instance Method Details

#make_major_paraObject



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/elisp/parser.rb', line 134

def make_major_para
  paras = [@nodes.pop.to_minor_para]
  while (node = @nodes.pop)
    if node.is_a?(Elisp::MinorPara)
      paras.unshift(node)
    else
      @nodes.push(node)
      break
    end
  end
  @nodes << Elisp::MajorPara.new(paras)
end

#pandoc_json_objectObject



165
166
167
168
169
170
171
# File 'lib/elisp/parser.rb', line 165

def pandoc_json_object
  {
    "pandoc-api-version": [1,23,1,1],
    "meta": {},
    "blocks": @nodes.map(&:pandoc_json_object),
  }
end

#parseObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/elisp/parser.rb', line 56

def parse
  until @scanner.eos?

    if @state == :start && @scanner.skip(HEADER_LINE)
      @nodes << Elisp::Heading.new(@scanner[:title], level: 1)
      @nodes << Elisp::Desc.new(@scanner[:desc])
      @nodes << Elisp::Variables.new(@scanner[:vars])
      @state = :after_header_line

    elsif [:after_header_line,
           :after_major_para,
           :after_minor_para,
           :after_code,
           :after_page_delimiter,
           :after_heading].include?(@state) &&
          @scanner.skip(HEADING)
      level = @scanner[:level].size
      title = @scanner[:title]
      if level.zero?
        if ["Commentary:", "Code:"].include?(title)
          title.chop!
        elsif title.match?(/[a-z]+[.]el ends here/)
          next  # validate file name?
        end
      end
      @nodes << Elisp::Heading.new(title, level: level + 2)
      @state = :after_heading

    elsif [:after_heading,
           :after_major_para,
           :after_minor_para,
           :after_code,
           :after_header_line].include?(@state) &&
          @scanner.skip(COMMENT)
      @nodes << Elisp::Comment.new(@scanner[:comment])
      @state = :after_comment

    elsif @state == :after_comment && @scanner.skip(";;\n")
      @nodes[-1] = @nodes.last.to_minor_para
      @state = :after_minor_para

    elsif @state == :after_comment && @scanner.skip(COMMENT)
      @nodes.last << @scanner[:comment]

    elsif @state == :after_comment && @scanner.skip(BLANKLINES)
      make_major_para
      @state = :after_major_para

    elsif [:after_major_para, :after_page_delimiter, :after_heading].include?(@state) &&
          @scanner.skip(CODE)
      @nodes << Elisp::Code.new(@scanner[:content])
      @state = :after_code

    elsif @state == :after_comment && @scanner.skip(CODE)
      make_major_para
      @nodes << Elisp::Code.new(@scanner[:content])
      @state = :after_code

    elsif @state == :after_code && @scanner.skip(CODE_OR_BLANKLINES)
      @nodes.last << @scanner[:content]

    elsif @state == :after_code && @scanner.skip(/\f\n+/)
      @nodes << Elisp::PageDelimiter.new
      @state = :after_page_delimiter

    else
      raise Elisp::Error, <<~MESSAGE
        parse failed
        --- rest line (#{@state}) ---
        #{@scanner.rest.lines.first.inspect}
        --- scanner ---
        #{@scanner.inspect}
      MESSAGE
    end
  end
  self
end

#write(output, css: nil) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/elisp/parser.rb', line 147

def write(output, css: nil)
  css = css ? %(<link rel="stylesheet" href="#{CGI.escape(css)}">) : DEFAULT_CSS
  body = @nodes.map(&:html).join("\n")
  output.write(<<~END_HTML)
    <!doctype html>
    <html>
      <head>
        #{css}
        <meta charset="utf8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
      </head>
      <body>
        <main>#{body}</main>
      </body>
    </html>
  END_HTML
end

#write_pandoc_json(output) ⇒ Object



173
174
175
176
# File 'lib/elisp/parser.rb', line 173

def write_pandoc_json(output)
  json = JSON.dump(pandoc_json_object)
  output.write(json)
end