-
Notifications
You must be signed in to change notification settings - Fork 6
/
generate.rb
executable file
·80 lines (65 loc) · 2.76 KB
/
generate.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
#!/usr/bin/env ruby
require 'optparse'
require 'yaml'
require 'erb'
require 'ostruct'
options = {}
optparse = OptionParser.new do |opts|
opts.banner = "Usage: generate.rb [options]"
options[:input] = 'resume.yml'
opts.on( '-i', '--input INPUT', 'input file' ) do |input|
options[:input] = input
end
options[:private] = 'private.yml'
opts.on( '-p', '--private PRIVATE', 'private input file' ) do |private|
options[:private] = private
end
options[:template] = 'html'
opts.on( '-t', '--template TEMPLATE', 'template for extension' ) do |template|
options[:template] = template
end
options[:web] = false
opts.on( '-w', '--web', 'create a web version omitting private information' ) do |w|
options[:web] = true
end
end.parse!
# Semi-hack to pass the correct bindings to ERB
class ErbBinding < OpenStruct
def get_binding
return binding()
end
end
# Does the resume file exist?
abort("Error: #{options[:input]} is not present in this directory. Use -i or --input to specify another input file.") unless File.exist?( options[:input] )
# Load the resume YAML files
resume = YAML::load( File.open(options[:input]) )
# Does the private input file exist?
abort("Error: #{options[:private]} is not present in this directory. Use -p or --private to specify another private input file.") unless File.exist?( options[:private] ) or options[:web]
# Does the template file exist?
template_file = File.join( File.dirname(__FILE__), 'templates', options[:template] + '.erb')
abort("Error: template for #{options[:template]} does not exist. Use -t or --template to specify another template file.") unless File.exist?(template_file)
input_basename, input_extension = options[:input].split('.')
if options[:web]
output_file = input_basename + '-web.' + options[:template]
else
output_file = input_basename + '.' + options[:template]
# Load and merge contact information (for full resume)
# private.yml contains contact information I don't want posted
private = YAML::load( File.open(options[:private]) )
resume["contact"] = resume["contact"].merge(private["contact"])
end
# Load the escape function and run it on resume hash (if escape function exists)
escape_defn = File.join( File.dirname(__FILE__), 'escape', options[:template] + '.rb' )
require escape_defn if File.exist?(escape_defn)
escape(resume) if defined?(escape)
# Load the ERB template
template = ERB.new( File.new(template_file).read, trim_mode: "<>" )
namespace = ErbBinding.new resume
result = template.result namespace.send(:get_binding)
# Create 'output' directory if it doesn't exist
Dir.mkdir("output") unless File.exist?("output") && File.directory?("output")
# Write to output file
File.open( 'output/' + output_file, "w" ) do |file|
file.write result
end
puts "Created #{output_file}"