#!/usr/bin/env python import os, argparse, sys, re import zippydoc import os _ROOT = os.path.abspath(os.path.dirname(zippydoc.__file__)) def get_data(path): return os.path.join(_ROOT, 'data', path) 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) 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 '
%s
' % text def transform_header(self, depth, text): if depth <= 7: title_type = "h%d" % depth else: title_type = "h7" return "<%s>%s" % (title_type, text.transform(self), title_type) def transform_definition(self, forms, children): anchor = self.create_anchor(forms[0]) formlist = "
".join([form.transform(self) for form in forms]) return '
%s %s
' % (anchor, formlist, children) def transform_argument(self, name, description, children): return "
%s
%s%s
" % (name, description.transform(self), children) def transform_example(self, title, children): return '
Example: %s %s
' % (title.transform(self), children) def transform_section(self, title, children): return '
%s
%s
' % (title.transform(self), children) def transform_code(self, text): return 'Code:
%s
' % self.escape_html(text) def transform_output(self, text): return 'Output:
%s
' % self.escape_html(text) def transform_exclamation(self, text, children): return '
Important: %s %s
' % (text.transform(self), children) def transform_text(self, text): return '
%s
' % text.transform(self) def transform_list(self, items): return '' % "".join("
  • %s
  • " % item.transform(self) for item in items) def transform_reference(self, target, description): return '%s' % (target, description.transform(self)) def transform_external_reference(self, target, description): return '%s' % (target, description.transform(self)) def transform_fixed_width(self, text): return '%s' % text def transform_emphasis(self, text): return "%s" % text.transform(self) def transform_strong(self, text): return "%s" % 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 = '(also: %s)' % ", ".join(form.clean() for form in forms[1:]) else: alternatives = "" description = item[1] if len(description) > 80: matches = re.match("^(.{0,80})\W", description) description = matches.group(1) + "..." description = zippydoc.Value(description).transform(self) rendered += '
  • %s %s %s
  • ' % (anchor, forms[0].clean(), description, alternatives) return '

    Table of contents

    ' % rendered files = options["files"] template = open(get_data("template.html")).read() for zpy in files: destination = os.path.splitext(zpy)[0] + ".html" f = open(zpy, "r") data = f.read() f.close() doc = zippydoc.Document(data) rendered = doc.transform(HtmlRuleset()) f = open(destination, "w") f.write(template.replace("{CONTENT}", rendered)) f.close() print "Rendered %s" % destination