gnumed-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gnumed-devel] twiki on gmwiki


From: catmat
Subject: [Gnumed-devel] twiki on gmwiki
Date: Tue, 01 Mar 2005 12:49:25 +1100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5) Gecko/20041231

There is a link in the gmwiki frontpage to a port of twiki gnumed pages to moinmoin.

I had the latest copy of moinmoin and it seems to have a different structure
to the one on salaam.

e.g. my copy of moin moin stores files in data/pages and each page on the twiki
is actually a directory with the structure:

PageName (directory with same name as TwikiPageName)
  -revisions (directory)
      000000001 (file)
      000000002 (file)
-current ( a file, with the number 00000002, meaning 00000002 is the current revision)

whereas on the salaam moinmoin the files are in data/text , and it's one file per
wiki page with the same name.

I've attached the two programs that do the first conversion from twiki gnumed, and then a second conversion
from the directory to the pages only structure.



twikidir = '.'
targetdir = './moin'
import os
import sys
import os.path
import traceback 
import re
import string


def twikimoin_format(l):
        l = match_heading(l)
        l = match_anchor(l)
        l = match_anchorlink1(l)
        l = twiki_spaced_wikiword(l)
        return l


re_heading = re.compile("\s*---(?P<level>\++)\s*(?P<heading>.+)")
def match_heading(l):
        mobj = re_heading.match(l)
        if mobj:
                print mobj.group("level"), mobj.group("heading")
                level = len(mobj.group("level"))
                l = level  * "=" +" "+ mobj.group("heading") + " " + level * "="
                print l
        return l

def match_anchor(l):
        l3 = l
        if l.lstrip() and  l.lstrip()[0] is '#':
                l2 = l.lstrip()[1:]
                l3 =  "[[Anchor("+l2.split(' ')[0].strip()+")]]"
        return l3


re_anchorlink1 = 
re.compile("[^\[]*\[\[\s*(?P<page>\w*)\#(?P<anchor>\w+)\]\[(?P<label>.+)\]\]")
re_anchorlink2 = 
re.compile("\[\[\s*Anchor\((?P<page>\w*)\#(?P<anchor>\w+)\)\]\[(?P<label>.+)\]\]")
def match_anchorlink1(l):
        mobj = re_anchorlink1.match(l)
        if mobj:
                print l[:-1] , " is an anchor link"
                page = mobj.group('page')
                anchor = mobj.group('anchor')
                label = mobj.group('label')
                if not page or not len(page):
                        l = "[#"+anchor+" "+label.strip() + "]"
                else:
                        l = "[:"+page+"#"+anchor+":"+label.strip()+"]"
        else:
                if l.lstrip()[:2] == '[[':
                        print l[:-1], " is not an anchor link"
        return l                
        
re_spaced_wikiword = 
re.compile("(?P<before>[^\[]*)\[\[(?P<word>\w+(\s+\w+)*)\](\[(?P<label>.+)\])?\](?P<after>.*)")
def twiki_spaced_wikiword(l):
        m = re_spaced_wikiword.match(l)
        if m:
                words = m.group('word')
                lwords = words.split(' ')
                if len(lwords) > 1:

                        def capitalizeUnlessUpper(x):
                                if x.isupper():
                                        return x
                                else:
                                        return x.capitalize()

                                        
                        words = "".join( [ capitalizeUnlessUpper(x) for x in 
words.split(' ')] )

                label = m.group('label')
                if label and len(label) > 0 :
                        label = " " + label
                else:
                        if len(lwords) > 1:
                                label = " " + " ".join(lwords)
                        else:
                                label = " " + words

                l = m.group('before') + "[wiki:Self:"+words+ 
label+"]"+m.group('after')
                
        return l

#main

try:
        os.mkdir(targetdir)
except:
        print "error trying to make " , targetdir
        print sys.exc_info()[0]


l = os.listdir(twikidir)

for f in l:
        if f[-3:] == 'txt':
                print f
                prefix = f[:-4]
                print prefix
                try:
                        path = os.path.join(targetdir, prefix)
                        print path
                except:
                        print "error creating path from ", targetdir, prefix
                        print sys.exc_info()[0]
                        continue
                try:
                        os.mkdir(path)
                except:
                        print "error creating ", path


                try:
                        revisions = os.path.join(path, "revisions")
                        os.mkdir(revisions)
                except:
                        print "error creating ", revisions
                try:    

                        one = os.path.join(revisions, "00000001")
                        targ = file(one, "w+")
                        for l in file(f):
                                l = twikimoin_format(l)
                                #continue
                                targ.write(l)
                                targ.write('\n')
                        continue        
                        targ2 = file(os.path.join(path, "current"), "w+")
                        targ2.write("00000001\n")
                except:
                        print sys.exc_info()[0]
                        traceback.print_tb( sys.exc_info()[2] )



        
import os

l = os.listdir("moin")
for d in l:
        src = file( os.path.join("./moin/"+d, "revisions/00000001") )
        targ = file( os.path.join("./text", d), "w+" )
        for l in src:
                targ.write(l)
        

reply via email to

[Prev in Thread] Current Thread [Next in Thread]