Module: Jekyll::Minibundle::VariableTemplate::Parser

Defined in:
lib/jekyll/minibundle/variable_template.rb

Class Method Summary collapse

Class Method Details

.make_close_tag_regexpObject



92
93
94
# File 'lib/jekyll/minibundle/variable_template.rb', line 92

def self.make_close_tag_regexp
  @_close_tag_regexp ||= Regexp.compile(Regexp.escape(CLOSE_TAG))
end

.make_escape_sequence_or_open_tag_regexpObject



84
85
86
87
88
89
90
# File 'lib/jekyll/minibundle/variable_template.rb', line 84

def self.make_escape_sequence_or_open_tag_regexp
  @_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_regexpObject

rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



78
79
80
81
82
# File 'lib/jekyll/minibundle/variable_template.rb', line 78

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

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

Raises:

  • (ArgumentError)


39
40
41
42
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
# File 'lib/jekyll/minibundle/variable_template.rb', line 39

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