Attachment 'twiki_to_moin13.py'

Download

   1 #!/usr/bin/python
   2 import os
   3 import re
   4 import shutil
   5 
   6 def main(old_dir, new_dir, data_dir=None):
   7     names = os.listdir(old_dir)
   8     names.sort()
   9     for name in names:
  10         if name[-4:] == ".txt":
  11             topic = name[:-4]
  12             print topic
  13 
  14             txt = file(os.path.join(old_dir, name)).read()
  15             makePage(new_dir, topic, twiki2moin(txt))
  16             if data_dir:
  17                 copyAttachments(new_dir, data_dir, topic, txt)
  18 
  19 def twiki2moin(txt):
  20     # remove lines starting and ending with %
  21     txt = re.compile("^%.*%$", re.M).sub("", txt)
  22     # change attachment links
  23     txt = re.compile(r"\[\[%ATTACHURL%/(.*?)\]\[(.*?)\]\]").sub('[attachment:\\1 \\2]', txt)
  24     # change links
  25     txt = re.compile(r"\[\[(https?://[^\[\]]+)\]\[(.*?)\]\]").sub('[\\1 \\2]', txt)
  26     txt = re.compile(r"\[\[(.*?)\]\[(.*?)\]\]").sub('[:\\1:\\2]', txt)
  27     txt = re.compile(r"\[\[(.*?)\]\]").sub('["\\1"]', txt)
  28     # convert italic
  29     txt = re.compile(r"\b_([^*\n]*?)_").sub("''\\1''", txt)
  30     # convert bold
  31     txt = re.compile(r"\*\b([^*\n]*?)\*").sub("'''\\1'''", txt)
  32     # convert bold italic
  33     txt = re.compile(r"__\b([^*\n]*?)__").sub("''''\\1''''", txt)
  34     # convert verbatim
  35     txt = re.compile(r"\B=\b([^*\n]*?)=\B").sub("{{{\\1}}}", txt)
  36     # convert definition list 
  37     # Three spaces, a dollar sign, the term, a colon, a space, followed by the definition ( "   $ term: definition" -> "term:: definition" )
  38     txt = re.compile("   \$ (.*): (.*)").sub(" \\1:: \\2", txt)
  39     # convert headings
  40     txt = re.compile("^-?" + re.escape("---+++++") + "\s*(.*)$", re.M).sub("====== \\1 ======", txt)
  41     txt = re.compile("^-?" + re.escape("---++++") + "\s*(.*)$", re.M).sub("===== \\1 =====", txt)
  42     txt = re.compile("^-?" + re.escape("---+++") + "\s*(.*)$", re.M).sub("==== \\1 ====", txt)
  43     txt = re.compile("^-?" + re.escape("---++") + "\s*(.*)$", re.M).sub("=== \\1 ===", txt)
  44     txt = re.compile("^-?" + re.escape("---+") + "\s*(.*)$", re.M).sub("== \\1 ==", txt)
  45     txt = re.compile("^-?" + re.escape("---#") + "\s*(.*)$", re.M).sub("= \\1 =", txt)
  46     # remove signatures
  47     ## uncommento to enable ## txt = re.compile("^-- Main.([a-z]+) - [0-9]+ [a-zA-Z]+ 200[0-9]\s*\n?", re.M).sub("", txt)
  48     # tables
  49     txt = re.compile(r"\|", re.M).sub("||", txt)
  50     # rules
  51     txt = re.compile(r"^\s*<hr ?/?>\s*$", re.M).sub("----", txt)
  52     return txt
  53 
  54 def makePage(new_dir, topic, txt):
  55     txt = unicode(txt, "latin1").encode("utf8")
  56     name = os.path.join(new_dir, topic)
  57     try:
  58         os.mkdir(name)
  59     except OSError:
  60         pass
  61     try:
  62         os.mkdir(os.path.join(name,"revisions"))
  63     except OSError:
  64         pass
  65     file(os.path.join(name,"current"), "w").write("00000001")
  66     file(os.path.join(name,"revisions","00000001"), "w").write(txt)
  67 
  68 def copyAttachments(new_dir, data_dir, topic, txt):
  69     name = os.path.join(new_dir,topic)
  70     try:
  71         os.mkdir(name)
  72     except OSError:
  73         pass
  74     try:
  75         os.mkdir(os.path.join(name,"attachments"))
  76     except OSError:
  77         pass
  78     attachments = re.compile("%META:FILEATTACHMENT.*name=\"(.*?)\"\s",
  79         re.M).findall(txt)
  80     for attachment in attachments:
  81         try:
  82             shutil.copyfile(
  83                 os.path.join(data_dir,topic,attachment),
  84                 os.path.join(name,"attachments",attachment))
  85         except IOError:
  86             print "Could not copy attachment %s for topic %s" \
  87                 % (attachment, topic)
  88             pass
  89 
  90 if __name__ == '__main__':
  91     main("../awiki/data/Main", "moin-1.3.5/wiki/data/pages", "../awiki/pub/Main")

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2013-07-31 02:56:24, 11.0 KB) [[attachment:TWiki_To_Moin-1.0.tar.gz]]
  • [get | view] (2010-05-18 21:20:02, 3.6 KB) [[attachment:twiki_to_moin13.py]]
  • [get | view] (2009-02-03 05:52:29, 4.2 KB) [[attachment:twiki_to_moin18.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.