@@ -16,13 +16,13 @@ Pandoc must be [installed](https://pandoc.org/installing.html) and available as
16
16
17
17
With ` pip ` :
18
18
``` bash
19
- pip install mkdocs-manpage
19
+ pip install mkdocs-manpage[preprocess]
20
20
```
21
21
22
22
With [ ` pipx ` ] ( https://github.com/pipxproject/pipx ) :
23
23
``` bash
24
24
python3.7 -m pip install --user pipx
25
- pipx install mkdocs-manpage
25
+ pipx install mkdocs-manpage[preprocess]
26
26
```
27
27
28
28
## Usage
@@ -31,15 +31,85 @@ pipx install mkdocs-manpage
31
31
# mkdocs.yml
32
32
plugins :
33
33
- manpage :
34
- enabled : !ENV [MANPAGE, false]
35
34
pages :
36
35
- index.md
37
36
- usage.md
38
37
- reference/api.md
39
38
` ` `
40
39
41
- We also recommend disabling some options from other plugins/extensions
42
- to improve the final manual page:
40
+ To enable/disable the plugin with an environment variable:
41
+
42
+ ` ` ` yaml
43
+ # mkdocs.yml
44
+ plugins :
45
+ - manpage :
46
+ enabled : !ENV [MANPAGE, false]
47
+ ` ` `
48
+
49
+ Then set the environment variable and run MkDocs:
50
+
51
+ ` ` ` bash
52
+ MANPAGE=true mkdocs build
53
+ ```
54
+
55
+ The manpage will be written into the root of the site directory
56
+ and named ` manpage.1 ` .
57
+
58
+ ### Pre-processing HTML
59
+
60
+ This plugin works by concatenating the HTML from all selected pages
61
+ into a single file that is then converted to a manual page using Pandoc.
62
+
63
+ With a complete conversion, the final manual page will not look so good.
64
+ For example images and SVG will be rendered as long strings of data and/or URIs.
65
+ So this plugin allows users to pre-process the HTML, to remove unwanted
66
+ HTML elements before converting the whole thing to a manpage.
67
+
68
+ First, you must make sure to install the ` preprocess ` extra:
69
+
70
+ ``` bash
71
+ pip install mkdocs-manpage[preprocess]
72
+ ```
73
+
74
+ To pre-process the HTML, we use [ BeautifulSoup] ( https://pypi.org/project/beautifulsoup4/ ) .
75
+ Users have to write their own ` preprocess ` function in order to modify the soup
76
+ returned by BeautifulSoup:
77
+
78
+ ``` python title="scripts/preprocess.py"
79
+ from bs4 import BeautifulSoup, Tag
80
+
81
+
82
+ def to_remove (tag : Tag) -> bool :
83
+ # remove images and SVGs
84
+ if tag.name in {" img" , " svg" }:
85
+ return True
86
+ # remove links containing images or SVGs
87
+ if tag.name == " a" and tag.img and to_remove(tag.img):
88
+ return True
89
+ # remove permalinks
90
+ if tag.name == " a" and " headerlink" in tag.get(" class" , ()):
91
+ return True
92
+ return False
93
+
94
+
95
+ def preprocess (soup : BeautifulSoup) -> None :
96
+ for element in soup.find_all(to_remove):
97
+ element.decompose()
98
+ ```
99
+
100
+ Then, instruct the plugin to use this module and its ` preprocess ` function:
101
+
102
+ ``` yaml title="mkdocs.yml"
103
+ plugins :
104
+ - manpage :
105
+ preprocess : scripts/preprocess.py
106
+ ` ` `
107
+
108
+ See the documentation of both [` BeautifulSoup`][bs4.BeautifulSoup] and [`Tag`][bs4.Tag]
109
+ to know what methods are available to correctly select the elements to remove.
110
+
111
+ The alternative to HTML processing for improving the final manpage
112
+ is disabling some options from other plugins/extensions :
43
113
44
114
- no source code through `mkdocstrings` :
45
115
@@ -67,5 +137,4 @@ export MANPAGE=true
67
137
export PERMALINK=false
68
138
export SHOW_SOURCE=false
69
139
mkdocs build
70
- # manpage is in site dir: ./site/manpage.1
71
140
` ` `
0 commit comments