-
Notifications
You must be signed in to change notification settings - Fork 10
/
resume.py
97 lines (76 loc) · 2.31 KB
/
resume.py
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
"""
Generate pdf resume from markdown and css.
"""
import argparse
import codecs
import os
import pdfkit
from jinja2 import Environment, PackageLoader, Markup
from markdown2 import markdown
#from subprocess import call
def parse_args():
parser = argparse.ArgumentParser(
description='Generate pdf resume from markdown and css',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
'--css',
dest='css',
default='src/css/default.css',
help='specify the path of the CSS file'
)
parser.add_argument(
'-m',
'--markdown',
dest='markdown',
default='src/md/john-cadengo.md',
help=('specify the path of the MARKDOWN file')
)
args = parser.parse_args()
return args
def generate_markdown(mdfile):
"""
Generate html from a markdown file at the specified path.
"""
f = codecs.open(mdfile, encoding='utf-8')
html = Markup(markdown(f.read()))
f.close()
return html
def create_template(content, css):
"""
Plug the html content into the body of the template.
"""
env = Environment(loader=PackageLoader('resume', '.'))
template = env.get_template('src/base.html')
return template.render(content=content, css=css)
def main():
args = parse_args()
content = generate_markdown(args.markdown)
template = create_template(content, args.css)
filename = os.path.basename(args.markdown) # john-cadengo.md
name = os.path.splitext(filename)[0] # john-cadengo
if not os.path.exists('build/html'):
os.makedirs('build/html')
html = 'build/html/{}.html'.format(name)
f = codecs.open(html, 'w', encoding='utf-8')
f.write(template)
f.close()
# Generate a png
#if not os.path.exists('build/png'):
# os.makedirs('build/png')
#call(['wkhtmltoimage', html,
# './build/png/{}.png'.format(name)])
# Generate the pdf
if not os.path.exists('build/pdf'):
os.makedirs('build/pdf')
options = {
'margin-top': '0.0',
'margin-right': '0.0',
'margin-bottom': '0.0',
'margin-left': '0.0',
'encoding': "UTF-8",
'page-size': 'Letter'
}
pdfkit.from_file(html, './build/pdf/{}.pdf'.format(name), options=options)
if __name__ == '__main__':
main()