aboutsummaryrefslogtreecommitdiff
path: root/util/mkrfd.py
blob: e1d0a256e0bf3b64f3f34af4af8faf51ee051337 (plain) (blame)
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
#!/usr/bin/env python3

"""
mkrfd

Utility to generate plain text Request for Discussions files from simple
markup.

Copyright (C) 2022  Andrew Yu <andrew@andrewyu.org>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
"""

from sys import *
import yaml
from pprint import pprint  # For debugging

# Constants that should stay constant across the program
PAGEBREAK = "\f"  # Form feed, sometimes denoted as Control-L
WIDTH = 72

# Variables that actually change while running
page_counter = 1
section_counter = 1
section_counting = True
this_page_line_counter = 0

output_queue = []

def output(string):
    output_queue.append("string")
    r = 0
    for i in range(len(string)):
        if (string == "\n"):
            r += 1
    return r

preamble = ""
while True:
    l = stdin.readline()
    if l == "":
        print(
            "mkrfd: Unexpected EOF when expecting the preamble to end (---).",
            file=stderr,
        )
        exit(1)
    elif l.startswith("---"):
        break
    else:
        preamble += l
del l

configuration = yaml.safe_load(preamble)
del preamble

stdout.write(
    (int(0.5 * (WIDTH - len(configuration["title"])))) * " "
    + configuration["title"]
    + "\n"
)
stdout.write("\n")

while True:
    l = stdin.readline()
    if l == "":
        exit(0)
    else:
        if l.startswith("\\"):  # This string is a single backslash
            if l.startswith("\\\\"):
                l = l[1:]
            elif l.startswith("\\frontmatter"):
                section_counting = False
                toc_recording = True
                continue
            elif l.startswith("\\mainmatter"):
                toc_recording = True
                continue
            else:
                print("mkrfd: Undefined control sequence: " + l + ".", file=stderr)
                exit(2)
del l