Module: Jekyll::Minibundle::VariableTemplate::Parser
- Defined in:
- lib/jekyll/minibundle/variable_template.rb
Class Method Summary collapse
- .make_close_tag_regexp ⇒ Object
- .make_escape_sequence_or_open_tag_regexp ⇒ Object
- .make_escape_sequence_regexp ⇒ Object
- .parse(template) ⇒ Object
Class Method Details
.make_close_tag_regexp ⇒ Object
97 98 99 |
# File 'lib/jekyll/minibundle/variable_template.rb', line 97 def self.make_close_tag_regexp @_make_close_tag_regexp ||= Regexp.compile(Regexp.escape(CLOSE_TAG)) end |
.make_escape_sequence_or_open_tag_regexp ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/jekyll/minibundle/variable_template.rb', line 89 def self.make_escape_sequence_or_open_tag_regexp @_make_escape_sequence_or_open_tag_regexp ||= begin regexp = [make_escape_sequence_regexp.join('|'), Regexp.escape(OPEN_TAG)].map { |p| "(#{p})" }.join('|') Regexp.compile(regexp) end end |
.make_escape_sequence_regexp ⇒ Object
83 84 85 86 87 |
# File 'lib/jekyll/minibundle/variable_template.rb', line 83 def self.make_escape_sequence_regexp escape_chars = (OPEN_TAG + CLOSE_TAG).chars.uniq escape_chars << ESCAPE_CHAR escape_chars.map { |c| Regexp.escape(ESCAPE_CHAR + c) } end |
.parse(template) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 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 |
# File 'lib/jekyll/minibundle/variable_template.rb', line 43 def self.parse(template) raise ArgumentError, 'Nil template' if template.nil? escape_or_open_regex = Parser.make_escape_sequence_or_open_tag_regexp close_regex = Parser.make_close_tag_regexp scanner = StringScanner.new(template) tokens = [] text_buffer = '' escape_or_open_match = scanner.scan_until(escape_or_open_regex) while escape_or_open_match escape_match = scanner[1] # escape sequence if escape_match text_buffer += escape_or_open_match[0..-3] text_buffer += escape_match[1, 1] # open tag else text_buffer += escape_or_open_match[0..-(OPEN_TAG.size + 1)] tokens << Token.text(text_buffer) text_buffer = '' close_match = scanner.scan_until(close_regex) raise SyntaxError.new(%{Missing closing tag ("#{CLOSE_TAG}") for variable opening tag ("#{OPEN_TAG}")}, template, scanner.charpos) unless close_match tokens << Token.variable(close_match[0..-(CLOSE_TAG.size + 1)].strip) end escape_or_open_match = scanner.scan_until(escape_or_open_regex) end text_buffer += scanner.rest unless scanner.eos? tokens << Token.text(text_buffer) unless text_buffer.empty? tokens end |