Module: TemplateUtils

Defined in:
lib/template_utils.rb

Overview

template_utils.rb Copyright © Rémi Even 2016

This file is part of Xolti.

Xolti is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Xolti is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Xolti. If not, see <www.gnu.org/licenses/>.

Class Method Summary collapse

Class Method Details

.create_detection_regexp_for_line(template_line) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/template_utils.rb', line 56

def TemplateUtils.create_detection_regexp_for_line(template_line)
  tokens = split_template_tokens_from_line(template_line)
  regexpTokens = tokens.map do |token|
    if tag?(token) then create_regexp_for_tag(token) else Regexp.escape(token) end
  end
  Regexp.new("(#{regexpTokens.join(")(")})")
end

.create_regexp_for_tag(tag) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/template_utils.rb', line 72

def TemplateUtils.create_regexp_for_tag(tag)
  case extract_tag_type(tag)
  when "year"
    "?<year>[[:digit:]]{4}"
  when "author"
    "?<author>.*"
  when "project_name"
    "?<project_name>.*"
  when "file_name"
    "?<file_name>.*"
  else
    ".*"
  end
end

.extract_tag_type(tag) ⇒ Object



68
69
70
# File 'lib/template_utils.rb', line 68

def TemplateUtils.extract_tag_type(tag)
  tag[2..-2]
end

.find_template_tokens_indexes(template_line) ⇒ Object

Return the positions of every (alternating) % and } in template_line



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/template_utils.rb', line 21

def TemplateUtils.find_template_tokens_indexes(template_line)
  indexes = []
  searchedChar = "%"
  for i in 0..template_line.length - 1
    if template_line[i].chr == searchedChar
      indexes.push(i)
      searchedChar = searchedChar == "%" ? "}" : "%"
    end
  end
  indexes
end

.split_template_tokens_from_line(template_line) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/template_utils.rb', line 33

def TemplateUtils.split_template_tokens_from_line(template_line)
  tokens = []
  currentTokenStart = 0
  currentTokenEnd = 0
  inTag = false
  while currentTokenEnd < template_line.length do
    if !inTag && template_line[currentTokenEnd].chr == "%"
      if (currentTokenEnd != currentTokenStart)
        tokens.push(template_line[currentTokenStart..(currentTokenEnd - 1)])
      end
      currentTokenStart = currentTokenEnd
      inTag = true
    elsif inTag && template_line[currentTokenEnd].chr == "}"
      tokens.push(template_line[currentTokenStart..currentTokenEnd])
      currentTokenStart = currentTokenEnd + 1
      inTag = false
    end
    currentTokenEnd += 1
  end
  tokens.push(template_line[currentTokenStart..-1]) unless currentTokenStart == template_line.length
  tokens
end

.tag?(token) ⇒ Boolean



64
65
66
# File 'lib/template_utils.rb', line 64

def TemplateUtils.tag?(token)
  token[0] == "%"
end