forked from keitheis/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikitext2xhtml.py
executable file
·67 lines (51 loc) · 1.93 KB
/
wikitext2xhtml.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import re
from mwlib.dummydb import DummyDB
from mwlib.uparser import parseString
from mwlib.xhtmlwriter import MWXHTMLWriter, preprocess
MWXHTMLWriter.ignoreUnknownNodes = False
class SuppressOutput(object):
'''A context manager for doing a "deep suppression" of stdout and stderr in
Python
i.e. will suppress all print, even if the print originates in a compiled
C/Fortran sub-function.
This will not suppress raised exceptions, since exceptions are printed to
stderr just before a script exits, and after the context manager has exited
(at least, I think that is why it lets exceptions through).
'''
def __init__(self):
# Open a pair of null files.
self.null_fds = [os.open(os.devnull, os.O_RDWR) for x in range(2)]
# Save the actual stdout (1) and stderr (2) file descriptors.
self.saved_fds = (os.dup(1), os.dup(2))
def __enter__(self):
# Assign the null pointers to stdout and stderr.
os.dup2(self.null_fds[0], 1)
os.dup2(self.null_fds[1], 2)
def __exit__(self, *_):
# Close the null files
os.close(self.null_fds[0])
os.close(self.null_fds[1])
# Re-assign the real stdout/stderr back to (1) and (2)
os.dup2(self.saved_fds[0], 1)
os.dup2(self.saved_fds[1], 2)
def getXHTML(wikitext):
db = DummyDB()
r = parseString(title="", raw=wikitext, wikidb=db)
preprocess(r)
dbw = MWXHTMLWriter()
with SuppressOutput():
dbw.writeBook(r)
return dbw.asstring()
def transFileName(filename, suffix):
return re.sub(r'\.[^\.]*$', suffix, filename)
if __name__ == "__main__":
filename = sys.argv[1]
raw = open(filename).read().decode('utf8')
output = open(transFileName(filename, '.xhtml'), 'w')
xhtml = getXHTML(raw)
output.write(xhtml)
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 textwidth=79