| 1 |
import py |
| 2 |
import sys, os, traceback |
| 3 |
import re |
| 4 |
|
| 5 |
if hasattr(sys.stdout, 'fileno') and os.isatty(sys.stdout.fileno()): |
| 6 |
def log(msg): |
| 7 |
print msg |
| 8 |
else: |
| 9 |
def log(msg): |
| 10 |
pass |
| 11 |
|
| 12 |
def convert_rest_html(source, source_path, stylesheet=None, encoding='latin1'): |
| 13 |
""" return html latin1-encoded document for the given input. |
| 14 |
source a ReST-string |
| 15 |
sourcepath where to look for includes (basically) |
| 16 |
stylesheet path (to be used if any) |
| 17 |
""" |
| 18 |
from docutils.core import publish_string |
| 19 |
kwargs = { |
| 20 |
'stylesheet' : stylesheet, |
| 21 |
'stylesheet_path': None, |
| 22 |
'traceback' : 1, |
| 23 |
'embed_stylesheet': 0, |
| 24 |
'output_encoding' : encoding, |
| 25 |
|
| 26 |
'halt_level' : 2, |
| 27 |
} |
| 28 |
|
| 29 |
source_path = os.path.abspath(str(source_path)) |
| 30 |
prevdir = os.getcwd() |
| 31 |
try: |
| 32 |
|
| 33 |
return publish_string(source, source_path, writer_name='html', |
| 34 |
settings_overrides=kwargs) |
| 35 |
finally: |
| 36 |
os.chdir(prevdir) |
| 37 |
|
| 38 |
def process(txtpath, encoding='latin1'): |
| 39 |
""" process a textfile """ |
| 40 |
log("processing %s" % txtpath) |
| 41 |
assert txtpath.check(ext='.txt') |
| 42 |
if isinstance(txtpath, py.path.svnwc): |
| 43 |
txtpath = txtpath.localpath |
| 44 |
htmlpath = txtpath.new(ext='.html') |
| 45 |
|
| 46 |
|
| 47 |
style = txtpath.dirpath('style.css') |
| 48 |
if style.check(): |
| 49 |
stylesheet = style.basename |
| 50 |
else: |
| 51 |
stylesheet = None |
| 52 |
content = unicode(txtpath.read(), encoding) |
| 53 |
doc = convert_rest_html(content, txtpath, stylesheet=stylesheet, encoding=encoding) |
| 54 |
htmlpath.write(doc) |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
rex1 = re.compile(ur'.*<body>(.*)</body>.*', re.MULTILINE | re.DOTALL) |
| 61 |
rex2 = re.compile(ur'.*<div class="document">(.*)</div>.*', re.MULTILINE | re.DOTALL) |
| 62 |
|
| 63 |
def strip_html_header(string, encoding='utf8'): |
| 64 |
""" return the content of the body-tag """ |
| 65 |
uni = unicode(string, encoding) |
| 66 |
for rex in rex1,rex2: |
| 67 |
match = rex.search(uni) |
| 68 |
if not match: |
| 69 |
break |
| 70 |
uni = match.group(1) |
| 71 |
return uni |
| 72 |
|
| 73 |
class Project: |
| 74 |
def __init__(self, sourcepath): |
| 75 |
self.sourcepath = sourcepath |
| 76 |
def process(self, path): |
| 77 |
return process(path) |
| 78 |
def get_htmloutputpath(self, path): |
| 79 |
return path.new(ext='html') |