Attachment 'twiki_to_moin18.py'

Download

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

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.