maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

 1#!/usr/bin/python3
 2
 3"""
 4This script does all necessary pre-processing to convert scdoc format into
 5Markdown.
 6
 7Usage:
 8    prepare_md.py < in > out
 9    prepare_md.py file1 file2 file3
10        Converts into _generated_file1.md, etc.
11"""
12
13import sys
14import re
15
16anchor_escape = str.maketrans(r' #()./\+-_', '__________')
17
18def prepare(r, w):
19    new_lines = list()
20    title = str()
21    previous_h1_anchor = ''
22
23    inside_literal = False
24
25    for line in r:
26        if not inside_literal:
27            if line.startswith('; TITLE ') and title == '':
28                title = line[8:]
29            if line[0] == ';':
30                continue
31            # turn *page*(1) into [**page(1)**](../_generated_page.1)
32            line = re.sub(r'\*(.+?)\*\(([0-9])\)', r'[*\1(\2)*](../_generated_\1.\2)', line)
33            # *aaa* => **aaa**
34            line = re.sub(r'\*(.+?)\*', r'**\1**', line)
35            # remove ++ from line endings
36            line = re.sub(r'\+\+$', '<br>', line)
37            # turn whatever looks like a link into one
38            line = re.sub(r'(https://[^ \)\(\\]+[a-z0-9_\-])', r'[\1](\1)', line)
39            # escape underscores inside words
40            line = re.sub(r'([^ ])_([^ ])', r'\1\\_\2', line)
41
42        if line.startswith('```'):
43            inside_literal = not inside_literal
44
45        new_lines.append(line)
46
47    if title != '':
48        print('#', title, file=w)
49
50    print(''.join(new_lines[1:]), file=w)
51
52if len(sys.argv) == 1:
53    prepare(sys.stdin, sys.stdout)
54else:
55    for f in sys.argv[1:]:
56        new_name = '_generated_' + f[:-4] + '.md'
57        prepare(open(f, 'r'), open(new_name, 'w'))