This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd2docbook.py
executable file
·476 lines (383 loc) · 13.5 KB
/
md2docbook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/usr/bin/env python3
#
# Copyright (c) 2018 Edward Tomasz Napierala <[email protected]>
# Copyright (c) 2019-2020 Lorenzo Salvadore <[email protected]>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# XXX: This should get rewritten by someone who actually knows Python.
#
import re
import sys
from email.utils import parseaddr
from xml.sax.saxutils import escape
class report_class:
# This part is partly copy/pasted from en_US.ISO8859-1/htdocs/news/status/report-template.xml
text = '''\
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE report PUBLIC "-//FreeBSD//DTD FreeBSD XML Database for
Status Report//EN"
"http://www.FreeBSD.org/XML/share/xml/statusreport.dtd" >
<!-- $FreeBSD: head/en_US.ISO8859-1/htdocs/news/status/report-template.xml 51132 2017-10-19 01:38:11Z bjk $ -->
<!-- This file was generated with https://github.com/trasz/md2docbook -->
<!--
Variables to replace:
%%START%% - report month start
%%STOP%% - report month end
%%YEAR%% - report year
-->
<report>
<date>
<month>%%START%%-%%STOP%%</month>
<year>%%YEAR%%</year>
</date>
<section>
<title>Introduction</title>
'''
# This part is copy/pasted from en_US.ISO8859-1/htdocs/news/status/report-template.xml
after_introduction = '''\
</section>
<category>
<name>team</name>
<description>&os; Team Reports</description>
<p>Entries from the various official and semi-official teams,
as found in the <a href="&enbase;/administration.html">Administration
Page</a>.</p>
</category>
<category>
<name>proj</name>
<description>Projects</description>
<p>Projects that span multiple categories, from the kernel and userspace
to the Ports Collection or external projects.</p>
</category>
<category>
<name>kern</name>
<description>Kernel</description>
<p>Updates to kernel subsystems/features, driver support,
filesystems, and more.</p>
</category>
<category>
<name>arch</name>
<description>Architectures</description>
<p>Updating platform-specific features and bringing in support
for new hardware platforms.</p>.
</category>
<category>
<name>bin</name>
<description>Userland Programs</description>
<p>Changes affecting the base system and programs in it.</p>
</category>
<category>
<name>ports</name>
<description>Ports</description>
<p>Changes affecting the Ports Collection, whether sweeping
changes that touch most of the tree, or individual ports
themselves.</p>
</category>
<category>
<name>doc</name>
<description>Documentation</description>
<p>Noteworthy changes in the documentation tree or new external
books/documents.</p>
</category>
<category>
<name>misc</name>
<description>Miscellaneous</description>
<p>Objects that defy categorization.</p>
</category>
<category>
<name>third</name>
<description>Third-Party Projects</description>
<p>Many projects build upon &os; or incorporate components of
&os; into their project. As these projects may be of interest
to the broader &os; community, we sometimes include brief
updates submitted by these projects in our quarterly report.
The &os; project makes no representation as to the accuracy or
veracity of any claims in these submissions.</p>
</category>
'''
# salvadore:
# the keys of the following dictionary are all tags
# EXCEPT introduction, wich is a key that takes track of when
# we need to print the content of the after_introduction variable.
inside = dict.fromkeys(['introduction',\
'project',\
'body',\
'p',\
'ul',\
'li',\
'code'])
def __init__(self):
self.inside['introduction'] = True
def append(self, line):
self.text += line
def append_contacts(self, contacts):
if not contacts:
return self.text
# You know, it's not that I don't know about templating
# engines. I do. I just want to give you some additional
# motivation :->
self.text += '<contact>'
for person in contacts:
self.text += '''\
<person>
<name>%s</name>
<email>%s</email>
</person>
''' % (person[0], person[1])
self.text += '''\
</contact>
'''
def append_links(self, links):
if not links:
return self.text
self.text += '''\
<links>
'''
for link in links:
self.text += '''\
<url href="%s">%s</url>
''' % (link[1], link[0])
self.text += '''\
</links>
'''
def append_sponsors(self, sponsors):
if not sponsors:
return self.text
for sponsor in sponsors:
self.text += '''\
<sponsor>
%s
</sponsor>
''' % sponsor
def close(self, tag):
self.text += '</' + tag + '>\n'
self.inside[tag] = False
if self.is_inside('introduction'):
if tag == 'ul': self.close('p')
def get(self):
return self.text + '</report>'
def is_inside(self, tag):
return self.inside[tag]
def newline(self):
self.append('\n\t')
# salvadore:
# category and title should be set only for tag == 'project'
# I do not check for it because if an error arises it is our fault,
# not the input fault
def open(self, tag, category='', title=''):
if tag == 'project':
extra = ' cat="' + category + \
'"><title>' + title + '</title'
if self.is_inside('project'):
self.close('project')
elif self.is_inside('introduction'):
self.text += self.after_introduction
self.inside['introduction'] = False
else:
extra = ''
if self.is_inside('introduction'):
if tag == 'body': return
if tag == 'ul': self.open('p')
self.text += '<' + tag + extra + '>'
self.inside[tag] = True
def reflow(line):
if not line:
return ''
text = ''
textlen = 0
for word in line.split():
if textlen + len(word) >= 66 - 8:
text = text + '\n' + '\t'
textlen = 8
elif textlen > 0:
text = text + ' '
textlen = textlen + 1
text = text + word
textlen = textlen + len(word)
return text
def md2docbook(infile):
cat = 'unknown' # For parsing individual submissions.
contacts = []
links = []
sponsors = []
report = report_class()
for line in infile:
line = line.rstrip()
avoid_newline = False
if line == '# FreeBSD Team Reports #':
cat = 'team'
continue
if line == '# Projects #':
cat = 'proj'
continue
if line == '# Kernel Projects #':
cat = 'kern'
continue
if line == '# Ports #':
cat = 'ports'
continue
if line == '# Architectures #':
cat = 'arch'
continue
if line == '# Documentation #':
cat = 'doc'
continue
if line == '# Third-Party Projects #':
cat = 'third'
continue
if line == '# Miscellaneous #':
cat = 'misc'
continue
if line == '# Userland Programs #':
cat = 'bin'
continue
if line.startswith('# ') and not report.is_inside('code'):
sys.exit('invalid category name "%s"; please consult %s source code"' \
% (line, sys.argv[0]))
if line.startswith('## '):
title = line.strip('# ')
# Start a new <project> entry. But first finish
# the old one.
# XXX: As I've mentioned, this _really_ should get rewritten.
# salvadore: I've rewritten this a bit, but I guess we can
# still do better
for tag in ['p', 'ul', 'body']:
if report.is_inside(tag):
report.close(tag)
if report.is_inside('project'):
report.append_sponsors(sponsors)
report.close('project')
contacts = []
links = []
sponsors = []
report.open('project', cat, title)
continue
# Translate '###' into '<p>', to match earlier reports.
if line.startswith('### '):
line = line.strip('# ')
if line.startswith('Contact:'):
# The comma in 'Name, <email>' confuses parseaddr().
line = line.replace(',', '')
contacts.append(parseaddr(line))
continue
if line.startswith('Link:'):
href = re.search(r'\((.+)\)', line)
if href:
href = href.group(1)
else:
href = ''
name = re.search(r'\[(.+)\]', line)
if name:
name = name.group(1)
else:
name = href
links.append((name, href))
continue
if line.startswith('Sponsor:'):
sponsor = line[len('Sponsor:'):].strip()
sponsors.append(sponsor)
continue
if line.strip() == '':
if not report.is_inside('body'):
continue
if report.is_inside('p'):
report.close('p')
continue
if not report.is_inside('body'):
report.append_contacts(contacts)
report.append_links(links)
report.open('body')
# Unordered lists.
if line.strip().startswith(('-', '*')):
line = line.lstrip('*- ')
if report.is_inside('p') and not report.is_inside('ul'):
report.close('p')
if report.is_inside('ul'):
report.close('li')
else:
report.open('ul')
inside_ul = True
report.open('li')
avoid_newline = True
elif not line.startswith(' ') and report.is_inside('ul'):
report.close('ul')
if line.startswith('```'):
line = line.lstrip('```')
if report.is_inside('code'):
if report.is_inside('p'):
report.close('p')
report.close('code')
else:
report.open('code')
# Here we paste the plain text. Note that the text
# in 'report' is generally _not_ followed by a newline,
# so that we don't need to remove them when we append
# '</p>'.
if report.is_inside('p'):
report.newline()
elif report.is_inside('ul'):
if not avoid_newline:
report.newline()
else:
report.open('p')
line = escape(line)
# Handle inline links with the usual Markdown syntax.
hrefs = re.findall(r'\(([^)]+)\)', line)
for match in hrefs:
if match.startswith('http'):
href = match
name = re.search(r'\[(.+)\]', line)
if name:
name = name.group(1)
line = re.sub(r'\[.*\]', '', line)
else:
name = href
line = line.replace('(' + href + ')',\
'<a href="%s">%s</a>' % (href, name))
# Handle *this*.
line = re.sub(r'(^| )\*([^ ][^*]+)\*', r'\1<tt>\2</tt>', line)
# Handle `this`.
line = re.sub(r'(^| )\`([^ ][^`]+)\`', r'\1<tt>\2</tt>', line)
report.append(reflow(line))
for tag in ['p', 'ul']:
if report.is_inside(tag):
report.close(tag)
report.append_sponsors(sponsors)
report.close('body')
report.close('project')
return report.get()
def main():
if len(sys.argv) > 3:
sys.exit('usage: %s [input-file [output-file]]' % sys.argv[0])
if len(sys.argv) > 2:
outfile = open(sys.argv[2], 'w')
else:
outfile = sys.stdout
if len(sys.argv) > 1:
infile = open(sys.argv[1], 'r')
else:
infile = sys.stdin
docbook = md2docbook(infile)
outfile.write(docbook)
if __name__ == '__main__':
main()