Debian Codegraph @

hmmer/easel/devkit/rmanprocess.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # rmanprocess.py
4 # Massages output of PolyGlotMan's `rman -f latex2e` to fit Tufteian userguide style.
5 # Example:
6 # rman -f latex2e hmmbuild.man | rmanprocess.py > manpage.tex
7 #
8
9
10 import sys
11 import re
12
13 in_synopsis = False
14
15 if len(sys.argv) == 1:
16 f = sys.stdin
17 else:
18 f = open(sys.argv[1])
19
20 for line in f:
21 line = line.rstrip('\n')
22
23 # State flags (where are we in the document)
24 if re.match(r'\\section{Synopsis}', line):
25 in_synopsis = True
26 elif re.match(r'\\section{', line):
27 in_synopsis = False
28
29 #
30 # Linewise substitutions: replace certain entire lines with something else.
31 #
32 # Remove \documentclass, and changes to \parindent and \parskip
33 if re.match(r'\\documentclass', line): continue
34 if re.match(r'\\setlength{\\parindent}', line): continue
35 if re.match(r'\\setlength{\\parskip}', line): continue
36 if re.match(r'\\begin\{document\}', line): continue
37
38 # Replace \section{Name} with \section{progname - description}, using next line too.
39 if re.match(r'\\section\{Name\}', line):
40 for line in f:
41 if not re.fullmatch(r'\s*', line):
42 break
43 m = re.match(r'(\S+)\s*\\?-\s*(.+)$', line)
44 if m:
45 print(r'\section{{\texorpdfstring{{\monob{{{0}}}}}{{{0}}} - {1}}}'.format(m.group(1), m.group(2)))
46 else:
47 print("Error: no progname/description line found");
48 sys.exit(1)
49 continue
50
51 # Remove everything after \section{See Also), and finish.
52 if re.match(r'\\section\{See', line) or re.match(r'\\end\{document', line):
53 print("\\newpage");
54 break
55
56
57 #
58 # Extra directives: preface certain lines with something extra, but still
59 # process the line.
60 #
61
62 # In synopsis, put \noindent in front of each commandline, and preserve the .B's as bold.
63 if in_synopsis and re.match(r'\s*\\textbf{', line):
64 line = re.sub(r'\\textbf\{', r'\\monob{', line)
65 print("\\noindent")
66
67 #
68 # Substitutions within a line.
69 # The order of these replacements is important. (More specific ones first.)
70 #
71 line = re.sub(r'\\begin\{itemize\}', r'\\begin{wideitem}', line)
72 line = re.sub(r'\\end\{itemize\}', r'\\end{wideitem}', line)
73 line = re.sub(r'\\section\{', r'\\subsection*{', line) # \subsection* suppresses inclusion in TOC
74 line = re.sub(r'--', r'{-}{-}', line)
75 line = re.sub(r'\\item\s*\[\\textbf', r'\\item [\\monob', line) # option names in .TP are emphasized bold
76 line = re.sub(r'\\textbf\{\\% ', r'\\user{\\% ', line) # example command lines are bold, on their own line
77 line = re.sub(r'\\textit\{', r'\\monoi{', line) # metavariables (options, args) are .I in man, mono italic in tex
78 line = re.sub(r'\\textbf\{', r'\\mono{', line) # literals (commands, etc) are .B in man, normal mono in tex
79
80 print(line)
81
82
83
84 if f != sys.stdin:
85 f.close()
86