-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.rb
executable file
·96 lines (77 loc) · 2.51 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env ruby
require 'erb'
require 'yaml'
require 'config'
require 'rainbow/refinement'
require_relative 'string_extensions'
require_relative 'latex_helpers'
using Rainbow
# Directory constants relative to script location
BUILD_DIR = File.join(__dir__, 'build')
CONFIG_DIR = File.join(__dir__, 'config')
OUTPUT_DIR = File.join(__dir__, 'output')
CONTENTS_DIR = File.join(__dir__, 'contents')
TEX_DIR_NAME = 'tex'
PDF_DIR_NAME = 'pdf'
include LatexHelpers
def ensure_output_directories(content_name)
output_dir = File.join(OUTPUT_DIR, content_name)
tex_dir = File.join(output_dir, TEX_DIR_NAME)
pdf_dir = File.join(output_dir, PDF_DIR_NAME)
[BUILD_DIR, OUTPUT_DIR, output_dir, tex_dir, pdf_dir].each do |dir|
Dir.mkdir(dir) unless Dir.exist?(dir)
end
[tex_dir, pdf_dir]
end
def generate_pdf(tex_file, pdf_dir)
command = [
"pdflatex",
"-interaction nonstopmode",
"-aux-directory \"#{BUILD_DIR}\"",
"-output-directory \"#{pdf_dir}\"",
"\"#{tex_file}\""
].join(' ')
if system("#{command} > #{BUILD_DIR}/latex.log 2>&1")
puts "PDF Generated: #{pdf_dir}".green
true
else
puts "Error generating PDF - check #{BUILD_DIR}/latex.log for details".red
false
end
end
def render_content(content_file)
content_name = File.basename(content_file, '.yml')
yaml_content = YAML.load_file(content_file)
# Load YAML content into instance variables for ERB
@title = yaml_content['title']
@about = yaml_content['about']
@info = yaml_content['info']
@technical = yaml_content['technical']
@education = yaml_content['education']
@experiences = yaml_content['experiences']
tex_dir, pdf_dir = ensure_output_directories(content_name)
template = File.read('template.tex.erb')
Dir.glob(File.join(CONFIG_DIR, "*.yml")) do |config_file|
config_name = File.basename(config_file, '.yml')
puts "Generating #{config_name}...".yellow
begin
Config.load_and_set_settings(
File.join(CONFIG_DIR, 'default.yml'),
config_file
)
puts Settings.to_s.faint
renderer = ERB.new(template, trim_mode: '-')
latex = renderer.result()
tex_file = File.join(tex_dir, "#{config_name}.tex")
File.write(tex_file, latex)
puts "Tex Generated: #{tex_file}".green
generate_pdf(tex_file, pdf_dir)
rescue StandardError => e
puts "Error processing #{config_name}: #{e.message}".red
end
end
end
# Process all YAML files in contents directory
Dir.glob(File.join(CONTENTS_DIR, "*.yml")) do |file|
render_content(file)
end