aboutsummaryrefslogtreecommitdiff
path: root/hack/markdown-preprocess
diff options
context:
space:
mode:
Diffstat (limited to 'hack/markdown-preprocess')
-rwxr-xr-xhack/markdown-preprocess80
1 files changed, 52 insertions, 28 deletions
diff --git a/hack/markdown-preprocess b/hack/markdown-preprocess
index 6d2675cc4..86667a32d 100755
--- a/hack/markdown-preprocess
+++ b/hack/markdown-preprocess
@@ -5,6 +5,7 @@
import glob
import os
+import re
import sys
def main():
@@ -28,11 +29,11 @@ def process(infile):
# Some options are the same between containers and pods; determine
# which description to use from the name of the source man page.
pod_or_container = 'container'
- if '-pod-' in infile:
+ if '-pod-' in infile or '-kube-' in infile:
pod_or_container = 'pod'
# Sometimes a man page includes the subcommand.
- subcommand = removesuffix(removeprefix(infile,"podman-"),".1.md.in").replace("-", " ")
+ subcommand = podman_subcommand(infile)
# foo.md.in -> foo.md -- but always write to a tmpfile
outfile = os.path.splitext(infile)[0]
@@ -46,42 +47,65 @@ def process(infile):
if line.startswith('@@option '):
_, optionname = line.strip().split(" ")
optionfile = os.path.join("options", optionname + '.md')
- fh_out.write("[//]: # (BEGIN included file " + optionfile + ")\n")
+
+ # Comment intended to help someone viewing the .md file.
+ # Leading newline is important because if two lines are
+ # consecutive without a break, sphinx (but not go-md2man)
+ # treats them as one line and will unwantedly render the
+ # comment in its output.
+ fh_out.write("\n[//]: # (BEGIN included file " + optionfile + ")\n")
with open(optionfile, 'r') as fh_optfile:
for opt_line in fh_optfile:
- opt_line = opt_line.replace('<POD-OR-CONTAINER>', pod_or_container)
- opt_line = opt_line.replace('<SUBCOMMAND>', subcommand)
+ opt_line = replace_type(opt_line, pod_or_container)
+ opt_line = opt_line.replace('<<subcommand>>', subcommand)
fh_out.write(opt_line)
-
- # Weird special case: options/image-volume.md ends in a
- # list, and in markdown lists are continued across lines,
- # so without an intervening blank line the '[//]' comment
- # becomes part of the final list entry.
- if opt_line.startswith('-'):
- fh_out.write("\n")
-
- fh_out.write("[//]: # (END included file " + optionfile + ")\n")
+ fh_out.write("\n[//]: # (END included file " + optionfile + ")\n")
else:
fh_out.write(line)
os.chmod(outfile_tmp, 0o444)
os.rename(outfile_tmp, outfile)
-# str.removeprefix() is python 3.9+, we need to support older versions on mac
-def removeprefix(string: str, prefix: str) -> str:
- if string.startswith(prefix):
- return string[len(prefix):]
- else:
- return string[:]
-
-# str.removesuffix() is python 3.9+, we need to support older versions on mac
-def removesuffix(string: str, suffix: str) -> str:
- # suffix='' should not call self[:-0].
- if suffix and string.endswith(suffix):
- return string[:-len(suffix)]
- else:
- return string[:]
+# Given a file path of the form podman-foo-bar.1.md.in, return "foo bar"
+def podman_subcommand(string: str) -> str:
+ if string.startswith("podman-"):
+ string = string[len("podman-"):]
+ if string.endswith(".1.md.in"):
+ string = string[:-len(".1.md.in")]
+ return string.replace("-", " ")
+
+# Replace instances of '<<pod|container>>' with the desired one (based on
+# 'type' which is 'pod' or 'container').
+def replace_type(line: str, type: str) -> str:
+ # Internal helper function: determines the desired half of the <a|b> string
+ def replwith(matchobj):
+ lhs, rhs = matchobj[0].split('|')
+ # Strip off '<<' and '>>'
+ lhs = lhs[2:]
+ rhs = rhs[:len(rhs)-2]
+
+ # Check both sides for 'pod' followed by (non-"m" or end-of-string).
+ # The non-m prevents us from triggering on 'podman', which could
+ # conceivably be present in both sides. And we check for 'pod',
+ # not 'container', because it's possible to have something like
+ # <<container in pod|container>>.
+ if re.match('pod([^m]|$)', lhs, re.IGNORECASE):
+ if re.match('pod([^m]|$)', rhs, re.IGNORECASE):
+ raise Exception("'%s' matches 'pod' in both left and right sides" % matchobj[0])
+ # Only left-hand side has "pod"
+ if type == 'pod':
+ return lhs
+ else:
+ return rhs
+ else:
+ if not re.match('pod([^m]|$)', rhs, re.IGNORECASE):
+ raise Exception("'%s' does not match 'pod' in either side" % matchobj[0])
+ if type == 'pod':
+ return rhs
+ else:
+ return lhs
+ return re.sub('<<[^\|>]+\|[^\|>]+>>', replwith, line)
if __name__ == "__main__":
main()