#!/usr/bin/env python

import metakit, re, string, os

VALID_TITLE_CHARS = string.letters + string.digits

class ConversionJob(object):

    titleTranslations = {}

    def __init__(self, wikitFileName, outDir, titleTrans):
        self.wikitFileName = wikitFileName
        self.outDir = outDir
        self.storage = metakit.storage(wikitFileName,False)
        self.pagesVw = self.storage.view("pages")
        self.titleTrans = dict([(k.lower(),v) for (k,v) in titleTrans.items()])


    def convertAll(self):
        knownTitles = {}
        for row in self.pagesVw:
            knownTitles[row.name] = 1
        for row in self.pagesVw:
            txt = row.page
            name = row.name
            newName = self.pageNameToDirName(self.titleTrans.get(name.lower(),name))
            newTxt = self.wikit2moin(txt)
            self.makePage(self.outDir, newName, newTxt)

    def pageNameToDirName(self,pageNameUtf8):
        outChars = []
        for c in pageNameUtf8:
            if c == ' ':
                outChars.append('_')
            elif c in VALID_TITLE_CHARS:
                outChars.append(c)
            else:
                s = '(%.2x)'%ord(c)
                outChars.append(s)
        return ''.join(outChars)
            
    def wikit2moin(self, txt):
        def adjustLink(m):
            origLink = m.group(1).lower()
            if self.titleTrans.has_key(origLink):
                newLink = self.titleTrans[origLink]
            else:
                newLink = origLink
            return '["%s"]'%newLink
        
        # link
        txt = re.compile(r"\[([^\]]+)\]").sub(adjustLink, txt)

        # verbatim
        lines = txt.split('\n')
        inVerbatim = 0
        outLines = []
        for line in lines:
            if line == '':
                outLines.append('')
                continue
            if line.startswith(' ') \
                   and not line.startswith('   *') \
                   and not line.startswith('   1.') \
                   and  not inVerbatim:
                inVerbatim = 1
                outLines.append("{{{")
            elif line[0]!=' ' and inVerbatim:
                inVerbatim = 0
                outLines.append("}}}")
            outLines.append(line)
        txt = '\n'.join(outLines)
        return txt

    def makePage(self, new_dir, topic, txt):
        txt = unicode(txt, "utf8").encode("utf8")
        name = os.path.join(new_dir, topic)
        try:
            os.mkdir(name)
        except OSError:
            pass
        try:
            os.mkdir(os.path.join(name,"revisions"))
        except OSError:
            pass
        file(os.path.join(name,"current"), "w").write("00000001")
        file(os.path.join(name,"revisions","00000001"), "w").write(txt)

    def writeTransTemplate(self):
        print '{'
        for row in self.pagesVw:
            print repr(row.name),':',repr(row.name),','
        print '}'


titleTrans = eval(file("title-trans.txt").read())
cj = ConversionJob("wikit.tkd", "/srv/moin/data-spa/pages", titleTrans)

cj.writeTransTemplate()

cj.convertAll()
