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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
# -*- coding: utf-8 -*-
"""Code to generate manpages
Author: G.J.J. van den Burg
License: See the LICENSE file.
Copyright: 2021, G.J.J. van den Burg
This file is part of Wilderness.
"""
import datetime as dt
import os
import re
from typing import TYPE_CHECKING
from typing import List
from typing import Optional
if TYPE_CHECKING:
import wilderness.application
class ManPage:
def __init__(
self,
application_name: str,
author: Optional[str] = "",
command_name: Optional[str] = None,
date: Optional[str] = None,
title: Optional[str] = None,
version: Optional[str] = "",
):
self._application_name = application_name
self._command_name = command_name
self._version = version
date = dt.date.today().strftime("%Y-%m-%d") if date is None else date
self._title = title
self._metadata = {
"Title": self.name,
"Author": author,
"Generator": "Wilderness <https://pypi.org/project/wilderness>",
"Date": date,
"Manual": f"{self._application_name} Manual",
"Source": f"{self._application_name} {self._version}",
"Language": "English",
}
# Must be last
self._page = []
self._page.extend(self.metadata())
self._page.append(self.header())
self._page.extend(self.preamble())
self._page.append(self.section_name())
@property
def name(self) -> str:
app = self._application_name
cmd = self._command_name
if cmd is None:
return app
return f"{app}-{cmd}"
def metadata(self) -> List[str]:
text = ["'\\\" t"] # This invokes the gtbl preprocessor (unused)
# Metadata
maxlen = max(map(len, self._metadata.keys()))
for key, value in self._metadata.items():
text.append(f'.\\"{key.rjust(maxlen + 1)}: {value}')
text.append('.\\"')
return text
def preamble(self) -> List[str]:
text = []
# Portability stuff
text.append('.\\" ' + "-" * 65)
text.append('.\\" * Define some portability stuff')
text.append('.\\" ' + "-" * 65)
text.append('.\\" ' + "~" * 65)
text.append('.\\" http://bugs.debian.org/507673')
text.append(
'.\\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html'
)
text.append('.\\" ' + "~" * 65)
text.append(".ie \\n(.g .ds Aq \\(aq")
text.append(".el .ds Aq '")
# Default formatting
text.append('.\\" ' + "-" * 65)
text.append('.\\" * set default formatting *')
text.append('.\\" ' + "-" * 65)
text.append('.\\" disable hyphenation')
text.append(".nh")
text.append('.\\" disable justification')
text.append(".ad l")
# Banner
text.append('.\\" ' + "-" * 65)
text.append('.\\" * MAIN CONTENT STARTS HERE *')
text.append('.\\" ' + "-" * 65)
return text
def header(self) -> str:
assert isinstance(self._metadata["Date"], str)
assert isinstance(self._version, str)
app = self._application_name
date = self._metadata["Date"].replace("-", "\\-")
version = self._version.replace(".", "\\&.")
header = (
f'.TH "{self.name.upper()}" "1" "{date}" '
f'"{app.capitalize()} {version}" '
f'"{app.capitalize()} Manual"'
)
return header
def section_name(self) -> str:
if self._title is None:
return f'.SH "NAME"\n{self.name}'
return f'.SH "NAME"\n{self.name} \\- {self._title}'
def add_section_synopsis(self, synopsis: str) -> None:
text = [
'.SH "SYNOPSIS"',
".sp",
".nf",
f"\\fI{self.groffify(synopsis)}",
".fi",
".sp",
]
self._page.extend(text)
def add_section(self, label: str, text: str) -> None:
section = [
f'.SH "{label.upper()}"',
".sp",
self.groffify(text),
]
self._page.extend(section)
def groffify(self, text: str) -> str:
"""Format a text line for use in manpages
This function supports several basic formatting constructs. First,
newlines in the text are preserved. Next, lists can be created by
starting lines with :literal:`* \ ` , as long as each list entry starts
on its own line (that is, separated by :code:`\\n`). Indented text can
be created by prefixing a line with one or more :code:`\\t` characters.
Numbered lists are also recognized, as long as each item starts with
:literal:`1. \ ` (that is, a digit followed by a period, followed by a
space).
Parameters
----------
text : str
The text to convert
Returns
-------
formatted_text : str
The formatted text ready for use in manpage documents.
"""
output = []
lines = text.split("\n")
for line in lines:
match = re.match("^\ ?\d+\.\ ", line)
if line.startswith("* "):
output.append(".RS 4")
output.append(".ie n \\{\\")
output.append("\\h'-04'\\(bu\\h'+03'\\c")
output.append(".\\}")
output.append(".el \\{\\")
output.append(".sp -1")
output.append(".IP \\(bu 2.3")
output.append(".\\}")
output.append(self.groffify_line(line[2:]))
output.append(".RE")
elif line.startswith("\t"):
c = 0
while line.startswith("\t"):
line = line[1:]
c += 1
output.append(f".RS {4 * c}")
output.append(self.groffify_line(line))
output.append(".RE")
elif match:
label = line[match.start() : match.end()]
rest = line[match.end() :]
output.append(f"\\fB{label}\\fR{self.groffify_line(rest)}")
output.append(".br")
elif line in ["", "\n"]:
output.append(".sp")
else:
output.append(self.groffify_line(line))
return "\n".join(output)
def groffify_line(self, line: str) -> str:
line = line.replace("\\", "\\e")
line = line.replace("-", "\\-")
line = line.replace("...", "\\&...")
line = line.replace("\n\n", "\n.sp\n")
return line
def export(self, output_dir: str) -> str:
filename = os.path.join(output_dir, f"{self.name}.1")
with open(filename, "w") as fp:
fp.write("\n".join(self._page))
return filename
def build_manpages(
app: "wilderness.application.Application", output_directory: str = "man"
) -> None:
"""Write manpages to the output directory
Parameters
----------
app : :class:`wilderness.Application`
The application for which to generate manpages.
output_directory : str
The output directory to which to write the manpages.
"""
os.makedirs(output_directory, exist_ok=True)
man = app.create_manpage()
filename = man.export(output_directory)
print(f"Wrote manpage to {filename}")
for cmd in app.commands:
man = cmd.create_manpage()
filename = man.export(output_directory)
print(f"Wrote manpage to {filename}")
|