bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
2054.2.1
by Aaron Bentley
Start work on pretty rest conversion |
1 |
#!/usr/bin/env python2.4
|
2 |
import errno |
|
3 |
import os |
|
4 |
from StringIO import StringIO |
|
5 |
import sys |
|
6 |
||
7 |
from docutils.core import publish_file |
|
8 |
from docutils.parsers import rst |
|
9 |
from elementtree.ElementTree import XML |
|
10 |
import kid |
|
11 |
||
12 |
def kidified_rest(rest_file, template_name): |
|
13 |
xhtml = publish_file(rest_file, writer_name='html', destination=StringIO()) |
|
14 |
xml = XML(xhtml) |
|
15 |
head = xml.find('{http://www.w3.org/1999/xhtml}head') |
|
16 |
body = xml.find('{http://www.w3.org/1999/xhtml}body') |
|
17 |
assert head is not None |
|
18 |
assert body is not None |
|
19 |
template=kid.Template(file=template_name, |
|
20 |
head=head, body=body) |
|
21 |
return (template.serialize()) |
|
22 |
||
23 |
def safe_open(filename, mode): |
|
24 |
try: |
|
25 |
return open(filename, mode + 'b') |
|
26 |
except IOError, e: |
|
27 |
if e.errno != errno.ENOENT: |
|
28 |
raise
|
|
29 |
sys.stderr.write('file not found: %s\n' % sys.argv[2]) |
|
30 |
sys.exit(3) |
|
31 |
args = sys.argv[1:] |
|
32 |
||
33 |
assert len(args) > 0 |
|
34 |
||
35 |
if len(args) > 1: |
|
36 |
rest_file = safe_open(args[1], 'r') |
|
37 |
else: |
|
38 |
rest_file = sys.stdin |
|
39 |
||
40 |
if len(args) > 2: |
|
41 |
out_file = safe_open(args[2], 'w') |
|
42 |
else: |
|
43 |
out_file = sys.stdout |
|
44 |
||
45 |
out_file.write(kidified_rest(rest_file, args[0])) |