summaryrefslogtreecommitdiff
path: root/roff.py
blob: 2690e2f61ac38e842e24c06066710d2b0b1ec9a9 (plain)
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
"""
Library for generating roff markup.

This library is FAR from complete, and supports exactly what these
scripts require.

For information about roff commands meaning, see groff_man(7).
"""

import os
from datetime import datetime
from typing import TypeAlias, Literal


Align: TypeAlias = Literal['R', 'L']


def title_heading(topic: str,
                  section: int,
                  *,
                  footer_middle: str | None = None,
                  footer_inside: str | None = None,
                  header_middle: str | None = None):
    print(f'.TH "{topic.upper()}" {section} "{footer_middle}"')


def section_heading(s: str):
    print(f'.SH "{s.upper()}"')


def subsection_heading(s: str):
    print(f'.SS "{s}"')


def roff_table(headers: list[tuple[str, Align]],
               keys: list[str],
               entries: list[dict[str, str]]):
    print(".TS")
    print("tab(@);")
    print(' '.join([a for (_, a) in headers] + ['.']))

    print("@".join(h for (h, _) in headers))

    for entry in entries:
        print("@".join(entry[key] for key in keys))

    print(".TE")


def now():
    if ct := os.getenv("CURRENT_TIME"):
        if ct.isnumeric():
            return datetime.fromtimestamp(int(ct))
        else:
            return datetime.fromisoformat(ct)
    else:
        return datetime.now()