zman 0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- zman-0.1/LICENSE +21 -0
- zman-0.1/PKG-INFO +109 -0
- zman-0.1/README.md +98 -0
- zman-0.1/man/zman.1 +98 -0
- zman-0.1/pyproject.toml +25 -0
- zman-0.1/setup.cfg +4 -0
- zman-0.1/src/zman/__init__.py +3 -0
- zman-0.1/src/zman/renderer.py +178 -0
- zman-0.1/src/zman/selection.py +321 -0
- zman-0.1/src/zman/zmanp.py +328 -0
- zman-0.1/src/zman.egg-info/PKG-INFO +109 -0
- zman-0.1/src/zman.egg-info/SOURCES.txt +17 -0
- zman-0.1/src/zman.egg-info/dependency_links.txt +1 -0
- zman-0.1/src/zman.egg-info/entry_points.txt +3 -0
- zman-0.1/src/zman.egg-info/top_level.txt +1 -0
- zman-0.1/tests/test_cli.py +225 -0
- zman-0.1/tests/test_install.py +42 -0
- zman-0.1/tests/test_renderer.py +55 -0
- zman-0.1/tests/test_selection.py +201 -0
zman-0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 zman contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
zman-0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: zman
|
|
3
|
+
Version: 0.1
|
|
4
|
+
Summary: A Python man-page viewer for z/OS UNIX with zotman fallback
|
|
5
|
+
Author: zman contributors
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.9
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Dynamic: license-file
|
|
11
|
+
|
|
12
|
+
# zman
|
|
13
|
+
|
|
14
|
+
A small, dependency-free Python manual-page viewer for z/OS UNIX. It is an
|
|
15
|
+
independent CLI implementation inspired by [Qman](https://github.com/plp13/qman),
|
|
16
|
+
not a port of its C source or its interactive terminal UI.
|
|
17
|
+
|
|
18
|
+
The `zman` and `man` commands render plain-text pages and a useful subset of
|
|
19
|
+
roff man-page macros. If a request cannot be handled locally, zman runs
|
|
20
|
+
`zotman` with the original arguments. This lets z/OS Open Tools handle pages
|
|
21
|
+
that need its formatter or database.
|
|
22
|
+
|
|
23
|
+
## Install without pip
|
|
24
|
+
|
|
25
|
+
Requires Python 3.9 or newer. The installer uses only the Python standard
|
|
26
|
+
library:
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
python3 install.py
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The default prefix is `/usr/local`. To install to a user-writable prefix, run
|
|
33
|
+
`python3 install.py --prefix "$HOME/.local"` or set `PREFIX`. The installer
|
|
34
|
+
copies the Python modules, creates executable `zman` and `man` launchers, and
|
|
35
|
+
installs `zman.1`. If `chtag` is available, it tags installed files as
|
|
36
|
+
ISO8859-1. Ensure the prefix's `bin` directory is on `PATH` and `zotman` is on
|
|
37
|
+
`PATH` for fallback handling. For a non-default prefix, include its
|
|
38
|
+
`share/man` directory in `MANPATH` to find the installed page.
|
|
39
|
+
|
|
40
|
+
The `man` launcher may replace the usual `man` command when first on `PATH`;
|
|
41
|
+
it invokes `zotman` whenever it cannot render a request.
|
|
42
|
+
|
|
43
|
+
## z/OS USS
|
|
44
|
+
|
|
45
|
+
The Git attributes keep source and documentation files in LF format and encode
|
|
46
|
+
the working tree as ISO8859-1. The installer uses `chtag` to tag installed
|
|
47
|
+
launchers, Python modules, and the man page when `chtag` is available. If the
|
|
48
|
+
installer reports that `chtag` is missing, tag the installed files manually:
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
prefix=/usr/local
|
|
52
|
+
for file in "$prefix/bin/zman" "$prefix/bin/man" "$prefix/share/man/man1/zman.1"; do
|
|
53
|
+
chtag -tc ISO8859-1 "$file"
|
|
54
|
+
done
|
|
55
|
+
find "$prefix/lib/zman/zman" -type f -name '*.py' -exec chtag -tc ISO8859-1 {} \;
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Install the zopen `man-db` package and run `mandb` to index the installed
|
|
59
|
+
manual page. The page is installed under `share/man/man1`.
|
|
60
|
+
|
|
61
|
+
## Use
|
|
62
|
+
|
|
63
|
+
```sh
|
|
64
|
+
man
|
|
65
|
+
man -m printf
|
|
66
|
+
man -k printf
|
|
67
|
+
man -kw print
|
|
68
|
+
man printf
|
|
69
|
+
man 1 printf
|
|
70
|
+
zman ./my-command.1
|
|
71
|
+
./src/zman/zmanp.py --version
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
With no page, zman runs a separate `man -k .` command and opens an interactive
|
|
75
|
+
list of available pages; it skips its own `man` launcher and tries `zotman -k`
|
|
76
|
+
if the separate command is unavailable or returns unusable output. If neither
|
|
77
|
+
command returns parseable results, zman indexes local manual pages by their
|
|
78
|
+
`NAME` sections. Use `-k keyword` to list pages containing the keyword, or
|
|
79
|
+
`-kw word` to match a whole word in a page name or description. Use `-m page`
|
|
80
|
+
to display a page.
|
|
81
|
+
The list supports type-to-filter, `/` to find, Ctrl+N/Ctrl+P to move between
|
|
82
|
+
find matches, and F1 for an interactive tutorial. The interface uses terminal
|
|
83
|
+
colors when available and falls back to monochrome attributes otherwise.
|
|
84
|
+
|
|
85
|
+
Page lookup uses `MANPATH` when set, then checks the common locations
|
|
86
|
+
`/usr/share/man`, `/usr/local/share/man`, `/usr/man`, and `/usr/local/man`.
|
|
87
|
+
Plain, gzip, bzip2, and xz-compressed pages are supported when the corresponding
|
|
88
|
+
Python standard-library codec is available.
|
|
89
|
+
|
|
90
|
+
Requests that are not supported locally and pages with unsupported roff
|
|
91
|
+
constructs are passed through to `zotman`. If `zotman` is not installed, zman
|
|
92
|
+
prints an error and exits with status 127.
|
|
93
|
+
|
|
94
|
+
This initial viewer does not implement Qman's full page navigation, in-page
|
|
95
|
+
search, or hyperlinks.
|
|
96
|
+
|
|
97
|
+
The page-selection screen requires a Python build with the standard-library
|
|
98
|
+
`curses` module. Direct page display does not require curses.
|
|
99
|
+
|
|
100
|
+
## Development
|
|
101
|
+
|
|
102
|
+
Run the test suite without installing the project:
|
|
103
|
+
|
|
104
|
+
```sh
|
|
105
|
+
PYTHONPATH=src python3 -m unittest discover -s tests
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
The project's `pyproject.toml` also supports package builds for environments
|
|
109
|
+
that use pip, but pip is not required to install or run the program.
|
zman-0.1/README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# zman
|
|
2
|
+
|
|
3
|
+
A small, dependency-free Python manual-page viewer for z/OS UNIX. It is an
|
|
4
|
+
independent CLI implementation inspired by [Qman](https://github.com/plp13/qman),
|
|
5
|
+
not a port of its C source or its interactive terminal UI.
|
|
6
|
+
|
|
7
|
+
The `zman` and `man` commands render plain-text pages and a useful subset of
|
|
8
|
+
roff man-page macros. If a request cannot be handled locally, zman runs
|
|
9
|
+
`zotman` with the original arguments. This lets z/OS Open Tools handle pages
|
|
10
|
+
that need its formatter or database.
|
|
11
|
+
|
|
12
|
+
## Install without pip
|
|
13
|
+
|
|
14
|
+
Requires Python 3.9 or newer. The installer uses only the Python standard
|
|
15
|
+
library:
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
python3 install.py
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The default prefix is `/usr/local`. To install to a user-writable prefix, run
|
|
22
|
+
`python3 install.py --prefix "$HOME/.local"` or set `PREFIX`. The installer
|
|
23
|
+
copies the Python modules, creates executable `zman` and `man` launchers, and
|
|
24
|
+
installs `zman.1`. If `chtag` is available, it tags installed files as
|
|
25
|
+
ISO8859-1. Ensure the prefix's `bin` directory is on `PATH` and `zotman` is on
|
|
26
|
+
`PATH` for fallback handling. For a non-default prefix, include its
|
|
27
|
+
`share/man` directory in `MANPATH` to find the installed page.
|
|
28
|
+
|
|
29
|
+
The `man` launcher may replace the usual `man` command when first on `PATH`;
|
|
30
|
+
it invokes `zotman` whenever it cannot render a request.
|
|
31
|
+
|
|
32
|
+
## z/OS USS
|
|
33
|
+
|
|
34
|
+
The Git attributes keep source and documentation files in LF format and encode
|
|
35
|
+
the working tree as ISO8859-1. The installer uses `chtag` to tag installed
|
|
36
|
+
launchers, Python modules, and the man page when `chtag` is available. If the
|
|
37
|
+
installer reports that `chtag` is missing, tag the installed files manually:
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
prefix=/usr/local
|
|
41
|
+
for file in "$prefix/bin/zman" "$prefix/bin/man" "$prefix/share/man/man1/zman.1"; do
|
|
42
|
+
chtag -tc ISO8859-1 "$file"
|
|
43
|
+
done
|
|
44
|
+
find "$prefix/lib/zman/zman" -type f -name '*.py' -exec chtag -tc ISO8859-1 {} \;
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Install the zopen `man-db` package and run `mandb` to index the installed
|
|
48
|
+
manual page. The page is installed under `share/man/man1`.
|
|
49
|
+
|
|
50
|
+
## Use
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
man
|
|
54
|
+
man -m printf
|
|
55
|
+
man -k printf
|
|
56
|
+
man -kw print
|
|
57
|
+
man printf
|
|
58
|
+
man 1 printf
|
|
59
|
+
zman ./my-command.1
|
|
60
|
+
./src/zman/zmanp.py --version
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
With no page, zman runs a separate `man -k .` command and opens an interactive
|
|
64
|
+
list of available pages; it skips its own `man` launcher and tries `zotman -k`
|
|
65
|
+
if the separate command is unavailable or returns unusable output. If neither
|
|
66
|
+
command returns parseable results, zman indexes local manual pages by their
|
|
67
|
+
`NAME` sections. Use `-k keyword` to list pages containing the keyword, or
|
|
68
|
+
`-kw word` to match a whole word in a page name or description. Use `-m page`
|
|
69
|
+
to display a page.
|
|
70
|
+
The list supports type-to-filter, `/` to find, Ctrl+N/Ctrl+P to move between
|
|
71
|
+
find matches, and F1 for an interactive tutorial. The interface uses terminal
|
|
72
|
+
colors when available and falls back to monochrome attributes otherwise.
|
|
73
|
+
|
|
74
|
+
Page lookup uses `MANPATH` when set, then checks the common locations
|
|
75
|
+
`/usr/share/man`, `/usr/local/share/man`, `/usr/man`, and `/usr/local/man`.
|
|
76
|
+
Plain, gzip, bzip2, and xz-compressed pages are supported when the corresponding
|
|
77
|
+
Python standard-library codec is available.
|
|
78
|
+
|
|
79
|
+
Requests that are not supported locally and pages with unsupported roff
|
|
80
|
+
constructs are passed through to `zotman`. If `zotman` is not installed, zman
|
|
81
|
+
prints an error and exits with status 127.
|
|
82
|
+
|
|
83
|
+
This initial viewer does not implement Qman's full page navigation, in-page
|
|
84
|
+
search, or hyperlinks.
|
|
85
|
+
|
|
86
|
+
The page-selection screen requires a Python build with the standard-library
|
|
87
|
+
`curses` module. Direct page display does not require curses.
|
|
88
|
+
|
|
89
|
+
## Development
|
|
90
|
+
|
|
91
|
+
Run the test suite without installing the project:
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
PYTHONPATH=src python3 -m unittest discover -s tests
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
The project's `pyproject.toml` also supports package builds for environments
|
|
98
|
+
that use pip, but pip is not required to install or run the program.
|
zman-0.1/man/zman.1
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
.TH ZMAN 1 "November 2026" "zman 0.1" "User Commands"
|
|
2
|
+
.SH NAME
|
|
3
|
+
zman, man \- view manual pages with a zotman fallback
|
|
4
|
+
.SH SYNOPSIS
|
|
5
|
+
.B man
|
|
6
|
+
[\fIsection\fR] \fIpage\fR
|
|
7
|
+
.br
|
|
8
|
+
.B zman
|
|
9
|
+
[\fB\-m\fR \fIpage\fR | \fB\-k\fR \fIkeyword\fR | \fB\-kw\fR \fIword\fR]
|
|
10
|
+
.br
|
|
11
|
+
.B zman
|
|
12
|
+
[\fIsection\fR] \fIpage\fR
|
|
13
|
+
.br
|
|
14
|
+
.B zman
|
|
15
|
+
\fIpath-to-page\fR
|
|
16
|
+
.br
|
|
17
|
+
.B zman
|
|
18
|
+
.SH DESCRIPTION
|
|
19
|
+
.B zman
|
|
20
|
+
is a lightweight manual-page viewer for z/OS UNIX. It renders plain text and
|
|
21
|
+
a subset of common roff man-page macros.
|
|
22
|
+
.PP
|
|
23
|
+
With no page argument, zman obtains the page list by running
|
|
24
|
+
.B "man -k ."
|
|
25
|
+
It displays the results in a selectable list that can be filtered and searched
|
|
26
|
+
interactively. The
|
|
27
|
+
.B man
|
|
28
|
+
command is an alias for
|
|
29
|
+
.BR zman .
|
|
30
|
+
If a request or page cannot be rendered, zman invokes
|
|
31
|
+
.B zotman
|
|
32
|
+
with the original arguments.
|
|
33
|
+
.SH OPTIONS
|
|
34
|
+
.TP
|
|
35
|
+
.B \-m, \-\-manpage page
|
|
36
|
+
Display the named manual page.
|
|
37
|
+
.TP
|
|
38
|
+
.B \-k, \-\-keyword keyword
|
|
39
|
+
Display a selectable list of pages matching the keyword.
|
|
40
|
+
.TP
|
|
41
|
+
.B \-kw word
|
|
42
|
+
Display a selectable list of pages where the word occurs as a whole word in
|
|
43
|
+
the page name or description.
|
|
44
|
+
.TP
|
|
45
|
+
.B \-h, \-\-help
|
|
46
|
+
Display command usage and exit.
|
|
47
|
+
.TP
|
|
48
|
+
.B \-V, \-\-version
|
|
49
|
+
Display the version and exit.
|
|
50
|
+
.SH SELECTION LIST
|
|
51
|
+
Type characters to filter the list. Use Backspace to remove a character and
|
|
52
|
+
Ctrl+U to clear the filter. Press
|
|
53
|
+
.B /
|
|
54
|
+
to enter a find term in the filtered list; Ctrl+N and Ctrl+P move to the next
|
|
55
|
+
and previous match.
|
|
56
|
+
.PP
|
|
57
|
+
Use Up/Down or Page Up/Page Down to move through the list. Press Enter to open
|
|
58
|
+
the selected page, q or Esc to exit, or F1 to display the interactive tutorial.
|
|
59
|
+
The interface uses colors when supported by the terminal.
|
|
60
|
+
.SH PAGE LOOKUP
|
|
61
|
+
Page lookup uses
|
|
62
|
+
.B MANPATH
|
|
63
|
+
when set. Otherwise, zman checks
|
|
64
|
+
.IR /usr/share/man ,
|
|
65
|
+
.IR /usr/local/share/man ,
|
|
66
|
+
.IR /usr/man ,
|
|
67
|
+
and
|
|
68
|
+
.IR /usr/local/man .
|
|
69
|
+
Plain, gzip, bzip2, and xz-compressed pages are supported when the Python
|
|
70
|
+
standard-library codec is available.
|
|
71
|
+
.SH FALLBACK
|
|
72
|
+
Unsupported requests, missing pages, unsupported roff syntax, and page
|
|
73
|
+
read/decompression errors are passed to
|
|
74
|
+
.BR zotman .
|
|
75
|
+
For page listing, zman tries a separate
|
|
76
|
+
.B man
|
|
77
|
+
command with
|
|
78
|
+
.BR \-k ;
|
|
79
|
+
if unavailable or unusable, it tries
|
|
80
|
+
.BR zotman \-k .
|
|
81
|
+
The exit status is the status returned by zotman. If zotman is not on
|
|
82
|
+
.BR PATH ,
|
|
83
|
+
zman reports the error and exits with status 127.
|
|
84
|
+
.SH INSTALLATION
|
|
85
|
+
Install with the project's
|
|
86
|
+
.B install.py
|
|
87
|
+
script. On z/OS USS, install the zopen
|
|
88
|
+
.B man-db
|
|
89
|
+
package and run
|
|
90
|
+
.B mandb
|
|
91
|
+
to index the installed manual page. Tag installed scripts and Python sources
|
|
92
|
+
as ISO8859-1; see the project README for the commands.
|
|
93
|
+
.SH REQUIREMENTS
|
|
94
|
+
Python 3.9 or newer. The selection list requires a Python build with the
|
|
95
|
+
.B curses
|
|
96
|
+
module. The viewer otherwise uses the Python standard library.
|
|
97
|
+
.SH LICENSE
|
|
98
|
+
Distributed under the MIT License.
|
zman-0.1/pyproject.toml
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "zman"
|
|
7
|
+
version = "0.1"
|
|
8
|
+
description = "A Python man-page viewer for z/OS UNIX with zotman fallback"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "zman contributors" }]
|
|
13
|
+
|
|
14
|
+
[project.scripts]
|
|
15
|
+
zman = "zman.zmanp:main"
|
|
16
|
+
man = "zman.zmanp:main"
|
|
17
|
+
|
|
18
|
+
[tool.setuptools]
|
|
19
|
+
package-dir = { "" = "src" }
|
|
20
|
+
|
|
21
|
+
[tool.setuptools.packages.find]
|
|
22
|
+
where = ["src"]
|
|
23
|
+
|
|
24
|
+
[tool.setuptools.data-files]
|
|
25
|
+
"share/man/man1" = ["man/zman.1"]
|
zman-0.1/setup.cfg
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"""Rendering for plain-text pages and a conservative subset of roff."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import gzip
|
|
6
|
+
import re
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class UnsupportedPage(ValueError):
|
|
11
|
+
"""Raised when a page uses syntax this renderer cannot safely handle."""
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
_INLINE_ESCAPES = re.compile(
|
|
15
|
+
r"\\(?:f\[[^]]+\]|f.|[\\&\-~ 0e]|\(.{2}|\[[^]]+\])"
|
|
16
|
+
)
|
|
17
|
+
_UNKNOWN_ESCAPE = re.compile(r"\\(?:[A-Za-z]|\(.{2}|\[[^]]+\])")
|
|
18
|
+
_NAMED_CHARS = {
|
|
19
|
+
"em": "\u2014",
|
|
20
|
+
"en": "\u2013",
|
|
21
|
+
"bu": "*",
|
|
22
|
+
"co": "(C)",
|
|
23
|
+
"lq": '"',
|
|
24
|
+
"rq": '"',
|
|
25
|
+
}
|
|
26
|
+
_MACROS_WITH_TEXT = {"B", "I", "SM", "SB", "BR", "BI", "IR", "RB", "RI"}
|
|
27
|
+
_SUPPORTED_MACROS = _MACROS_WITH_TEXT | {
|
|
28
|
+
"TH", "SH", "SS", "P", "PP", "LP", "TP", "IP", "br", "sp",
|
|
29
|
+
"nf", "fi", "RS", "RE", "EX", "EE",
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _decode_page(data: bytes) -> str:
|
|
34
|
+
import locale
|
|
35
|
+
|
|
36
|
+
encodings = [locale.getpreferredencoding(False), "utf-8", "cp1047", "latin-1"]
|
|
37
|
+
for encoding in dict.fromkeys(encodings):
|
|
38
|
+
try:
|
|
39
|
+
return data.decode(encoding)
|
|
40
|
+
except (LookupError, UnicodeDecodeError):
|
|
41
|
+
continue
|
|
42
|
+
return data.decode("utf-8", errors="replace")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def read_page(path: Path) -> str:
|
|
46
|
+
"""Read a possibly compressed manual page as text."""
|
|
47
|
+
data = path.read_bytes()
|
|
48
|
+
suffix = path.suffix.lower()
|
|
49
|
+
if suffix == ".gz":
|
|
50
|
+
try:
|
|
51
|
+
data = gzip.decompress(data)
|
|
52
|
+
except (OSError, EOFError) as exc:
|
|
53
|
+
raise UnsupportedPage(f"could not decompress {path}: {exc}") from exc
|
|
54
|
+
elif suffix == ".bz2":
|
|
55
|
+
try:
|
|
56
|
+
import bz2
|
|
57
|
+
except ImportError as exc:
|
|
58
|
+
raise UnsupportedPage("bzip2 support is unavailable") from exc
|
|
59
|
+
try:
|
|
60
|
+
data = bz2.decompress(data)
|
|
61
|
+
except (OSError, EOFError) as exc:
|
|
62
|
+
raise UnsupportedPage(f"could not decompress {path}: {exc}") from exc
|
|
63
|
+
elif suffix == ".xz":
|
|
64
|
+
try:
|
|
65
|
+
import lzma
|
|
66
|
+
except ImportError as exc:
|
|
67
|
+
raise UnsupportedPage("xz support is unavailable") from exc
|
|
68
|
+
try:
|
|
69
|
+
data = lzma.decompress(data)
|
|
70
|
+
except (OSError, EOFError, lzma.LZMAError) as exc:
|
|
71
|
+
raise UnsupportedPage(f"could not decompress {path}: {exc}") from exc
|
|
72
|
+
return _decode_page(data)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _inline(text: str) -> str:
|
|
76
|
+
def replace(match: re.Match[str]) -> str:
|
|
77
|
+
escape = match.group(0)
|
|
78
|
+
if escape.startswith(r"\f"):
|
|
79
|
+
return ""
|
|
80
|
+
if escape == r"\&":
|
|
81
|
+
return ""
|
|
82
|
+
if escape == r"\-":
|
|
83
|
+
return "-"
|
|
84
|
+
if escape in (r"\ ", r"\~", r"\0"):
|
|
85
|
+
return " "
|
|
86
|
+
if escape == r"\e":
|
|
87
|
+
return "\\"
|
|
88
|
+
if escape == r"\\":
|
|
89
|
+
return "\\"
|
|
90
|
+
if escape.startswith(r"\("):
|
|
91
|
+
return _NAMED_CHARS.get(escape[2:], "")
|
|
92
|
+
if escape.startswith(r"\["):
|
|
93
|
+
name = escape[2:-1]
|
|
94
|
+
if name in _NAMED_CHARS:
|
|
95
|
+
return _NAMED_CHARS[name]
|
|
96
|
+
raise UnsupportedPage(f"unsupported roff character escape: {escape}")
|
|
97
|
+
return escape
|
|
98
|
+
|
|
99
|
+
result = _INLINE_ESCAPES.sub(replace, text)
|
|
100
|
+
unknown = _UNKNOWN_ESCAPE.search(result)
|
|
101
|
+
if unknown:
|
|
102
|
+
raise UnsupportedPage(f"unsupported roff escape: {unknown.group(0)}")
|
|
103
|
+
if r"\*" in result:
|
|
104
|
+
raise UnsupportedPage("roff string references are not supported")
|
|
105
|
+
return result
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _arguments(text: str) -> str:
|
|
109
|
+
return text.strip().strip('"')
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def render_page(text: str) -> str:
|
|
113
|
+
"""Render a plain-text page or common man macros to terminal-friendly text."""
|
|
114
|
+
text = text.replace("\r\n", "\n").replace("\r", "\n").replace("\x85", "\n")
|
|
115
|
+
if not text.strip():
|
|
116
|
+
raise UnsupportedPage("the manual page is empty")
|
|
117
|
+
|
|
118
|
+
# Cat pages use backspace overstrikes for emphasis; keep the visible glyph.
|
|
119
|
+
if not any(line.startswith((".", "'")) for line in text.splitlines()):
|
|
120
|
+
plain = re.sub(r".\x08", "", text)
|
|
121
|
+
plain = plain.replace("\x08", "")
|
|
122
|
+
return plain.rstrip() + "\n"
|
|
123
|
+
|
|
124
|
+
output: list[str] = []
|
|
125
|
+
indent = 0
|
|
126
|
+
pending_tag = False
|
|
127
|
+
for line in text.splitlines():
|
|
128
|
+
if line.startswith((".", "'")):
|
|
129
|
+
command = line[1:].strip()
|
|
130
|
+
if not command:
|
|
131
|
+
output.append("")
|
|
132
|
+
continue
|
|
133
|
+
parts = command.split(None, 1)
|
|
134
|
+
macro = parts[0]
|
|
135
|
+
argument = parts[1] if len(parts) == 2 else ""
|
|
136
|
+
if macro not in _SUPPORTED_MACROS:
|
|
137
|
+
raise UnsupportedPage(f"unsupported roff macro: .{macro}")
|
|
138
|
+
|
|
139
|
+
if macro in {"TH", "nf", "fi", "EX", "EE"}:
|
|
140
|
+
continue
|
|
141
|
+
if macro in {"SH", "SS"}:
|
|
142
|
+
heading = _inline(_arguments(argument))
|
|
143
|
+
if output and output[-1]:
|
|
144
|
+
output.append("")
|
|
145
|
+
output.extend((heading.upper(), ""))
|
|
146
|
+
elif macro in {"P", "PP", "LP", "sp", "br"}:
|
|
147
|
+
if output and output[-1]:
|
|
148
|
+
output.append("")
|
|
149
|
+
elif macro == "TP":
|
|
150
|
+
pending_tag = True
|
|
151
|
+
elif macro == "IP":
|
|
152
|
+
label = _inline(_arguments(argument))
|
|
153
|
+
if label:
|
|
154
|
+
output.append(f"{' ' * (indent * 4)}{label}")
|
|
155
|
+
pending_tag = True
|
|
156
|
+
elif macro == "RS":
|
|
157
|
+
indent += 1
|
|
158
|
+
elif macro == "RE":
|
|
159
|
+
indent = max(0, indent - 1)
|
|
160
|
+
elif macro in _MACROS_WITH_TEXT:
|
|
161
|
+
value = _inline(_arguments(argument))
|
|
162
|
+
if value:
|
|
163
|
+
output.append(f"{' ' * (indent * 4)}{value}")
|
|
164
|
+
continue
|
|
165
|
+
|
|
166
|
+
value = _inline(line)
|
|
167
|
+
if pending_tag:
|
|
168
|
+
output.append(f"{' ' * (indent * 4)}{value}")
|
|
169
|
+
pending_tag = False
|
|
170
|
+
elif value:
|
|
171
|
+
output.append(f"{' ' * (indent * 4)}{value}")
|
|
172
|
+
elif output and output[-1]:
|
|
173
|
+
output.append("")
|
|
174
|
+
|
|
175
|
+
rendered = "\n".join(output).rstrip()
|
|
176
|
+
if not rendered:
|
|
177
|
+
raise UnsupportedPage("the manual page contains no printable text")
|
|
178
|
+
return rendered + "\n"
|