summaryrefslogtreecommitdiff
path: root/md2man
diff options
context:
space:
mode:
authorWayne Davison <wayne@opencoder.net>2020-06-09 00:23:15 -0700
committerWayne Davison <wayne@opencoder.net>2020-06-09 00:58:07 -0700
commit6dc94e39a766d8b667204b677cde2c7ca55f6f29 (patch)
tree2f4f9ccdcab68074112453959230bd9d155bfe87 /md2man
parent8146b04ffbbd9e334380b6f54ed6a2b04b3a10ce (diff)
downloadrsync-6dc94e39a766d8b667204b677cde2c7ca55f6f29.tar.gz
Output the files at the end; fix a missing double-quote.
Diffstat (limited to 'md2man')
-rwxr-xr-xmd2man157
1 files changed, 78 insertions, 79 deletions
diff --git a/md2man b/md2man
index 02c154b8..c0ccc7a7 100755
--- a/md2man
+++ b/md2man
@@ -57,26 +57,28 @@ NORM_FONT = ('\1', r"\fP")
BOLD_FONT = ('\2', r"\fB")
ULIN_FONT = ('\3', r"\fI")
-env_subs = { }
-
def main():
- mtime = None
-
fi = re.match(r'^(?P<fn>(?P<srcdir>.+/)?(?P<name>(?P<prog>[^/]+)\.(?P<sect>\d+))\.md)$', args.mdfile)
if not fi:
die('Failed to parse NAME.NUM.md out of input file:', args.mdfile)
fi = argparse.Namespace(**fi.groupdict())
+
if not fi.srcdir:
fi.srcdir = './'
+ fi.title = fi.prog + '(' + fi.sect + ') man page'
+ fi.date = None
+
chk_files = 'latest-year.h Makefile'.split()
for fn in chk_files:
try:
st = os.lstat(fi.srcdir + fn)
except:
die('Failed to find', fi.srcdir + fn)
- if not mtime:
- mtime = st.st_mtime
+ if not fi.date:
+ fi.date = time.strftime('%d %b %Y', time.localtime(st.st_mtime))
+
+ env_subs = { }
with open(fi.srcdir + 'Makefile', 'r', encoding='utf-8') as fh:
for line in fh:
@@ -90,91 +92,89 @@ def main():
if var == 'VERSION':
break
- MarkdownToManPage(fi, mtime)
+ with open(fi.fn, 'r', encoding='utf-8') as fh:
+ txt = re.sub(r'@VERSION@', env_subs['VERSION'], fh.read())
+ txt = re.sub(r'@LIBDIR@', env_subs['libdir'], txt)
+ fi.html_in = cmarkgfm.github_flavored_markdown_to_html(txt)
+ txt = None
+
+ HtmlToManPage(fi)
+
+ if args.test:
+ print("The test was successful.")
+ return
+
+ fn = fi.name + '.html'
+ print("Outputing HTML page:", fn)
+ with open(fn, 'w', encoding='utf-8') as fh:
+ fh.write(fi.html_out)
+ fn = fi.name
+ print("Outputing man page:", fn)
+ with open(fn, 'w', encoding='utf-8') as fh:
+ fh.write(fi.man_out)
-class MarkdownToManPage(HTMLParser):
- def __init__(self, fi, mtime):
+
+class HtmlToManPage(HTMLParser):
+ def __init__(self, fi):
HTMLParser.__init__(self, convert_charrefs=True)
- self.man_fh = self.html_fh = None
self.state = argparse.Namespace(
list_state = [ ],
p_macro = ".P\n",
- first_li_tag = False,
- first_dd_tag = False,
+ at_first_tag_in_li = False,
+ at_first_tag_in_dd = False,
dt_from = None,
in_pre = False,
txt = '',
)
- self.date = time.strftime('%d %b %Y', time.localtime(mtime))
-
- with open(fi.fn, 'r', encoding='utf-8') as fh:
- txt = re.sub(r'@VERSION@', env_subs['VERSION'], fh.read())
- txt = re.sub(r'@LIBDIR@', env_subs['libdir'], txt)
- html = cmarkgfm.github_flavored_markdown_to_html(txt)
- txt = None
+ self.html_out = [ HTML_START % fi.title ]
+ self.man_out = [ MAN_START % (fi.prog, fi.sect, fi.date) ]
- if args.test:
- self.html_fh = open(os.devnull, 'w', encoding='utf-8')
- self.man_fh = self.html_fh
- else:
- self.html_fn = fi.name + '.html'
- self.html_fh = open(self.html_fn, 'w', encoding='utf-8')
- self.html_fh.write(HTML_START % (fi.prog + '(' + fi.sect + ') man page'))
+ self.feed(fi.html_in)
+ fi.html_in = None
- self.man_fn = fi.name
- self.man_fh = open(self.man_fn, 'w', encoding='utf-8')
- self.man_fh.write(MAN_START % (fi.prog, fi.sect, self.date))
+ self.html_out.append(HTML_END % fi.date)
+ self.man_out.append(MAN_END)
- self.feed(html)
+ fi.html_out = ''.join(self.html_out)
+ self.html_out = None
- def __del__(self):
- if args.test:
- print("The test was successful.")
- return
+ fi.man_out = ''.join(self.man_out)
+ self.man_out = None
- if self.html_fh:
- self.html_fh.write(HTML_END % self.date)
- self.html_fh.close()
- print("Output HTML page: ", self.html_fn)
-
- if self.man_fh:
- self.man_fh.write(MAN_END)
- self.man_fh.close()
- print("Output man page: ", self.man_fn)
def handle_starttag(self, tag, attrs_list):
st = self.state
if args.debug:
print('START', tag, attrs_list, st)
- if st.first_li_tag:
+ if st.at_first_tag_in_li:
if st.list_state[-1] == 'dl':
st.dt_from = tag
if tag == 'p':
tag = 'dt'
else:
- self.html_fh.write('<dt>')
- st.first_li_tag = False
+ self.html_out.append('<dt>')
+ st.at_first_tag_in_li = False
if tag == 'p':
- if not st.first_dd_tag:
- self.man_fh.write(st.p_macro)
+ if not st.at_first_tag_in_dd:
+ self.man_out.append(st.p_macro)
elif tag == 'li':
- st.first_li_tag = True
+ st.at_first_tag_in_li = True
lstate = st.list_state[-1]
if lstate == 'dl':
return
if lstate == 'o':
- self.man_fh.write(".IP o\n")
+ self.man_out.append(".IP o\n")
else:
- self.man_fh.write(".IP " + str(lstate) + ".\n")
+ self.man_out.append(".IP " + str(lstate) + ".\n")
st.list_state[-1] += 1
elif tag == 'blockquote':
- self.man_fh.write(".RS 4\n")
+ self.man_out.append(".RS 4\n")
elif tag == 'pre':
st.in_pre = True
- self.man_fh.write(st.p_macro + ".nf\n")
+ self.man_out.append(st.p_macro + ".nf\n")
elif tag == 'code' and not st.in_pre:
st.txt += BOLD_FONT[0]
elif tag == 'strong' or tag == 'bold':
@@ -188,26 +188,24 @@ class MarkdownToManPage(HTMLParser):
start = int(val) # We only support integers.
break
if st.list_state:
- self.man_fh.write(".RS\n")
+ self.man_out.append(".RS\n")
if start == 0:
tag = 'dl'
attrs_list = [ ]
st.list_state.append('dl')
else:
st.list_state.append(start)
- self.man_fh.write(st.p_macro)
+ self.man_out.append(st.p_macro)
st.p_macro = ".IP\n"
elif tag == 'ul':
- self.man_fh.write(st.p_macro)
+ self.man_out.append(st.p_macro)
if st.list_state:
- self.man_fh.write(".RS\n")
+ self.man_out.append(".RS\n")
st.p_macro = ".IP\n"
st.list_state.append('o')
- outer_tag = '<' + tag
- for var, val in attrs_list:
- outer_tag += ' ' + var + '=' + safeText(val) + '"'
- self.html_fh.write(outer_tag + '>')
- st.first_dd_tag = False
+ self.html_out.append('<' + tag + ' '.join( ' ' + var + '="' + safeText(val) + '"' for var, val in attrs_list) + '>')
+ st.at_first_tag_in_dd = False
+
def handle_endtag(self, tag):
st = self.state
@@ -220,27 +218,27 @@ class MarkdownToManPage(HTMLParser):
txt = None
add_to_txt = None
if tag == 'h1':
- self.man_fh.write(st.p_macro + '.SH "' + manify(txt) + '"\n')
+ self.man_out.append(st.p_macro + '.SH "' + manify(txt) + '"\n')
elif tag == 'p':
if st.dt_from == 'p':
tag = 'dt'
- self.man_fh.write('.IP "' + manify(txt) + '"\n')
+ self.man_out.append('.IP "' + manify(txt) + '"\n')
st.dt_from = None
else:
- self.man_fh.write(manify(txt) + "\n")
+ self.man_out.append(manify(txt) + "\n")
elif tag == 'li':
if st.list_state[-1] == 'dl':
- if st.first_li_tag:
+ if st.at_first_tag_in_li:
die("Invalid 0. -> td translation")
tag = 'dd'
if txt != '':
- self.man_fh.write(manify(txt) + "\n")
- st.first_li_tag = False
+ self.man_out.append(manify(txt) + "\n")
+ st.at_first_tag_in_li = False
elif tag == 'blockquote':
- self.man_fh.write(".RE\n")
+ self.man_out.append(".RE\n")
elif tag == 'pre':
st.in_pre = False
- self.man_fh.write(manify(txt) + "\n.fi\n")
+ self.man_out.append(manify(txt) + "\n.fi\n")
elif tag == 'code' and not st.in_pre:
add_to_txt = NORM_FONT[0]
elif tag == 'strong' or tag == 'bold':
@@ -251,30 +249,31 @@ class MarkdownToManPage(HTMLParser):
if st.list_state.pop() == 'dl':
tag = 'dl'
if st.list_state:
- self.man_fh.write(".RE\n")
+ self.man_out.append(".RE\n")
else:
st.p_macro = ".P\n"
- st.first_dd_tag = False
- self.html_fh.write('</' + tag + '>')
+ st.at_first_tag_in_dd = False
+ self.html_out.append('</' + tag + '>')
if add_to_txt:
if txt is None:
st.txt += add_to_txt
else:
txt += add_to_txt
if st.dt_from == tag:
- self.man_fh.write('.IP "' + manify(txt) + '"\n')
- self.html_fh.write('</dt><dd>')
- st.first_dd_tag = True
+ self.man_out.append('.IP "' + manify(txt) + '"\n')
+ self.html_out.append('</dt><dd>')
+ st.at_first_tag_in_dd = True
st.dt_from = None
elif tag == 'dt':
- self.html_fh.write('<dd>')
- st.first_dd_tag = True
+ self.html_out.append('<dd>')
+ st.at_first_tag_in_dd = True
+
def handle_data(self, data):
st = self.state
if args.debug:
print(' DATA', [data], st)
- self.html_fh.write(safeText(data))
+ self.html_out.append(safeText(data))
st.txt += data