Debian Codegraph @

android/app/jni/res/docpdf/make_paper.py

raw
1 #!/usr/bin/env python3
2
3 import os
4 import sys
5
6 import markdown
7 from mdx_gfm import GithubFlavoredMarkdownExtension
8
9 import weasyprint
10 import re
11
12 import logging
13 logging.basicConfig(format='%(levelname)s: %(message)s' ,stream=sys.stderr, level=logging.INFO)
14 LOGGER = logging.getLogger('preprocess')
15
16 # sort the file order
17 def sort_func(x):
18 # place "papers/" at the end (like an appendix)
19 try:
20 x.index('%sdoc%s' % (os.path.sep, os.path.sep))
21 except ValueError:
22 return 'z'
23
24 # place readmes at the start of each section
25 try:
26 rm = x.index('README.md')
27 return x[:rm] + '0'
28 except ValueError:
29 return x
30
31 # make the links work in-pdf
32 def fix_links(match):
33 # images
34 if os.path.splitext(match.group(2))[-1] == '.png':
35 return '[%s](%s)' % (
36 match.group(1),
37 os.path.join(os.path.split(my_file)[0], match.group(2))
38 )
39
40 # urls to other files
41 BASE_URL = 'https://github.com/tildearrow/furnace/tree/master/'
42 if match.group(2).startswith(BASE_URL):
43 file_path = match.group(2).split(BASE_URL)[-1]
44 if os.path.splitext(file_path)[-1] == '':
45 file_path += '/README.md'
46 return '[%s](#%s)' % (
47 match.group(1),
48 file_path.replace('/','__')
49 )
50
51 # preserve external urls
52 elif match.group(2).startswith('http'):
53 return match.group(0)
54
55 # fix paths
56 act_path = os.path.split(my_file)[0] + '/' + match.group(2)
57 act_path = os.path.relpath(os.path.abspath(act_path))
58 return '[%s](#%s)' % (
59 match.group(1),
60 act_path.replace(os.path.sep,'__')
61 )
62
63 def fix_headings(match):
64 return '%s#' % (
65 match.group(1)
66 )
67
68 if __name__ == "__main__":
69 #-- first, prepare the file list --#
70 file_list = []
71 for i in os.walk('../../doc'):
72 base_dir, subfolders, files = i
73 for file_ in filter(lambda x: x.lower().endswith('.md'), files):
74 file_list.append(os.path.join(base_dir, file_))
75
76 #-- then, create the index --#
77 index = '<h2>contents</h2><ol>'
78
79 #-- then, create the document --#
80 html = ''
81
82 # perform sort
83 file_list.sort(key=sort_func)
84
85 first = True
86
87 for my_file in file_list:
88 pageLink = my_file.replace(os.path.sep, "__")
89
90 if pageLink.endswith("__doc__README.md"):
91 continue
92
93 with open(my_file, 'r') as md:
94 LOGGER.info("processing file %s" % my_file)
95 data = md.read()
96
97 # retrieve title
98 pageTitle = data.partition('\n')[0].replace("# ","")
99
100 if pageLink.endswith("__README.md"):
101 if first:
102 first = False
103 else:
104 index += '</li></ol>'
105
106 index += '<li><a href="#%s" class="indexItemPre">%s</a><a href="#%s" class="indexItem"></a>' % ( pageLink, pageTitle, pageLink )
107 if pageLink.endswith("__README.md"):
108 index += '<ol>'
109 else:
110 index += '</li>'
111
112 # perform link fixing
113 data = re.sub(r'\[(.+?)\]\((.+?)\)', fix_links, data)
114 data = re.sub(r'^\s*(#+)', fix_headings, data, flags=re.MULTILINE)
115
116 # each file is its own section
117 html +='<section id="%s">%s</section>' % (
118 my_file.replace(os.path.sep, "__"),
119 markdown.markdown(data, extensions=['nl2br', 'mdx_breakless_lists', GithubFlavoredMarkdownExtension()])
120 )
121
122 # finish index
123 index += '</ol></li></ol>'
124
125 # build html
126 final_html = ('''
127 <!DOCTYPE html>
128 <html lang="en">
129 <head>
130 <meta charset="utf-8"/>
131 <title>Furnace Manual</title>
132 <style>
133 @font-face {
134 font-family: 'Exo 2';
135 font-style: normal;
136 src: url("fonts/Exo2[wght].ttf") format("truetype");
137 }
138 @font-face {
139 font-family: 'Exo 2';
140 font-style: italic;
141 src: url("fonts/Exo2-Italic[wght].ttf") format("truetype");
142 }
143 @font-face {
144 font-family: 'IBM Plex Mono';
145 font-style: normal;
146 src: url("fonts/IBMPlexMono-Regular.ttf") format("truetype");
147 }
148 body {
149 font-family: 'Exo 2';
150 line-height: 1.25;
151 font-size: 11pt;
152 color: #000;
153 }
154 section {
155 page-break-before:always;
156 text-align: left;
157 hyphens: auto;
158 }
159 code {
160 font-family: 'IBM Plex Mono';
161 }
162 ul {
163 padding-left: 10pt;
164 margin-right: 4pt;
165 list-style-type: none;
166 }
167 ul > li:before {
168 content: '-';
169 padding-right: 3pt;
170 }
171 h1 {
172 hyphens: none;
173 text-align: left;
174 }
175 h2 {
176 hyphens: none;
177 text-align: center;
178 font-size: 36pt;
179 margin: 0;
180 padding: 0 0 4pt 10pt;
181 border-style: none none double none;
182 border-width: 0 0 6pt 0;
183 }
184 h3 {
185 hyphens: none;
186 text-align: left;
187 font-size: 20pt;
188 margin: 0;
189 padding: 0 0 2pt 10pt;
190 border-style: none none solid none;
191 border-width: 0 0 2pt 0;
192 border-color: #999;
193 }
194 h4 {
195 hyphens: none;
196 text-align: left;
197 font-size: 15pt;
198 margin: 0;
199 padding: 0 0 2pt 4pt;
200 }
201 h5,h6 {
202 hyphens: none;
203 text-align: left;
204 }
205 img {
206 max-width: 100%%;
207 }
208 a {
209 color: #204788;
210 text-decoration: none;
211 letter-spacing: .01em;
212 }
213 a[href^='#']:after {
214 content: target-counter(attr(href),page);
215 font-weight: normal;
216 font-size: 0.5em;
217 color: #555;
218 }
219 a[href^='http']:after {
220 content: ' (' attr(href) ') ';
221 font-weight: normal;
222 color: #555;
223 }
224 a.indexItemPre {
225 color: #000;
226 text-decoration: none;
227 letter-spacing: .01em;
228 }
229 a.indexItemPre[href^='#']:after {
230 content: ' ' leader('.') ' ';
231 font-size: 1em;
232 }
233 a.indexItem {
234 float: right;
235 overflow: hidden;
236 }
237 a.indexItem[href^='#']:after {
238 content: target-counter(attr(href),page);
239 color: #000;
240 font-size: 11pt;
241 }
242 #cover {
243 height: 100%%;
244 text-align: center;
245 display: flex;
246 flex-direction: column;
247 }
248 #cover * {
249 flex-grow: 1;
250 }
251 #cover h1 {
252 text-align: center;
253 font-size: 2.25em;
254 }
255 pre {
256 font-size: .8em;
257 }
258 li > p {
259 display: inline;
260 }
261 table {
262 display: block;
263 width: 100%%;
264 max-width: 100%%;
265 overflow: auto;
266 border-collapse: collapse;
267 text-align: left;
268 }
269 table tr {
270 border-top: 1pt solid #aaa;
271 }
272 th, td {
273 padding: 2pt 3pt;
274 border: 1pt solid #ccc;
275 }
276 th {
277 hyphens: none;
278 padding: 2pt 4pt;
279 text-transform: uppercase;
280 font-size: .8em;
281 }
282 td {
283 font-size: 11pt;
284 }
285 @page {
286 size: a4;
287 margin: 14mm 16mm 3cm 16mm;
288 }
289 @page:left {
290 @bottom-left {
291 content: counter(page);
292 font-family: 'Exo 2';
293 font-size: 10pt;
294 }
295 }
296 @page:right {
297 @bottom-left {
298 content: 'Furnace manual';
299 font-family: 'Exo 2';
300 font-size: 10pt;
301 color: #777;
302 }
303 @bottom-right {
304 content: counter(page);
305 font-family: 'Exo 2';
306 font-size: 10pt;
307 }
308 }
309 @page:first {
310 @bottom-left {
311 content: '';
312 }
313 @bottom-right {
314 content: '';
315 }
316 }
317 </style>
318 </head>
319 <body>
320 <section id="cover">
321 <div>
322 </div>
323 <div>
324 <img src="../logo.png" style="width: 288pt;">
325 <h1>Furnace<br/>User Manual</h1>
326 </div>
327 <div>
328 <i>for version 0.6.8.1</i>
329 </div>
330 </section>
331 <section id="authors">
332 <div>
333 </div>
334 <div>
335 <h3>authors</h3>
336 <ul>
337 <li>brickblock369</li>
338 <li>cam900</li>
339 <li>DeMOSic</li>
340 <li>Electric Keet</li>
341 <li>freq-mod</li>
342 <li>host12prog</li>
343 <li>Lunathir</li>
344 <li>tildearrow</li>
345 </ul>
346 <p>special thanks to ZoomTen for providing tools which assisted in the production of this document!</p>
347 <p>copyright © 2024 tildearrow and other authors.</p>
348 <p>this documentation is under the <a href="https://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 Unported</a> license.</p>
349 <p>you may reproduce, modify and/or distribute this documentation provided this copyright notice (including license and attribution) is present and any necessary disclaimers whether modifications have been made.</p>
350 <p>this documentation is provided as-is and without warranty of any kind.</p>
351 <p>this manual is written for version 0.6.8.1 of Furnace.<br/>it may not necessarily apply to previous or future versions.</p>
352 </section>
353 <section id="index">
354 %s
355 </section>
356 %s
357 </body>
358 </html>
359 ''' % (
360 index, html
361 )
362 )
363
364 print(final_html)