forked from schneems/ruby_view_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page_generator.rb
72 lines (54 loc) · 1.7 KB
/
page_generator.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
require 'erb'
def process_erb(string)
template = ERB.new string
return template.result(binding)
end
## Define a function
# def add_thes_together(x,y)
# return x + y
# end
## Replace contents from a string
# str = "i rode a plane today"
# puts str.gsub('rode', 'bought')
# => "i bought a plane today"
## Remove contents from a string
# str = "i rode a plane today"
# puts str.gsub('today', '')
# => "i bought a plane"
## Turn a string into an array
# str = "tigers,lions,bears,ohmy"
# puts str.split(',')
# ["tigers", "lions", "bears", "ohmy"]
## Get the last element from an array
# arry = [1,2,3,4,5]
# puts arry.last
# => 5
## Read a file
# File.open('file_name', 'r').read
## Write a file
# File.open('file_name', 'w') do |f|
# f.write('new contents')
# end
## Iterate over all files in a directory matching a pattern
# Dir['views/*.html.erb'].each do |file|
# puts file`
# end
## Here the * character represents a wildcard
puts "================================================"
puts "=== Converting files in /views to html ========="
puts "================================================"
Dir['views/*.html.erb'].each do |file|
puts "- Reading #{file}"
content_string = File.open(file, 'r').read
file_name = file.split('/').last.gsub('html.erb', 'html')
output_file = "public/#{file_name}"
puts " - Converting .html.erb to html"
main_contents = process_erb(content_string)
puts " - Writing #{output_file}"
File.open(output_file, 'w') do |f|
f.write(main_contents)
end
end
puts "================================================"
puts "=== Done, open files in /public with browser ==="
puts "================================================"