hurl/bin/release/gen_manpage.py

87 lines
1.8 KiB
Python
Raw Normal View History

2020-08-27 10:07:46 +03:00
#!/usr/bin/env python3
2022-08-16 13:08:14 +03:00
"""Build Hurl Man File.
This script creates Hurl man file from a Markdown source.
This tool takes the Hurl man Markdown source file as a first argument.
Examples:
2022-10-24 21:58:56 +03:00
$ python3 bin/release/gen_manpage.py docs/manual/hurl.md > docs/manual/hurl.1
$ python3 bin/release/gen_manpage.py docs/manual/hurlfmt.md > docs/manual/hurlfmt.1
2022-08-16 13:08:14 +03:00
"""
2020-08-27 10:07:46 +03:00
import sys
import re
from datetime import date
2020-08-27 10:07:46 +03:00
def header(version, date):
return '.TH hurl 1 "%s" "hurl %s" " Hurl Manual"' % (
date.strftime("%d %b %Y"),
version,
)
2020-08-27 10:07:46 +03:00
def version():
2020-10-23 18:30:01 +03:00
p = re.compile('version = "(.*)"')
for line in open("packages/hurl/Cargo.toml", "r").readlines():
2020-10-23 18:30:01 +03:00
m = p.match(line)
if m:
return m.group(1)
return None
2020-08-27 10:07:46 +03:00
def process_code_block(s):
output = ""
2020-10-23 18:30:01 +03:00
indent = False
for line in s.split("\n"):
if indent and line.startswith("```"):
2020-10-23 18:30:01 +03:00
indent = False
elif not indent and line.startswith("```"):
2020-10-23 18:30:01 +03:00
indent = True
else:
if line != "":
2020-10-23 18:30:01 +03:00
if indent:
output += " "
2020-10-23 18:30:01 +03:00
output += line
output += "\n"
2020-10-23 18:30:01 +03:00
return output
# p.sub('\\\\f[C]\\1\\\\f[R]', s)
2020-08-27 10:07:46 +03:00
def convert_md(s):
p = re.compile("^###\s+(.*)")
s = p.sub('.IP "\\1"', s)
2020-08-27 10:07:46 +03:00
p = re.compile("^##")
s = p.sub(".SH", s)
2020-08-27 10:07:46 +03:00
p = re.compile("\*\*(.*)\*\*\s+")
s = p.sub(".B \\1\n", s)
2020-08-27 10:07:46 +03:00
# Remove link Text
2022-08-16 13:08:14 +03:00
p = re.compile("\[`?(.*?)`?\]\(.*?\)")
s = p.sub("\\\\fI\\1\\\\fP", s)
2020-08-27 10:07:46 +03:00
# Remove local anchor
p = re.compile("{#.*}")
s = p.sub("", s)
return s
2020-08-27 10:07:46 +03:00
def main():
input_file = sys.argv[1]
data = open(input_file).readlines()
print(header(version(), date.today()))
2020-08-27 10:07:46 +03:00
s = "".join([convert_md(line) for line in data])
2020-10-23 18:30:01 +03:00
s = process_code_block(s)
2020-08-27 10:07:46 +03:00
print(s)
if __name__ == "__main__":
2020-08-27 10:07:46 +03:00
main()