-
Notifications
You must be signed in to change notification settings - Fork 0
/
tata.rb
executable file
·170 lines (141 loc) · 4.43 KB
/
tata.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env ruby
# The Amazing Template Assembler by Jørn Kinderås - 2012
# Takes a bunch of HTML files and assembles them into the
# html file of your choice. For more information see the README file
# SETTINGS [Edit these...and yes they are using relative paths]
$template_dir = "directory/templates/"
$html_target_file = "directory/index.html"
# END SETTING
def tata(watch, compressed=false)
# validate that the template directory
# and the html file actually exists
if not File.directory? $template_dir
abort($template_dir + " is not a valid directory")
end
if not File.exists? $html_target_file or not File.extname($html_target_file) == '.html'
abort($html_target_file + ' is not a valid html file');
end
# make sure we have a valid directory path
$template_dir += "/" if $template_dir[-1,1] != '/'
# id pattern used for detecting a template file
pattern = /<!-- id=(.+) -->/
# Get the template files
files = Array.new
Dir.new($template_dir).entries.each { |n| files.push($template_dir + n) if File.extname(n) == '.html' }
# Holder for the templates
templates = "<!-- TEMPLATES -->\n"
# File that where processed
tplFiles = Array.new
# Does any of the files have changes
hasChanges = false
# init
templateText = ""
tplDetected = false
id = ""
# For each filename
files.each do |f|
# For each file
File.open(f) do |file|
templateText = ""
tplDetected = false
# For each line
file.each_line do |line|
# Test if this file is a valid template
if line =~ pattern
# A matching pattern was found
tplDetected = true
# Save the filenames for output
tplFiles.push(file.path)
# Grab the template id
id = line.scan(pattern)[0]
# Start to build the template
templateText += '<script id="' + id[0] + '" type="text/template">' + "\n"
# Detect if file has changed
if not $changed_files[file.path]
# this is a new file
hasChanges = true
else
if $changed_files[file.path] != file.mtime
# the file has changed
hasChanges = true
end
end
# remember when this file was last modified
$changed_files[file.path] = file.mtime
else
# add the rest of the template content
if compressed
templateText += line.strip
else
templateText += line
end
end # For each line end
end
# close the file
file.close
# wrap up the template
templateText += "\n</script>\n"
# If this file is a template
if tplDetected
templates += templateText
elsif $changed_files[file.path]
# This file was a template but has
# been removed, stop processing it
$changed_files.delete(file.path)
hasChanges = true
puts "Removed " + file.path
end
end # For each file end
end # For each filename end
# warp up the templates
templates += "<!-- TEMPLATES END -->\n"
# Get the content of the index file
hf = File.open($html_target_file, 'rb')
html = hf.read
hf.close
# Remove any previous templates
html.slice!(/<!-- TEMPLATES -->(.|\n|\r)+<!-- TEMPLATES END -->/)
# Just to be sure that old template code is gone
html.slice!(/<script(.+)text\/template">(.|\n|\r)+<\/script>/)
# Remove any extra newlines
html.gsub!(/\n\n+/, "\n")
# Remove any junk if all templates where removed
templates = "" if templates.lines.count < 3
# And insert the [new] templates
index = html.index(/<\/[B|b][o|O][d|D][y|Y]>/)
html.insert(index, templates)
# If there wasn't any changes, go again (if watch is enabled)
if not hasChanges and watch
sleep(1)
tata(watch, compressed)
end
# write the changes to the html file
f = File.open($html_target_file, 'w')
f.write(html)
f.close
# Tell the user what just happened
puts "The following templates where added to " + $html_target_file
puts tplFiles
puts "\n"
# If watch is enabled, go again
if watch
sleep(1)
tata(watch, compressed)
end
end # End of method
def init
watch = false
compress = false
ARGV.each do |a|
case a
when '-w'
watch = true
when '-compressed'
compress = true
end
end
tata(watch, compress)
end
# Start the app
$changed_files = Hash.new
init