-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
538 lines (433 loc) · 16.2 KB
/
main.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
from os import path
from re import finditer, search
from sys import argv
# TODO:
# |-- Possibly refactor everything to build an intermediate representation
# | instead of converting the input to output in-place.
# | |-- This would improve performace drastically, I presume.
# | `-- Convert markdown to a list of tokens, each with a type.
# | `-- Use these tokens to generate the contents portion of the output Texinfo file.
# |-- Mark lines that were made empty as removable from final output.
# |-- Parse HTML comments and replace them with nothing in final output
# | ie. `<!-- ... the comment ... -->`
# |-- Horizontal Rules (`---`, `___`, and `***`)
# `-- Parse markdown tables into GNU Texinfo multitables
def print_usage():
print("Markdown to GNU Texinfo Converter by Rylan `Lens` Kellogg")
print()
print("Usage:")
print(" python main.py [FLAGS] [OPTIONS] \"path/to/markdown.md\"")
print()
print("Flags:")
print(" -h, --help -- Show the help dialog in stdout")
print(" -i, --inline -- Do not specify new nodes for sections within chapters.")
print(" --hide-toc -- Hide any line that starts with `#` and contains `Table of Contents`.")
print()
print("Options:")
print(" -t <title> -- Specify the title of the Texinfo manual that is generated")
print(" -o <path> -- Specify the file path where the Texinfo manual will be saved to")
print()
exit(0)
def parse_headers_new(src, title, inline, hide_toc):
lines = src.split('\n')
within_toc = False
prev_hash_count = 0
nest_level = 0
for i in range(len(lines)):
if len(lines[i]) < 2:
continue
if i == 0 and lines[0][0] == '#' and lines[0][1] == ' ':
title = lines[0][2:]
lines[0] = ""
continue
if hide_toc and ("table of contents" in lines[i].lower() \
or "table-of-contents" in lines[i].lower()):
lines[i] = ""
within_toc = True
continue
hash_count = 0
while lines[i][hash_count] == '#':
hash_count += 1
if hide_toc and within_toc:
if hash_count != 0:
within_toc = False
else:
lines[i]= ""
continue
if hash_count == 0 or hash_count > 6:
continue
# Node names must not use periods, commas, or colons, or start with a left parenthesis.
name = lines[i][hash_count:]
name = name.replace(".", " ")
name = name.replace(",", " ")
name = name.replace(":", " ")
if name.startswith('('):
name[0] = ' '
if hash_count > prev_hash_count:
nest_level = min(nest_level + 1, 4)
elif hash_count < prev_hash_count:
nest_level = max(nest_level - (prev_hash_count - hash_count), 1)
prev_hash_count = hash_count
# Always specify a new node for each chapter,
# but do not for any other section if inline is specified.
if nest_level == 1 or not inline:
lines[i] = "@node" + name + '\n'
else:
lines[i] = ""
if nest_level == 1:
lines[i] += "@chapter" + name
elif nest_level == 2:
lines[i] += "@section" + name
elif nest_level == 3:
lines[i] += "@subsection" + name
elif nest_level == 4:
lines[i] += "@subsubsection" + name
else:
print("ERROR: Invalid nest level")
return ('\n'.join(lines), title)
def parse_headers(src, inline):
lines = src.split('\n')
for i in range(len(lines)):
if len(lines[i]) < 2:
continue
count = 0
while (lines[i][count] == '#'):
count += 1
if count == 0:
continue
if count > 4:
lines[i] = lines[i][count+1:]
continue
name = lines[i][count:]
# Always specify a new node for each chapter,
# but do not for any other section if inline is specified.
if count == 1 or not inline:
lines[i] = "@node" + name + '\n'
else:
lines[i] = ""
if count == 1:
lines[i] += "@chapter" + name
elif count == 2:
lines[i] += "@section" + name
elif count == 3:
lines[i] += "@subsection" + name
elif count == 4:
lines[i] += "@subsubsection" + name
return '\n'.join(lines)
names_used = set()
def parse_anchors(src):
lines = src.split('\n')
for i in range(len(lines)):
# Minimum length: "<a></a>"
if len(lines[i]) < 7:
continue
anchors = finditer(r'<a[^/].*>.*</a>', lines[i])
for anchor in anchors:
name = search(r'(name=")(.*)(")', anchor.group())
if name.groups()[1]:
lines[i] = lines[i].replace(anchor.group(), "")
if name.groups()[1] not in names_used:
lines[i] += "\n@anchor{" + name.groups()[1] + "}\n"
names_used.add(name.groups()[1])
return '\n'.join(lines)
def is_bulleted_list_line(line):
return line.startswith("- ") \
or line.startswith("+ ") \
or line.startswith("* ")
def parse_bulleted_lists(src):
lines = src.split('\n')
is_list = False
for i in range(len(lines)):
if len(lines[i]) < 2:
if is_list:
lines[i] = "@end itemize\n" + lines[i]
is_list = False
continue
# Detect all of the entries within a list.
if is_list:
if is_bulleted_list_line(lines[i]):
lines[i] = "@item\n" + lines[i][2:]
else:
lines[i] = "@end itemize\n" + lines[i]
is_list = False
continue
# Detect the start of a list.
if is_bulleted_list_line(lines[i]):
lines[i] = "@itemize\n@item\n" + lines[i][2:]
is_list = True
return '\n'.join(lines)
# FIXME: Checking exact indices for specific characters
# is quite possibly the worst way to do this.
def parse_enumerated_lists(src):
lines = src.split('\n')
is_list = False
for i in range(len(lines)):
# Minimum length: "1. a"
if len(lines[i]) < 4:
if is_list:
lines[i] = "@end enumerate\n" + lines[i]
is_list = False
continue
# Detect all of the entries within a list.
if is_list:
if lines[i][0].isdigit() and lines[i][1] == '.' and lines[i][2] == ' ':
lines[i] = "@item\n" + lines[i][3:]
else:
lines[i] = "@end enumerate\n" + lines[i]
is_list = False
continue
# Detect the start of a list.
if lines[i][0].isdigit() and lines[i][1] == '.' and lines[i][2] == ' ':
lines[i] = "@enumerate\n@item\n" + lines[i][3:]
is_list = True
return '\n'.join(lines)
# TODO: Parse nested lists!
def parse_lists(src):
out = src
out = parse_bulleted_lists(out)
out = parse_enumerated_lists(out)
return out
def parse_code(src):
lines = src.split('\n')
in_block = False
for i in range(len(lines)):
# Minimum length: "` `"
if len(lines[i]) < 3:
continue
# Triple backtick code blocks
if lines[i].startswith("```"):
if in_block:
lines[i] = "@end example"
in_block = False
else:
lines[i] = "@example"
in_block = True
continue
# Single backtick wrapped code segments
code_in_quotes = finditer(r'`.+?`', lines[i])
for code in code_in_quotes:
lines[i] = lines[i].replace(code.group(), "@code{" + code.group()[1:-1] + "}")
return '\n'.join(lines)
def parse_images(src):
lines = src.split('\n')
for i in range(len(lines)):
# Minimum length: "![a](b)"
if len(lines[i]) < 7:
continue
images = finditer(r'!\[(.*?)\] *\((.*?)\)', lines[i])
for image in images:
alt_text, link = image.groups()
# FIXME: Not all image links end with an extension!
filename, extension = link.rsplit('.', 1)
lines[i] = lines[i].replace(image.group(), "@image{" + filename + ",,," + alt_text + "," + extension + "}")
return '\n'.join(lines)
# NOTE: This function gets confused at images,
# so be sure to parse_images() first!
def parse_links(src):
lines = src.split('\n')
for i in range(len(lines)):
# Minimum length: "[a](b)"
if len(lines[i]) < 6:
continue
links = finditer(r'\[(.+?)\] *\((.+?)\)', lines[i])
for it in links:
name, link = it.groups()
if link.startswith('#'):
replacement = "@ref{" + link[1:]
if name:
replacement += ",," + name
replacement += "}"
lines[i] = lines[i].replace(it.group(), replacement)
continue
replacement = "@url{" + link
if name:
replacement += ", " + name
replacement += "}"
lines[i] = lines[i].replace(it.group(), replacement)
return '\n'.join(lines)
def parse_bold(src):
lines = src.split('\n')
for i in range(len(lines)):
# Minimum length: "**a**"
if len(lines[i]) < 5:
continue
bold_text = finditer(r'([\*\_])\1(.*)([\*\_])\1', lines[i])
for it in bold_text:
lines[i] = lines[i].replace(it.group(), "@strong{" + it.group()[2:-2] + "}")
return '\n'.join(lines)
def parse_italics(src):
lines = src.split('\n')
for i in range(len(lines)):
# Minimum length: "*a*"
if len(lines[i]) < 3:
continue
italic_text = finditer(r'([\*\_])(.*)([\*\_])', lines[i])
for it in italic_text:
lines[i] = lines[i].replace(it.group(), "@emph{" + it.group()[1:-1] + "}")
return '\n'.join(lines)
def parse_at_characters(src):
lines = src.split('\n')
links = "\\[(.+?)\\] *\\((.+?)\\)"
code_single = "`.+?`"
for i in range(len(lines)):
unescaped_at_characters = finditer(r'@', lines[i])
for it in unescaped_at_characters:
lines[i] = lines[i].replace(it.group(), '@' + it.group())
return '\n'.join(lines)
def parse_escaped_characters(src):
lines = src.split('\n')
links = "\\[(.+?)\\] *\\((.+?)\\)"
code_single = "`.+?`"
for i in range(len(lines)):
# Minimum length: "\a"
if len(lines[i]) < 2:
continue
# TODO: Figure out how to skip triple backtick code blocks...
verbatim_texts = finditer(r'' + links
+ '|' + code_single
, lines[i])
# Parse markdown escaped characters
escaped_characters = finditer(r'\\.', lines[i])
for it in escaped_characters:
skip = False
for txt in verbatim_texts:
if it.group() in txt.group():
skip = True
break
if skip:
skip = False
continue
character = it.group()[1]
# The following characters must be escaped in markdown but not Texinfo
if character == '\\' \
or character == '`' \
or character == '*' \
or character == '_' \
or character == '[' \
or character == ']' \
or character == '<' \
or character == '>' \
or character == '(' \
or character == ')' \
or character == '#' \
or character == '+' \
or character == '-' \
or character == '.' \
or character == '!' \
or character == '|':
lines[i] = lines[i].replace(it.group(), character)
# The following characters must be escaped in Texinfo as well as markdown
if character == '{' \
or character == '}':
lines[i] = lines[i].replace(it.group(), '@' + character)
return '\n'.join(lines)
def parse_trailing_backslash(src):
lines = src.split('\n')
for i in range(len(lines)):
# Minimum length: " \"
if len(lines[i]) < 2:
continue
if lines[i].endswith(" \\"):
lines[i] = lines[i][:-2] + '\n\n'
return '\n'.join(lines)
def parse_html_comments(src):
lines = src.split('\n')
for i in range(len(lines)):
# Minimum length: "<!---->"
if len(lines[i]) < 7:
continue
comments_in_line = finditer(r'<!--.*-->', lines[i])
for comment in comments_in_line:
lines[i] = lines[i].replace(comment.group(), "")
return '\n'.join(lines)
def parse_markdown(src, title, inline, hide_toc):
out = src
out = parse_at_characters(out)
out = parse_trailing_backslash(out)
out = parse_html_comments(out)
out = parse_anchors(out)
out, title = parse_headers_new(out, title, inline, hide_toc)
out = parse_lists(out)
out = parse_escaped_characters(out)
out = parse_bold(out)
out = parse_italics(out)
out = parse_code(out)
out = parse_images(out)
out = parse_links(out)
return (out, title)
# Returns a tuple with information parsed from command line arguments with the following structure:
# (
# file_path
# , output_file_path
# , title_of_manual
# , should_inline_sections
# , hide_table_of_contents
# )
def parse_cmd_args():
argc = len(argv)
if argc < 2:
print_usage()
file_path = ""
title = ""
output_file_path = ""
inline = False
hide_toc = False
i = 1
while i < argc:
arg = argv[i]
if "-h" in arg.lower() and "--h" not in arg.lower():
print_usage()
elif arg.startswith("-t"):
if i + 1 >= argc:
print("ERROR: Expected a title to be specified after `-t` option.")
exit(1)
i += 1
title = argv[i]
elif arg.startswith("-o"):
if i + 1 >= argc:
print("ERROR: Expected an output file path to be specified after `-o` option.")
exit(1)
i += 1
output_file_path = argv[i]
elif arg.startswith("-i") or arg.startswith("--inline"):
inline = True
elif arg.startswith("--hide-toc"):
hide_toc = True
else:
if not file_path:
file_path = arg
else:
print("ERROR: Multiple file paths specified (command line argument unrecognized)")
exit(1)
i += 1
if not file_path:
print("ERROR: No file path was given, or was not able to be parsed from the command line arguments given.")
exit(1)
if not file_path.lower().endswith(".md"):
print("ERROR: The file path given is not a valid markdown file (wrong extension).")
print(" -> ", file_path)
exit(1)
return (file_path, output_file_path, title, inline, hide_toc)
def main():
file_path, output_file_path, title, inline, hide_toc = parse_cmd_args()
with open(file_path) as markdown:
md = markdown.read()
md, title = parse_markdown(md, title, inline, hide_toc)
# If no manual title is specified on the command
# line, generate one from the input file name.
if not title:
title = path.basename(file_path)
# If no output file path is specified on the command
# line, generate one from the title.
# Convert to lowercase and replace all spaces with
# underscores to ensure valid file name.
if not output_file_path:
output_file_path = title.lstrip().lower().replace(' ', '_') + ".texi"
with open("template.texi") as template:
texi = template.read()
texi = texi.replace("$${{TITLE}}$$", title)
texi = texi.replace("$${{CONTENTS}}$$", md)
with open(output_file_path, 'w') as out:
out.write(texi)
if __name__ == "__main__":
main()