module Jekyll::Minibundle::VariableTemplate::Parser

Public Class Methods

make_close_tag_regexp() click to toggle source
   # File lib/jekyll/minibundle/variable_template.rb
95 def self.make_close_tag_regexp
96   @_make_close_tag_regexp ||= Regexp.compile(Regexp.escape(CLOSE_TAG))
97 end
make_escape_sequence_or_open_tag_regexp() click to toggle source
   # File lib/jekyll/minibundle/variable_template.rb
87 def self.make_escape_sequence_or_open_tag_regexp
88   @_make_escape_sequence_or_open_tag_regexp ||=
89     begin
90       regexp = [make_escape_sequence_regexp.join('|'), Regexp.escape(OPEN_TAG)].map { |p| "(#{p})" }.join('|')
91       Regexp.compile(regexp)
92     end
93 end
make_escape_sequence_regexp() click to toggle source
   # File lib/jekyll/minibundle/variable_template.rb
81 def self.make_escape_sequence_regexp
82   escape_chars = (OPEN_TAG + CLOSE_TAG).chars.uniq
83   escape_chars << ESCAPE_CHAR
84   escape_chars.map { |c| Regexp.escape(ESCAPE_CHAR + c) }
85 end
parse(template) click to toggle source
   # File lib/jekyll/minibundle/variable_template.rb
41 def self.parse(template)
42   raise ArgumentError, 'Nil template' if template.nil?
43 
44   escape_or_open_regex = Parser.make_escape_sequence_or_open_tag_regexp
45   close_regex = Parser.make_close_tag_regexp
46 
47   scanner = StringScanner.new(template)
48 
49   tokens = []
50   text_buffer = ''
51   escape_or_open_match = scanner.scan_until(escape_or_open_regex)
52 
53   while escape_or_open_match
54     escape_match = scanner[1]
55 
56     # escape sequence
57     if escape_match
58       text_buffer += escape_or_open_match[0..-3]
59       text_buffer += escape_match[1, 1]
60     # open tag
61     else
62       text_buffer += escape_or_open_match[0..-(OPEN_TAG.size + 1)]
63       tokens << Token.text(text_buffer)
64       text_buffer = ''
65       close_match = scanner.scan_until(close_regex)
66 
67       raise SyntaxError.new(%{Missing closing tag ("#{CLOSE_TAG}") for variable opening tag ("#{OPEN_TAG}")}, template, scanner.charpos) unless close_match
68 
69       tokens << Token.variable(close_match[0..-(CLOSE_TAG.size + 1)].strip)
70     end
71 
72     escape_or_open_match = scanner.scan_until(escape_or_open_regex)
73   end
74 
75   text_buffer += scanner.rest unless scanner.eos?
76   tokens << Token.text(text_buffer) unless text_buffer.empty?
77 
78   tokens
79 end