-
Notifications
You must be signed in to change notification settings - Fork 16
/
h2h.py
executable file
·72 lines (60 loc) · 2.14 KB
/
h2h.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
#!/usr/bin/python3
import argparse
import os.path
# import cProfile
from vimh2h import VimH2H
SECTION_BEGIN = r"""\addcontentsline{toc}{%s}{%s}
\markright{%s}
\begin{Verbatim}[commandchars=\\\{\},formatcom=\fixurl]
"""
CHAPTER_BEGIN = r"""\addcontentsline{toc}{%s}{%s}
\phantomsection{}
"""
SECTION_END = """
\\end{Verbatim}
\\cleardoublepage\\phantomsection{}
"""
DOC_END = r"""
\addcontentsline{toc}{%s}{About this pdf}
\markright{about this pdf}
"""
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--faq', dest='faq', action='store_true')
parser.add_argument('--no-faq', dest='faq', action='store_false')
parser.set_defaults(faq=True)
args = parser.parse_args()
print("Processing tags...")
with open(os.path.join('doc', 'tags'), 'r') as input_file:
h2h = VimH2H(input_file.read())
if args.faq:
with open(os.path.join('doc', 'vim_faq.txt'), 'r') as input_file:
h2h.add_tags(input_file.read())
with open('contents.txt', 'r') as input_file:
contents = input_file.read().split('\n')
with open('body.tex', 'w') as output_file:
level = "chapter"
for row in contents:
split_row = row.strip().split(None, 1)
if len(split_row) != 2:
continue
filename, title = split_row
if not args.faq and filename == 'vim_faq.txt':
continue
if filename == "#":
output_file.write(CHAPTER_BEGIN % ("chapter", title))
level = "section"
continue
if filename == "##":
output_file.write(CHAPTER_BEGIN % ("section", title))
level = "subsection"
continue
print("Processing " + filename + "...")
output_file.write(SECTION_BEGIN % (level, title, filename.replace('_', r'\_')))
with open(os.path.join('doc', filename), 'r') as input_file:
text = input_file.read()
output_file.write(h2h.to_tex(filename, text, args.faq))
output_file.write(SECTION_END)
output_file.write(DOC_END % level)
main()
# cProfile.run('main()')