You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
3.7 KiB
Plaintext
123 lines
3.7 KiB
Plaintext
12 years ago
|
#!/usr/bin/env python
|
||
|
|
||
12 years ago
|
import os, argparse, sys, re
|
||
12 years ago
|
import zippydoc
|
||
12 years ago
|
|
||
12 years ago
|
import os
|
||
|
|
||
|
_ROOT = os.path.abspath(os.path.dirname(zippydoc.__file__))
|
||
|
|
||
|
def get_data(path):
|
||
|
return os.path.join(_ROOT, 'data', path)
|
||
|
|
||
12 years ago
|
parser = argparse.ArgumentParser(description='Converts ZippyDoc source files to HTML.')
|
||
|
|
||
|
parser.add_argument('files', metavar='FILE', type=str, nargs='+',
|
||
|
help='files to convert to HTML')
|
||
|
|
||
|
args = parser.parse_args()
|
||
|
options = vars(args)
|
||
|
|
||
12 years ago
|
class HtmlRuleset(zippydoc.TransformationRuleset):
|
||
|
def create_anchor(self, title):
|
||
|
anchor = title.clean().replace("...", "").replace(".", "_")
|
||
|
anchor = re.sub("[^a-zA-Z0-9_]", "", anchor)
|
||
|
return anchor
|
||
|
|
||
|
def escape_html(self, text):
|
||
|
return text.replace("<", "<").replace(">", ">")
|
||
|
|
||
|
def transform_children(self, text):
|
||
|
return '<div class="children">%s</div>' % text
|
||
|
|
||
|
def transform_header(self, depth, text):
|
||
|
if depth <= 7:
|
||
|
title_type = "h%d" % depth
|
||
|
else:
|
||
|
title_type = "h7"
|
||
|
|
||
|
return "<%s>%s</%s>" % (title_type, text.transform(self), title_type)
|
||
|
|
||
|
def transform_definition(self, forms, children):
|
||
|
anchor = self.create_anchor(forms[0])
|
||
|
formlist = "<br>".join([form.transform(self) for form in forms])
|
||
|
return '<div class="definition"><a name="%s">%s %s</a></div>' % (anchor, formlist, children)
|
||
|
|
||
|
def transform_argument(self, name, description, children):
|
||
|
return "<dl><dt>%s</dt><dd>%s%s</dd></dl>" % (name, description.transform(self), children)
|
||
|
|
||
|
def transform_example(self, title, children):
|
||
|
return '<div class="example">Example: %s %s</div>' % (title.transform(self), children)
|
||
|
|
||
|
def transform_code(self, text):
|
||
|
return '<h7>Code:</h7><pre class="code">%s</pre>' % self.escape_html(text)
|
||
|
|
||
|
def transform_output(self, text):
|
||
|
return '<h7>Output:</h7><pre class="output">%s</pre>' % self.escape_html(text)
|
||
|
|
||
|
def transform_exclamation(self, text, children):
|
||
|
return '<div class="exclamation"><strong>Important:</strong> %s %s</div>' % (text.transform(self), children)
|
||
|
|
||
|
def transform_text(self, text):
|
||
|
return '<div class="text">%s</div>' % text.transform(self)
|
||
|
|
||
|
def transform_reference(self, target, description):
|
||
|
return '<a href="%s.html">%s</a>' % (target, description.transform(self))
|
||
|
|
||
|
def transform_external_reference(self, target, description):
|
||
|
return '<a href="%s">%s</a>' % (target, description.transform(self))
|
||
|
|
||
|
def transform_fixed_width(self, text):
|
||
|
return '<span class="fixed">%s</span>' % text
|
||
|
|
||
|
def transform_emphasis(self, text):
|
||
|
return "<em>%s</em>" % text.transform(self)
|
||
|
|
||
|
def transform_strong(self, text):
|
||
|
return "<strong>%s</strong>" % text.transform(self)
|
||
|
|
||
|
def transform_toc(self, items):
|
||
|
rendered = ""
|
||
|
|
||
|
for item in items:
|
||
|
forms = item[0].get_forms()
|
||
|
anchor = self.create_anchor(forms[0])
|
||
|
|
||
|
if len(forms) > 1:
|
||
|
alternatives = '<span class="alternatives">(also: %s)</span>' % ", ".join(form.clean() for form in forms[1:])
|
||
|
else:
|
||
|
alternatives = ""
|
||
|
|
||
|
description = item[1]
|
||
|
|
||
|
if len(description) > 80:
|
||
12 years ago
|
matches = re.match("^(.{0,80})\W", description)
|
||
12 years ago
|
description = matches.group(1) + "..."
|
||
|
|
||
|
description = zippydoc.Value(description).transform(self)
|
||
|
|
||
|
rendered += '<li><a href="#%s">%s</a> %s %s</li>' % (anchor, forms[0].clean(), description, alternatives)
|
||
|
|
||
|
return '<div class="toc"><h2>Table of contents</h2><ul>%s</ul></div>' % rendered
|
||
|
|
||
12 years ago
|
files = options["files"]
|
||
|
|
||
12 years ago
|
template = open(get_data("template.html")).read()
|
||
12 years ago
|
|
||
|
for zpy in files:
|
||
|
destination = os.path.splitext(zpy)[0] + ".html"
|
||
|
|
||
|
f = open(zpy, "r")
|
||
|
data = f.read()
|
||
|
f.close()
|
||
|
|
||
12 years ago
|
doc = zippydoc.Document(open(zpy, "r").read())
|
||
|
|
||
|
rendered = doc.transform(HtmlRuleset())
|
||
12 years ago
|
|
||
|
f = open(destination, "w")
|
||
12 years ago
|
f.write(template.replace("{CONTENT}", rendered))
|
||
12 years ago
|
f.close()
|
||
|
|
||
|
print "Rendered %s" % destination
|