diff --git a/doc/buildweb.py b/doc/buildweb.py new file mode 100755 index 00000000..1afa03f6 --- /dev/null +++ b/doc/buildweb.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python +# -*- coding: utf-8 - +# +# This file is part of gunicorn released under the MIT license. +# See the NOTICE for more information. + +import codecs +import datetime +import os +import sys + +from docutils.core import publish_parts +from jinja2 import Environment +from jinja2.loaders import FileSystemLoader +from jinja2.utils import open_if_exists + +import conf + +class Site(object): + def __init__(self): + self.url = conf.SITE_URL.rstrip('/') + + fs_loader = FileSystemLoader(conf.TEMPLATES_PATH, encoding="utf-8") + self.env = Environment(loader=fs_loader) + self.env.charset = 'utf-8' + self.env.filters['rel_url'] = self.rel_url + + def rel_url(self, value): + return value.split(self.url)[1] + + def render(self): + for curr_path, dirs, files in os.walk(conf.INPUT_PATH): + tgt_path = curr_path.replace(conf.INPUT_PATH, conf.OUTPUT_PATH) + if not os.path.isdir(tgt_path): + os.makedirs(tgt_path) + self.process(files, curr_path, tgt_path) + + def process(self, files, curr_path, tgt_path): + files = [f for f in files if os.path.splitext(f)[1] in conf.EXTENSIONS] + for f in files: + if os.path.splitext(f)[1] not in conf.EXTENSIONS: + continue + + page = Page(self, f, curr_path, tgt_path) + if not page.needed(): + continue + + print "Page: %s" % page.source + page.write() + + def get_template(self, name): + return self.env.get_template(name) + +class Page(object): + + def __init__(self, site, filename, curr_path, tgt_path): + self.site = site + self.filename = filename + self.source = os.path.join(curr_path, filename) + self.headers = {} + self.body = "" + + with open(self.source, 'Ur') as handle: + headers, body = handle.read().split("\n\n", 1) + + try: + for line in headers.splitlines(): + name, value = line.split(':', 1) + self.headers[name.strip()] = value.strip() + except ValueError: + self.headers = {} + body = "\n\n".join([headers, body]) + self.headers['pubDate'] = ctime = os.stat(self.source).st_ctime + self.headers['published'] = datetime.datetime.fromtimestamp(ctime) + + basename, oldext = os.path.splitext(filename) + oldext = oldext.lower()[1:] + converter = getattr(self, "convert_%s" % oldext, lambda x: x) + self.body = converter(body) + + newfn = "%s.%s" % (basename, self.headers.get('ext', 'html')) + self.target = os.path.join(tgt_path, newfn) + + def url(self): + path = self.target.split(conf.OUTPUT_PATH)[1].lstrip('/') + return "/".join([self.site.url, path]) + + def needed(self): + for f in "force --force -f": + if f in sys.argv[1:]: + return True + + if not os.path.exists(self.target): + return True + + smtime = os.stat(self.source).st_mtime + tmtime = os.stat(self.target).st_mtime + return smtime > tmtime + + def write(self): + contents = self.render() + with codecs.open(self.target, 'w', 'utf-8') as tgt: + tgt.write(contents) + + def render(self): + tmpl_name = self.headers.get('template', conf.DEFAULT_TEMPLATE) + + kwargs = {"conf": conf, "stuff": self.body, "url": self.url()} + kwargs.update(self.headers) + print self.body + + return self.site.get_template(tmpl_name).render(kwargs) + + def convert_rst(self, body): + parts = publish_parts(source=body, writer_name="html") + return parts['html_body'] + +def main(): + Site().render() + +if __name__ == "__main__": + main() diff --git a/doc/conf.py b/doc/conf.py new file mode 100644 index 00000000..30c15bf8 --- /dev/null +++ b/doc/conf.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 - +# +# This file is part of gunicorn released under the MIT license. +# See the NOTICE for more information. + +import os + +# options +SITE_NAME = "Green Unicorn" +SITE_URL = "http://www.gunicorn.org" +SITE_DESCRIPTION = "A Python port of Ruby's Unicorn project." + +EXTENSIONS = ['.rst'] +DEFAULT_TEMPLATE = "default.html" +CONTENT_TYPE = "textile" + +# paths +DOC_PATH = os.path.dirname(os.path.abspath(__file__)) +TEMPLATES_PATH = os.path.join(DOC_PATH, "templates") +INPUT_PATH = os.path.join(DOC_PATH, "site") +OUTPUT_PATH = os.path.join(DOC_PATH, "htdocs") diff --git a/doc/site/index.rst b/doc/site/index.rst new file mode 100644 index 00000000..1570a673 --- /dev/null +++ b/doc/site/index.rst @@ -0,0 +1,12 @@ +template: index.html + + +This is some markup +=================== + +More stuff:: + + Hello there. + Yes... yes... yes! + +And done. \ No newline at end of file diff --git a/doc/templates/base.html b/doc/templates/base.html new file mode 100644 index 00000000..641c40bf --- /dev/null +++ b/doc/templates/base.html @@ -0,0 +1,24 @@ + + + + + Green Unicorn - Welcome + + + + + + + {% block body %}{% endblock %} + + diff --git a/doc/templates/index.html b/doc/templates/index.html new file mode 100644 index 00000000..2d468ade --- /dev/null +++ b/doc/templates/index.html @@ -0,0 +1,3 @@ +{% extends "base.html" %} + +{% block body %}{{ stuff }}{% endblock %}