-
Notifications
You must be signed in to change notification settings - Fork 0
/
cv.rb
executable file
·151 lines (122 loc) · 4.72 KB
/
cv.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
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'yaml'
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
def section_replace_scalars(section, replacements)
if replacements.instance_of?(String) && section.include?('{list_item}')
section.gsub('{list_item}', "#{replacements}\n")
elsif section.include? '{list_item}'
details = ''
replacements.each do |x|
copy = section
copy = copy.gsub('{list_item}', "#{x.strip}\n")
details += copy
end
details
else
matches = section.scan(/\{(.*?)\}/i)
matches.each do |x|
section = section.gsub("{#{x[0]}}", "#{replacements[x[0]].strip}\n")
end
section
end
end
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
def references(template_html, cv_yaml)
match = template_html.scan(%r{<references>(.*?)</references>}ms)
replacement = ''
cv_yaml['references'].each do |x|
replacement += section_replace_scalars(match[0][0], x)
end
template_html.gsub(%r{<references>(.*?)</references>}ms, replacement)
end
# rubocop:disable Metrics/MethodLength, Metrics/BlockLength, Metrics/AbcSize
def pages(template_html, cv_yaml)
(0..cv_yaml['pages'].length - 1).each do |count|
x = (count + 1).to_s
match = template_html.scan(%r{<page#{x}>(.*?)</page#{x}>}ms)
page = cv_yaml['pages'][count]
jobs = page['jobs']
replacement = ''
jobs.each do |job|
job_section = match[0][0]
role_match = job_section.scan(%r{<roles>(.*?)</roles>}ms)
roles_replacement = ''
job['roles'].each do |role|
role_section = role_match[0][0]
details_match = job_section.scan(%r{<details>(.*?)</details>}m)
details_replacement = ''
role['details'].each do |detail|
detail_copy = section_replace_scalars(details_match[0][0], detail)
details_replacement += detail_copy
end
role_section = role_section.gsub(%r{<details>(.*?)</details>}ms, details_replacement)
role_section = section_replace_scalars(role_section, role)
roles_replacement += role_section
end
job_section = job_section.gsub(%r{<roles>(.*?)</roles>}ms, roles_replacement)
job_section = section_replace_scalars(job_section, job)
replacement += job_section
end
template_html = template_html.gsub(%r{<page#{x}>(.*?)</page#{x}>}ms, replacement)
end
template_html
end
# rubocop:enable Metrics/MethodLength, Metrics/BlockLength, Metrics/AbcSize
def roles(template_html, cv_yaml)
template_html = template_html.gsub(/{current-role}/ms, cv_yaml['roles'][0])
match = template_html.scan(%r{<previous-roles>(.*?)</previous-roles>}ms)
rows = ''
(1..cv_yaml['roles'].length - 1).each do |count|
next if count.zero?
row = match[0][0]
row = section_replace_scalars(row, cv_yaml['roles'][count])
rows += row
end
template_html.gsub(%r{<previous-roles>(.*?)</previous-roles>}ms, rows)
end
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/BlockLength, Metrics/AbcSize
def replace_one(template_html, cv_yaml)
cv_yaml.each do |x, value|
if value.instance_of?(String)
replacement = value.gsub('\\n', '<br/>') if value.include? '\\n'
template_html = template_html.gsub("{#{x}}", replacement)
else
match_instance = template_html.scan(%r{<#{x}>(.*?)</#{x}>}ms)
next if match_instance.instance_of?(Array) == false
next if match_instance.empty?
list_replacement = ''
cv_yaml[x].each do |y|
copy = match_instance[0][0]
if y.instance_of?(String) == false
if y.include? 'list'
match = copy.scan(%r{<children>(.*?)</children>}ms)
sublist = section_replace_scalars(match[0][0], y['list'])
copy = copy.gsub(%r{<children>(.*?)</children>}ms, sublist)
copy = section_replace_scalars(copy, y['item'])
else
copy = copy.gsub(%r{{list_item}.*?<ul.*?</ul>}ms, y['item'])
end
list_replacement += copy
elsif y.instance_of?(String)
copy = section_replace_scalars(copy, y)
list_replacement += copy
end
end
template_html = template_html.gsub(%r{<#{x}>(.*?)</#{x}>}ms, list_replacement)
end
end
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/BlockLength, Metrics/AbcSize
def replace
cv_details = ENV['cv_file']
template = ENV['template_file']
out_html = ENV['output_file']
template_html = File.read(template)
cv_yaml = YAML.safe_load(File.read(cv_details))
template_html = references(template_html, cv_yaml)
template_html = pages(template_html, cv_yaml)
template_html = roles(template_html, cv_yaml)
File.write(out_html, template_html)
end
replace