Source

ittywiki / formatter.py

Full commit
import docutils.core

from settings import TITLE, SIDEBAR_ITEMS, ROOT_PATH, DATA_PATH

from docutils import nodes
from docutils.readers import standalone
from docutils.transforms import Transform

from os import path

#following three classes are derived from: http://docutils.sourceforge.net/sandbox/ianb/wiki/Wiki.py
class WikiLinkResolver(nodes.SparseNodeVisitor):

    def visit_reference(self, node):
        if node.resolved or not node.hasattr('refname'):
            return
        refname = node['refname']
        node.resolved = 1
        node['class'] = 'wiki'
        node['refuri'] = node['name'] #refname
        del node['refname']


class WikiLink(Transform):

    default_priority = 800

    def apply(self):
        visitor = WikiLinkResolver(self.document)
        self.document.walk(visitor)


class Reader(standalone.Reader):

    supported = standalone.Reader.supported + ('wiki',)

    def get_transforms(self):
        return standalone.Reader.get_transforms(self) + [WikiLink]

#from /usr/share/pyshared/docutils/writers/html4css1/template.txt
#%(stylesheet)s
format_doc = """
%(head_prefix)s
%(head)s
<link rel="stylesheet" type="text/css" href="/static/nwiki.css" /> 
%(body_prefix)s
%(custom_sidebar)s
%(body_pre_docinfo)s
%(docinfo)s
%(body)s
<br/><br/>
<div align="center">
<a href="AllPages">All Pages</a> |
<a href="%(pagename)s.slide">View as Slideshow</a> |
<a href="%(pagename)s.rst">View the Source</a>
</div>
%(body_suffix)s
"""

def get_sidebar_markup():
    formatted_items = ["""<li>%s</li>""" % item for item in SIDEBAR_ITEMS]

    return """
    <div class="sidebar">
    <p class="sidebar-title">%s</p>
    <ul>
    %s
    </ul>
    </div>""" %  (TITLE, "".join(formatted_items))


def get_formatted_doc_from_string(text, pagename=""):
    html_dict = docutils.core.publish_parts("", reader=Reader(), writer_name='html')

    html_dict['custom_sidebar'] = get_sidebar_markup()
    html_dict['pagename'] = pagename
    html_dict['body'] = text
    
    return format_doc % html_dict


def get_formatted_doc(pagename, extension='rst'):
    #todo check for existence and raise exception/blank page
    filename = path.join(DATA_PATH, pagename + '.' + extension)

    #just reading the file into a string to run it through docutils
    text = open(filename).read()
    html_dict = docutils.core.publish_parts(text, reader=Reader(), writer_name='html')
    
    html_dict['custom_sidebar'] = get_sidebar_markup()
    html_dict['pagename'] = pagename
    
    return format_doc % html_dict


def get_formatted_s5_doc(pagename):
    #todo check for existence and raise exception/blank page
    filename = path.join(DATA_PATH, pagename + '.rst')

    #just reading th efile into a string to 
    text = open(filename).read()
    html_dict = docutils.core.publish_parts(text, reader=Reader(), writer_name='s5_html')

    return """%(head_prefix)s
%(head)s
%(stylesheet)s
%(body_prefix)s
%(body_pre_docinfo)s
%(docinfo)s
%(body)s
%(body_suffix)s
""" % html_dict