-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
281 lines (217 loc) · 8.78 KB
/
main.py
File metadata and controls
281 lines (217 loc) · 8.78 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Generator (build process) for project documentation as a static HTML WebPage
Generator (build process) for project documentation as a static HTML WebPage
Calls other scripts "exportPngFromDiagram.py" and "exportPdfFromHtml.py".
Expects following directory tree:
project_root_directory
├── _www ........ temporary, HTML output of documentation generator
├── content ..... here are all the Markdown files and additional files,
│ │ e.g. screenshots, PDFs
│ ├── any_markdown.md
│ ├── screenshot.png
│ ├── datasheet.pdf
│ └── calculation.xlsx
├── originals ... here are all diagramms which get exported to PNGs
│ ├── diagram.drawio
│ └── diagram.umlet
├── exportPdfFromHtml.py ...... Python exporter script for PDFs from HTML
├── exportPngFromDiagram.py ... Python exporter script for diagrams
├── main.py ................... Python doc generator script
├── page_template.html ........ HTML template for document generator
├── print.css ................. stylesheet for print layout of documentation
├── README.md ................. this file
└── requirements.txt .......... Python requirements for additional libs
It is strongly recommended to setup all necessary parameters in the
init() function of the file config.py, before running it for the first time."""
import os
import shutil
import subprocess
import sys
import markdown
from config import init
import exportPngFromDiagram
import exportPdfFromHtml
# ---------------------------
def cleanup(parameters):
aPath = parameters['webDir']
if os.path.isdir(aPath):
shutil.rmtree(aPath)
print("DEBUG: cleanup()")
# ---------------------------
def buildDirLayout(parameters):
os.makedirs(parameters['webDir'])
# copy page.css file if any exists
filepath = os.path.join(parameters['projectRootDir'], 'page.css')
if os.path.exists(filepath):
shutil.copy(filepath, parameters['webDir'])
# copy print.css file if any exists
filepath = os.path.join(parameters['projectRootDir'], 'print.css')
if os.path.exists(filepath):
shutil.copy(filepath, parameters['webDir'])
print("DEBUG: buildDirLayout()")
# ---------------------------
def load_page_template(parameters):
fileName = os.path.join(parameters['projectRootDir'], "page_template.html")
with open(fileName, 'r') as fileObject:
parameters['pageTemplate'] = fileObject.read()
print("DEBUG: load_page_template()")
# ---------------------------
def create_html_page(parameters):
html_content = None
md_all_content = []
# concatenate all Markdown documents in the right sequence
for md_file in parameters['documentSequence']:
md_file = os.path.join(parameters['contentDir'], md_file + ".md")
with open(md_file, 'r') as fileObject:
md_content = fileObject.read()
md_all_content.append(md_content)
# Initialize Markdown-Builder
mdBuilder = markdown.Markdown(
extensions= ['markdown.extensions.extra', 'markdown.extensions.toc'],
extension_configs= { 'markdown.extensions.toc' : { 'toc_depth' : 2 } },
output_format = "html5",
tab_length = 4,
smart_emphasis = True
)
# save Markdown file
md_content = ("\n").join(md_all_content)
md_filepath = os.path.join(parameters['webDir'], parameters['documentName'] + ".md")
with open(md_filepath, 'w') as fileObject:
fileObject.write(md_content)
# generate HTML and save it to file
# and add temporarily TOC statement for HTML and PDF generation
html_content = mdBuilder.convert(parameters['tocString'] + md_content)
pageParameters = dict (
TITLE = parameters['title'],
CONTENT = html_content,
CURRENTYEAR = parameters['currentYear'],
GENERATED = parameters['generated'],
GENERATED_HUMANREADABLE = parameters['generatedHumanReadable']
)
html_content = parameters['pageTemplate'].format(**pageParameters)
html_filepath = os.path.join(parameters['webDir'], parameters['documentName'] + ".html")
with open(html_filepath, 'w') as fileObject:
fileObject.write(html_content)
print("DEBUG: create_page( " + parameters['documentName'] + " )")
# ---------------------------
def handle_all_content_files(parameters):
contentDir = parameters['contentDir']
files = os.listdir(contentDir)
for filename in files:
filepath = os.path.join(contentDir, filename)
if os.path.isfile(filepath):
if filename.endswith(".md"):
pass
else:
# copy any matching file
shutil.copy(filepath, parameters['webDir'])
elif os.path.islink(filepath):
# filter out symbolic links
pass
elif os.path.isdir(filepath):
# copy any matching subdir
shutil.copytree(filepath, os.path.join(parameters['webDir'], filename))
print("DEBUG: handle_all_content_files()")
# ---------------------------
def handle_all_diagram_files():
exportPngFromDiagram.run()
print("DEBUG: handle_all_diagram_files()")
# ---------------------------
def handle_all_pdf_files():
exportPdfFromHtml.run()
print("DEBUG: handle_all_pdf_files()")
# ---------------------------
def handle_all_docx_files(parameters):
md_filepath = os.path.join(parameters['webDir'], parameters['documentName'] + ".md")
word_filepath = os.path.join(parameters['webDir'], parameters['documentName'] + ".docx")
user_home_path = os.path.join(
os.environ['HOMEDRIVE'], os.sep, os.environ['HOMEPATH'])
apps_installation_root = os.path.join(
user_home_path, "installations")
pandoc_path = os.path.join(
apps_installation_root,
"pandoc",
"pandoc.exe"
)
command = [
pandoc_path,
"--table-of-contents",
"--toc-depth=2",
"--from=markdown+footnotes",
md_filepath,
"--to=docx",
"--reference-doc",
os.path.join(parameters['projectRootDir'], 'custom-reference.docx'),
"--output",
word_filepath,
"--resource-path",
parameters['webDir']
]
subprocess.run(command)
print("DEBUG: handle_all_docx_files()")
# ---------------------------
def show_usage():
usage = ["NAME:",
" main.py", "",
"SYNOPSIS:",
" main.py [--help] [--diagram] [--pdf] [--docx]", "",
"DESCRIPTION:",
" Generator (build process) for project documentation as a static HTML WebPage", "",
"OPTIONS:",
" --help shows this information", "",
" --diagram calls script 'exportPngFromDiagram.py' during build process",
" to generate diagrams out of diagramming files", "",
" --pdf calls script 'exportPdfFromHtml.py' during build process",
" to generate PDFs out of generated HTML files",
" --docx generates MS-Word docx out of markdown file"
]
print("\n".join(usage))
# ---------------------------
def handle_cli_parameters():
print("DEBUG: handle_cli_parameters()")
run_with_diagram_generation = False
run_with_pdf_generation = False
run_with_docx_generation = False
if len(sys.argv) > 1:
for pos in range(1, len(sys.argv)):
parameter = sys.argv[pos].lower()
if parameter == "--help":
show_usage()
exit(1)
elif parameter == "--diagram":
run_with_diagram_generation = True
elif parameter == "--pdf":
run_with_pdf_generation = True
elif parameter == "--docx":
run_with_docx_generation = True
else:
print("ERROR: wrong cli parameter!")
show_usage()
exit(2)
return run_with_diagram_generation, run_with_pdf_generation, run_with_docx_generation
# ---------------------------
def run():
cli_with_diagrams, cli_with_pdfs, cli_with_docx = handle_cli_parameters()
parameters = init()
cleanup(parameters)
buildDirLayout(parameters)
load_page_template(parameters)
if cli_with_diagrams:
print("DEBUG: cli_with_diagrams")
handle_all_diagram_files()
create_html_page(parameters)
handle_all_content_files(parameters)
if cli_with_docx:
print("DEBUG: cli_with_docx")
handle_all_docx_files(parameters)
if cli_with_pdfs:
print("DEBUG: cli_with_pdfs")
handle_all_pdf_files()
print("DEBUG: run()")
# ---------------------------
# MAIN
# ---------------------------
if __name__ == '__main__':
run()
print("DEBUG: main() done")