Module: IdSubstitutions

Included in:
Id
Defined in:
lib/id-substitutions.rb

Instance Method Summary collapse

Instance Method Details

#sub(line) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/id-substitutions.rb', line 3

def sub line
  return line if not line
  unless @raw
    ref = ""
    if @references.find_index(line[0])
      ref = line[0]
      line = line[1..-1]
    end
    datetime = Time.new.to_s.split()[0..1]
    date = datetime[0]
    time = datetime[1][/[0-9]+:[0-9]+/]
    hour = time[/[0-9]+/]
    # ? substitutions
    line.sub! "?d", today
    line.sub! "?t", time
    line.sub! "?h", hour
    line.sub! "?", @node
    # $ substitutions
    return line if line == "$"
    if line[0] == "$"
      line = line[1..-1] if line[1..-1]
      line = @nodes[line] if @nodes[line]
      line = line[0] if line[0]
    end
    if line["*"]
      @nodes[@node].each do |l|
        if l.start_with? ref + line[0...line.index("*")]
          line = line.gsub(/.*\*/, l)
          break 
        end
      end
    end
    return ref + line
  else
    return line
  end
end