forked from machine/machine.specifications
-
Notifications
You must be signed in to change notification settings - Fork 4
/
quicktemplate.rb
48 lines (37 loc) · 881 Bytes
/
quicktemplate.rb
1
2
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
40
41
42
43
44
45
46
47
48
require 'rake'
require 'erb'
class QuickTemplate
attr_reader :args, :file
def initialize(file)
raise "The template file to process must be given" if file.nil?
@file = file
end
def exec(args = {})
template = prepare_template File.read(@file)
result = exec_erb template, args
resultFile = @file.ext('')
File.open(resultFile, 'w') do
|f| f.write(result)
end
puts "Created file #{resultFile}"
end
def prepare_template(template)
tag_regex = /(@\w[\w\.]+\w@)/
hits = template.scan(tag_regex)
tags = hits.map do |item|
item[0].chomp('@').reverse.chomp('@').reverse.strip
end
tags.map! do |a|
a.to_sym
end
tags.uniq!
tags.inject(template) do |text, tag|
text.gsub /@#{tag.to_s}@/, "<%= #{tag.to_s} %>"
end
end
def exec_erb(template, args)
b = binding
erb = ERB.new(template, 0, "%")
erb.result(b)
end
end