File: append-colophon

package info (click to toggle)
manpages-zh 1.6.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,460 kB
  • sloc: python: 129; makefile: 4
file content (133 lines) | stat: -rwxr-xr-x 4,026 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3

"""append-colophon -- automatically append colophon(“跋”) when making

Copyright (c) 2016-2020 Boyuan Yang <byang@debian.org>
This file is released into the public domain.

* Require UTF-8 input and output.
"""

import io
import sys
import fileinput

# deal with I/O encoding first
sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
def utf8_print(output: str, end="\n"):
    output_str = output + end
    sys.stdout.buffer.write(output_str.encode('UTF-8'))

def parse_is_stub(line: str) -> bool:
    """Try-parse to see if the document is a stub"""
    if line[:4] == ".so ":
        return True
    else:
        return False

def parse_translator_info(line: str, t_info: dict) -> bool:
    """Try-parse to see if we met the translator information.

    A "good" translator information should be like:
        .\" manpages-zh translator: Foo Bar <email>
        .\" manpages-zh date: 2016-11-22
        .\" manpages-zh orig-date: 2012-03-22
        .\" manpages-zh orig-package: bash
    """
    if line[:16] == ".\\\" manpages-zh ":
        t_info[line[16:].split(": ")[0]] = line[16:].split(": ")[1]
        return True
    return False

def parse_find_colophon(line: str) -> bool:
    """Try-parse to see if this is a colophon.

    Definition of colophon:
        .SH "跋" or .SH 跋
    """
    colophon_lines = [
            '.SH "跋"\n',
            '.SH 跋\n',
            '.Sh 跋\n',
            '.Sh "跋"\n']
    if line in colophon_lines:
        return True
    else:
        return False
    pass

def parse_mdoc_type(line: str) -> bool:
    """Determine if we have met a mdoc page.
    """
    if line[:4] == '.Sh ':
        return True
    else:
        return False

if __name__ == "__main__":
    "some flags here"
    flag_met_colophon = False
    no_colophon = False
    mdoc_type = False
    t_info = {} # translator information
    format_mdoc_lines = {
            "new_paragraph": ".Pp",
            "new_section_colophon": '.Sh "跋"',
            }
    format_man_lines = {
            "new_paragraph": ".PP",
            "new_section_colophon": '.SH "跋"',
            }
    format_lines = None

    for line in fileinput.input():
        if parse_mdoc_type(line):
            mdoc_type = True
        if parse_is_stub(line):
            "only a '.so foobar.x' stub, copy as-is"
            no_colophon = True
        if parse_translator_info(line, t_info):
            continue
        if parse_find_colophon(line):
            flag_met_colophon = True
        utf8_print(line, end="")

    "At the end of the output, append our colophon"
    if mdoc_type:
        format_lines = format_mdoc_lines
    else:
        format_lines = format_man_lines
    if no_colophon:
        sys.exit(0)
    if not flag_met_colophon:
        utf8_print(format_lines["new_section_colophon"])
    else:
        pass
    #utf8_print(format_lines["new_paragraph"])
    utf8_print('.br')
    utf8_print('本页面中文版由中文 man 手册页计划提供。')

    if len(t_info.keys()) > 0:
        "we need another paragraph"
        utf8_print(format_lines["new_paragraph"])
        if "translator" in t_info.keys():
            utf8_print('翻译人员:' + t_info["translator"], end="")
            utf8_print('.br')
        if "orig-date" in t_info.keys():
            utf8_print('获取日期:' + t_info["orig-date"], end="")
            utf8_print('.br')
        if "date" in t_info.keys():
            utf8_print('翻译日期:' + t_info["date"], end="")
            utf8_print('.br')
        if "orig-package" in t_info.keys():
            utf8_print('原始软件:' + t_info["orig-package"], end="")
            utf8_print('.br')
        if "comment" in t_info.keys():
            utf8_print(t_info["comment"], end="")
            utf8_print('.br')

    #utf8_print(format_lines["new_paragraph"])
    utf8_print('.br')
    utf8_print("中文 man 手册页计划:\\fB" +
          "https://github.com/man-pages-zh/manpages-zh" +
          "\\fR")