Source

timestamp / wiki / __init__.py

Full commit
'''integration with Bitbucket WikiCreole

wiki is an extension for Mercurial that allows it to act as a offline
BitBucket wiki renderer.

'''
from mercurial.i18n import _
from mercurial.util import opener, makedirs, rundetached

import os, sys, shutil
from creole import Parser
from creole2html import HtmlEmitter

def wiki(ui, repo, *args, **opts):
    '''see detailed help for list of subcommands'''
    modified, added, removed, deleted, unknown, ignored, clean = [
            n for n in repo.status(clean=True)]

    files = modified + added + clean

    o = opener(os.path.abspath(args[0]))

    for fn in files:        
        if fn.endswith('.wiki'):
            ui.debug('Converting %s' % fn)
            f = open(repo.wjoin(fn), 'r')
            document = Parser(unicode(f.read(), 'utf-8', 'ignore')).parse()
            f.close()
            fnt = fn + '.html'
            ui.debug(' to %s\n' % fnt)
            f = o(fnt, 'w')
            part = open(repo.wjoin('.wiki.header'), 'r')
            f.write(part.read())
            part.close()
            f.write(HtmlEmitter(document).emit().encode('utf-8', 'ignore'))
            part = open(repo.wjoin('.wiki.footer'), 'r')
            f.write(part.read())
            part.close()
            f.close()
        else:
            ui.debug('Copying %s' % fn)
            fnt = os.path.abspath(os.path.join(args[0], fn))
            ui.debug(' to %s\n' % fnt)
            try:
                shutil.copy(fn, fnt)
            except:
                makedirs(os.path.dirname(fnt))
                shutil.copy(fn, fnt)

    def condf():
        return True
    fnt = os.path.abspath(os.path.join(args[0], 'Home.wiki.html'))
    ui.debug('Starting browser with %s\n' % fnt)
    rundetached(['explorer',fnt], condf)

cmdtable = {
    "wiki":
        (wiki, [], _('generates html from .wiki file')),
}