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
|
2020-11-16 10:29:05 +03:00
|
|
|
from datetime import date
|
2020-08-27 10:07:46 +03:00
|
|
|
|
|
|
|
|
2020-11-16 10:29:05 +03:00
|
|
|
def header(version, date):
|
2022-02-05 13:13:49 +03:00
|
|
|
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 = "(.*)"')
|
2022-02-05 13:13:49 +03:00
|
|
|
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):
|
2022-02-05 13:13:49 +03:00
|
|
|
output = ""
|
2020-10-23 18:30:01 +03:00
|
|
|
indent = False
|
2022-02-05 13:13:49 +03:00
|
|
|
for line in s.split("\n"):
|
|
|
|
if indent and line.startswith("```"):
|
2020-10-23 18:30:01 +03:00
|
|
|
indent = False
|
2022-02-05 13:13:49 +03:00
|
|
|
elif not indent and line.startswith("```"):
|
2020-10-23 18:30:01 +03:00
|
|
|
indent = True
|
|
|
|
else:
|
2022-02-05 13:13:49 +03:00
|
|
|
if line != "":
|
2020-10-23 18:30:01 +03:00
|
|
|
if indent:
|
2022-02-05 13:13:49 +03:00
|
|
|
output += " "
|
2020-10-23 18:30:01 +03:00
|
|
|
output += line
|
2022-02-05 13:13:49 +03:00
|
|
|
output += "\n"
|
2020-10-23 18:30:01 +03:00
|
|
|
|
|
|
|
return output
|
2022-02-05 13:13:49 +03:00
|
|
|
# p.sub('\\\\f[C]\\1\\\\f[R]', s)
|
2020-08-27 10:07:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
def convert_md(s):
|
|
|
|
|
2022-02-05 13:13:49 +03:00
|
|
|
p = re.compile("^###\s+(.*)")
|
|
|
|
s = p.sub('.IP "\\1"', s)
|
2020-08-27 10:07:46 +03:00
|
|
|
|
2022-02-05 13:13:49 +03:00
|
|
|
p = re.compile("^##")
|
|
|
|
s = p.sub(".SH", s)
|
2020-08-27 10:07:46 +03:00
|
|
|
|
2022-02-05 13:13:49 +03:00
|
|
|
p = re.compile("\*\*(.*)\*\*\s+")
|
|
|
|
s = p.sub(".B \\1\n", s)
|
2020-08-27 10:07:46 +03:00
|
|
|
|
2022-02-05 13:13:49 +03:00
|
|
|
# Remove link Text
|
2022-08-16 13:08:14 +03:00
|
|
|
p = re.compile("\[`?(.*?)`?\]\(.*?\)")
|
2022-02-05 13:13:49 +03:00
|
|
|
s = p.sub("\\\\fI\\1\\\\fP", s)
|
2020-08-27 10:07:46 +03:00
|
|
|
|
2022-02-05 13:13:49 +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()
|
2020-11-16 10:29:05 +03:00
|
|
|
print(header(version(), date.today()))
|
2020-08-27 10:07:46 +03:00
|
|
|
|
2022-02-05 13:13:49 +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)
|
|
|
|
|
|
|
|
|
2022-02-05 13:13:49 +03:00
|
|
|
if __name__ == "__main__":
|
2020-08-27 10:07:46 +03:00
|
|
|
main()
|