wikit2moin.py

This script converts Wikit (http://www.equi4.com/wikit/) databases to Moin.

Supported markup

There is no support for subpages, headlines, attachments or tables in Wikit, which makes conversion easy.

Requirements

Usage

Since this is a one-off to convert just one 400-page wikit, I didn't bother to make a UI. Just edit the last lines of the source to suit your needs.

There is also support for title translation. The script expects title-trans.txt to contain a (possibly incomplete) dict literal mapping from original titles to new titles. cj.writeTransTemplate writes a template which you can edit and save as title-trans.txt.

Author: KonradAnton, with Moin file output borrowed from TwikiConverter. License: GPL.

   1 #!/usr/bin/env python
   2 
   3 import metakit, re, string, os
   4 
   5 VALID_TITLE_CHARS = string.letters + string.digits
   6 
   7 class ConversionJob(object):
   8 
   9     titleTranslations = {}
  10 
  11     def __init__(self, wikitFileName, outDir, titleTrans):
  12         self.wikitFileName = wikitFileName
  13         self.outDir = outDir
  14         self.storage = metakit.storage(wikitFileName,False)
  15         self.pagesVw = self.storage.view("pages")
  16         self.titleTrans = dict([(k.lower(),v) for (k,v) in titleTrans.items()])
  17 
  18 
  19     def convertAll(self):
  20         knownTitles = {}
  21         for row in self.pagesVw:
  22             knownTitles[row.name] = 1
  23         for row in self.pagesVw:
  24             txt = row.page
  25             name = row.name
  26             newName = self.pageNameToDirName(self.titleTrans.get(name.lower(),name))
  27             newTxt = self.wikit2moin(txt)
  28             self.makePage(self.outDir, newName, newTxt)
  29 
  30     def pageNameToDirName(self,pageNameUtf8):
  31         outChars = []
  32         for c in pageNameUtf8:
  33             if c == ' ':
  34                 outChars.append('_')
  35             elif c in VALID_TITLE_CHARS:
  36                 outChars.append(c)
  37             else:
  38                 s = '(%.2x)'%ord(c)
  39                 outChars.append(s)
  40         return ''.join(outChars)
  41             
  42     def wikit2moin(self, txt):
  43         def adjustLink(m):
  44             origLink = m.group(1).lower()
  45             if self.titleTrans.has_key(origLink):
  46                 newLink = self.titleTrans[origLink]
  47             else:
  48                 newLink = origLink
  49             return '["%s"]'%newLink
  50         
  51         # link
  52         txt = re.compile(r"\[([^\]]+)\]").sub(adjustLink, txt)
  53 
  54         # verbatim
  55         lines = txt.split('\n')
  56         inVerbatim = 0
  57         outLines = []
  58         for line in lines:
  59             if line == '':
  60                 outLines.append('')
  61                 continue
  62             if line.startswith(' ') \
  63                    and not line.startswith('   *') \
  64                    and not line.startswith('   1.') \
  65                    and  not inVerbatim:
  66                 inVerbatim = 1
  67                 outLines.append("{{{")
  68             elif line[0]!=' ' and inVerbatim:
  69                 inVerbatim = 0
  70                 outLines.append("}}}")
  71             outLines.append(line)
  72         txt = '\n'.join(outLines)
  73         return txt
  74 
  75     def makePage(self, new_dir, topic, txt):
  76         txt = unicode(txt, "utf8").encode("utf8")
  77         name = os.path.join(new_dir, topic)
  78         try:
  79             os.mkdir(name)
  80         except OSError:
  81             pass
  82         try:
  83             os.mkdir(os.path.join(name,"revisions"))
  84         except OSError:
  85             pass
  86         file(os.path.join(name,"current"), "w").write("00000001")
  87         file(os.path.join(name,"revisions","00000001"), "w").write(txt)
  88 
  89     def writeTransTemplate(self):
  90         print '{'
  91         for row in self.pagesVw:
  92             print repr(row.name),':',repr(row.name),','
  93         print '}'
  94 
  95 
  96 titleTrans = eval(file("title-trans.txt").read())
  97 cj = ConversionJob("wikit.tkd", "/srv/moin/data-spa/pages", titleTrans)
  98 
  99 cj.writeTransTemplate()
 100 
 101 cj.convertAll()
wikit2moin.py

MoinMoin: WikitConverter (last edited 2007-10-29 19:19:17 by localhost)