@@ -57,26 +57,28 @@ NORM_FONT = ('\1', r"\fP")
57
57
BOLD_FONT = ('\2 ' , r"\fB" )
58
58
ULIN_FONT = ('\3 ' , r"\fI" )
59
59
60
- env_subs = { }
61
-
62
60
def main ():
63
- mtime = None
64
-
65
61
fi = re .match (r'^(?P<fn>(?P<srcdir>.+/)?(?P<name>(?P<prog>[^/]+)\.(?P<sect>\d+))\.md)$' , args .mdfile )
66
62
if not fi :
67
63
die ('Failed to parse NAME.NUM.md out of input file:' , args .mdfile )
68
64
fi = argparse .Namespace (** fi .groupdict ())
65
+
69
66
if not fi .srcdir :
70
67
fi .srcdir = './'
71
68
69
+ fi .title = fi .prog + '(' + fi .sect + ') man page'
70
+ fi .date = None
71
+
72
72
chk_files = 'latest-year.h Makefile' .split ()
73
73
for fn in chk_files :
74
74
try :
75
75
st = os .lstat (fi .srcdir + fn )
76
76
except :
77
77
die ('Failed to find' , fi .srcdir + fn )
78
- if not mtime :
79
- mtime = st .st_mtime
78
+ if not fi .date :
79
+ fi .date = time .strftime ('%d %b %Y' , time .localtime (st .st_mtime ))
80
+
81
+ env_subs = { }
80
82
81
83
with open (fi .srcdir + 'Makefile' , 'r' , encoding = 'utf-8' ) as fh :
82
84
for line in fh :
@@ -90,91 +92,89 @@ def main():
90
92
if var == 'VERSION' :
91
93
break
92
94
93
- MarkdownToManPage (fi , mtime )
95
+ with open (fi .fn , 'r' , encoding = 'utf-8' ) as fh :
96
+ txt = re .sub (r'@VERSION@' , env_subs ['VERSION' ], fh .read ())
97
+ txt = re .sub (r'@LIBDIR@' , env_subs ['libdir' ], txt )
98
+ fi .html_in = cmarkgfm .github_flavored_markdown_to_html (txt )
99
+ txt = None
100
+
101
+ HtmlToManPage (fi )
102
+
103
+ if args .test :
104
+ print ("The test was successful." )
105
+ return
106
+
107
+ fn = fi .name + '.html'
108
+ print ("Outputing HTML page:" , fn )
109
+ with open (fn , 'w' , encoding = 'utf-8' ) as fh :
110
+ fh .write (fi .html_out )
94
111
112
+ fn = fi .name
113
+ print ("Outputing man page:" , fn )
114
+ with open (fn , 'w' , encoding = 'utf-8' ) as fh :
115
+ fh .write (fi .man_out )
95
116
96
- class MarkdownToManPage (HTMLParser ):
97
- def __init__ (self , fi , mtime ):
117
+
118
+ class HtmlToManPage (HTMLParser ):
119
+ def __init__ (self , fi ):
98
120
HTMLParser .__init__ (self , convert_charrefs = True )
99
121
100
- self .man_fh = self .html_fh = None
101
122
self .state = argparse .Namespace (
102
123
list_state = [ ],
103
124
p_macro = ".P\n " ,
104
- first_li_tag = False ,
105
- first_dd_tag = False ,
125
+ at_first_tag_in_li = False ,
126
+ at_first_tag_in_dd = False ,
106
127
dt_from = None ,
107
128
in_pre = False ,
108
129
txt = '' ,
109
130
)
110
131
111
- self .date = time .strftime ('%d %b %Y' , time .localtime (mtime ))
112
-
113
- with open (fi .fn , 'r' , encoding = 'utf-8' ) as fh :
114
- txt = re .sub (r'@VERSION@' , env_subs ['VERSION' ], fh .read ())
115
- txt = re .sub (r'@LIBDIR@' , env_subs ['libdir' ], txt )
116
- html = cmarkgfm .github_flavored_markdown_to_html (txt )
117
- txt = None
132
+ self .html_out = [ HTML_START % fi .title ]
133
+ self .man_out = [ MAN_START % (fi .prog , fi .sect , fi .date ) ]
118
134
119
- if args .test :
120
- self .html_fh = open (os .devnull , 'w' , encoding = 'utf-8' )
121
- self .man_fh = self .html_fh
122
- else :
123
- self .html_fn = fi .name + '.html'
124
- self .html_fh = open (self .html_fn , 'w' , encoding = 'utf-8' )
125
- self .html_fh .write (HTML_START % (fi .prog + '(' + fi .sect + ') man page' ))
135
+ self .feed (fi .html_in )
136
+ fi .html_in = None
126
137
127
- self .man_fn = fi .name
128
- self .man_fh = open (self .man_fn , 'w' , encoding = 'utf-8' )
129
- self .man_fh .write (MAN_START % (fi .prog , fi .sect , self .date ))
138
+ self .html_out .append (HTML_END % fi .date )
139
+ self .man_out .append (MAN_END )
130
140
131
- self .feed (html )
141
+ fi .html_out = '' .join (self .html_out )
142
+ self .html_out = None
132
143
133
- def __del__ (self ):
134
- if args .test :
135
- print ("The test was successful." )
136
- return
144
+ fi .man_out = '' .join (self .man_out )
145
+ self .man_out = None
137
146
138
- if self .html_fh :
139
- self .html_fh .write (HTML_END % self .date )
140
- self .html_fh .close ()
141
- print ("Output HTML page: " , self .html_fn )
142
-
143
- if self .man_fh :
144
- self .man_fh .write (MAN_END )
145
- self .man_fh .close ()
146
- print ("Output man page: " , self .man_fn )
147
147
148
148
def handle_starttag (self , tag , attrs_list ):
149
149
st = self .state
150
150
if args .debug :
151
151
print ('START' , tag , attrs_list , st )
152
- if st .first_li_tag :
152
+ if st .at_first_tag_in_li :
153
153
if st .list_state [- 1 ] == 'dl' :
154
154
st .dt_from = tag
155
155
if tag == 'p' :
156
156
tag = 'dt'
157
157
else :
158
- self .html_fh . write ('<dt>' )
159
- st .first_li_tag = False
158
+ self .html_out . append ('<dt>' )
159
+ st .at_first_tag_in_li = False
160
160
if tag == 'p' :
161
- if not st .first_dd_tag :
162
- self .man_fh . write (st .p_macro )
161
+ if not st .at_first_tag_in_dd :
162
+ self .man_out . append (st .p_macro )
163
163
elif tag == 'li' :
164
- st .first_li_tag = True
164
+ st .at_first_tag_in_li = True
165
165
lstate = st .list_state [- 1 ]
166
166
if lstate == 'dl' :
167
167
return
168
168
if lstate == 'o' :
169
- self .man_fh . write (".IP o\n " )
169
+ self .man_out . append (".IP o\n " )
170
170
else :
171
- self .man_fh . write (".IP " + str (lstate ) + ".\n " )
171
+ self .man_out . append (".IP " + str (lstate ) + ".\n " )
172
172
st .list_state [- 1 ] += 1
173
173
elif tag == 'blockquote' :
174
- self .man_fh . write (".RS 4\n " )
174
+ self .man_out . append (".RS 4\n " )
175
175
elif tag == 'pre' :
176
176
st .in_pre = True
177
- self .man_fh . write (st .p_macro + ".nf\n " )
177
+ self .man_out . append (st .p_macro + ".nf\n " )
178
178
elif tag == 'code' and not st .in_pre :
179
179
st .txt += BOLD_FONT [0 ]
180
180
elif tag == 'strong' or tag == 'bold' :
@@ -188,26 +188,24 @@ class MarkdownToManPage(HTMLParser):
188
188
start = int (val ) # We only support integers.
189
189
break
190
190
if st .list_state :
191
- self .man_fh . write (".RS\n " )
191
+ self .man_out . append (".RS\n " )
192
192
if start == 0 :
193
193
tag = 'dl'
194
194
attrs_list = [ ]
195
195
st .list_state .append ('dl' )
196
196
else :
197
197
st .list_state .append (start )
198
- self .man_fh . write (st .p_macro )
198
+ self .man_out . append (st .p_macro )
199
199
st .p_macro = ".IP\n "
200
200
elif tag == 'ul' :
201
- self .man_fh . write (st .p_macro )
201
+ self .man_out . append (st .p_macro )
202
202
if st .list_state :
203
- self .man_fh . write (".RS\n " )
203
+ self .man_out . append (".RS\n " )
204
204
st .p_macro = ".IP\n "
205
205
st .list_state .append ('o' )
206
- outer_tag = '<' + tag
207
- for var , val in attrs_list :
208
- outer_tag += ' ' + var + '=' + safeText (val ) + '"'
209
- self .html_fh .write (outer_tag + '>' )
210
- st .first_dd_tag = False
206
+ self .html_out .append ('<' + tag + ' ' .join ( ' ' + var + '="' + safeText (val ) + '"' for var , val in attrs_list ) + '>' )
207
+ st .at_first_tag_in_dd = False
208
+
211
209
212
210
def handle_endtag (self , tag ):
213
211
st = self .state
@@ -220,27 +218,27 @@ class MarkdownToManPage(HTMLParser):
220
218
txt = None
221
219
add_to_txt = None
222
220
if tag == 'h1' :
223
- self .man_fh . write (st .p_macro + '.SH "' + manify (txt ) + '"\n ' )
221
+ self .man_out . append (st .p_macro + '.SH "' + manify (txt ) + '"\n ' )
224
222
elif tag == 'p' :
225
223
if st .dt_from == 'p' :
226
224
tag = 'dt'
227
- self .man_fh . write ('.IP "' + manify (txt ) + '"\n ' )
225
+ self .man_out . append ('.IP "' + manify (txt ) + '"\n ' )
228
226
st .dt_from = None
229
227
else :
230
- self .man_fh . write (manify (txt ) + "\n " )
228
+ self .man_out . append (manify (txt ) + "\n " )
231
229
elif tag == 'li' :
232
230
if st .list_state [- 1 ] == 'dl' :
233
- if st .first_li_tag :
231
+ if st .at_first_tag_in_li :
234
232
die ("Invalid 0. -> td translation" )
235
233
tag = 'dd'
236
234
if txt != '' :
237
- self .man_fh . write (manify (txt ) + "\n " )
238
- st .first_li_tag = False
235
+ self .man_out . append (manify (txt ) + "\n " )
236
+ st .at_first_tag_in_li = False
239
237
elif tag == 'blockquote' :
240
- self .man_fh . write (".RE\n " )
238
+ self .man_out . append (".RE\n " )
241
239
elif tag == 'pre' :
242
240
st .in_pre = False
243
- self .man_fh . write (manify (txt ) + "\n .fi\n " )
241
+ self .man_out . append (manify (txt ) + "\n .fi\n " )
244
242
elif tag == 'code' and not st .in_pre :
245
243
add_to_txt = NORM_FONT [0 ]
246
244
elif tag == 'strong' or tag == 'bold' :
@@ -251,30 +249,31 @@ class MarkdownToManPage(HTMLParser):
251
249
if st .list_state .pop () == 'dl' :
252
250
tag = 'dl'
253
251
if st .list_state :
254
- self .man_fh . write (".RE\n " )
252
+ self .man_out . append (".RE\n " )
255
253
else :
256
254
st .p_macro = ".P\n "
257
- st .first_dd_tag = False
258
- self .html_fh . write ('</' + tag + '>' )
255
+ st .at_first_tag_in_dd = False
256
+ self .html_out . append ('</' + tag + '>' )
259
257
if add_to_txt :
260
258
if txt is None :
261
259
st .txt += add_to_txt
262
260
else :
263
261
txt += add_to_txt
264
262
if st .dt_from == tag :
265
- self .man_fh . write ('.IP "' + manify (txt ) + '"\n ' )
266
- self .html_fh . write ('</dt><dd>' )
267
- st .first_dd_tag = True
263
+ self .man_out . append ('.IP "' + manify (txt ) + '"\n ' )
264
+ self .html_out . append ('</dt><dd>' )
265
+ st .at_first_tag_in_dd = True
268
266
st .dt_from = None
269
267
elif tag == 'dt' :
270
- self .html_fh .write ('<dd>' )
271
- st .first_dd_tag = True
268
+ self .html_out .append ('<dd>' )
269
+ st .at_first_tag_in_dd = True
270
+
272
271
273
272
def handle_data (self , data ):
274
273
st = self .state
275
274
if args .debug :
276
275
print (' DATA' , [data ], st )
277
- self .html_fh . write (safeText (data ))
276
+ self .html_out . append (safeText (data ))
278
277
st .txt += data
279
278
280
279
0 commit comments