root/hodgestar/PythonCode/Tiki2Pm/pmwiki.py

Revision 377, 2.9 kB (checked in by simon, 4 years ago)

Scripts for moving TikiWiki? data into PmWiki? and transforming the markup.

  • Property svn:mime-type set to text/python-source
  • Property svn:eol-style set to native
Line 
1import sets, os, time, string, array
2import logging
3
4class PmWikiWriter(object):
5    PER_GROUP_UPLOAD = 1
6    PER_PAGE_UPLOAD = 2
7    GLOBAL_UPLOAD = 3
8
9    def __init__(self,sWikiDir,sPmWikiVersion,sUploadsDir,iUploadsPrefix):
10        self.__sWikiDir = sWikiDir
11        self.__sPmWikiVersion = sPmWikiVersion
12        self.__sUploadsDir = sUploadsDir
13        self.__iUploadsPrefix = iUploadsPrefix
14       
15    def writePage(self,sPage,sGroup,sAuthor,sText,oTime=None,oCtime=None):               
16        sOut = os.path.join(self.__sWikiDir,sGroup + "." + sPage)
17        fOut = file(sOut,"w")
18       
19        fOut.write("version=pmwiki-%s urlencoded=1\n" % (self.__sPmWikiVersion))
20        fOut.write("author=" + sAuthor + "\n")
21        fOut.write("text=")
22        fOut.write(sText.replace("%",'%25').replace("\n",'%0a'))
23        fOut.write("\n")
24        if not oTime is None:
25            fOut.write("time=" + str(int(time.mktime(oTime.timetuple()))) + "\n")
26        if not oCtime is None:
27            fOut.write("ctime=" + str(int(time.mktime(oCtime.timetuple()))) + "\n")
28        if oTime is None and not oCtime is None:
29            fOut.write("time=" + str(int(time.mktime(oCtime.timetuple()))) + "\n")
30        if oTime is None and oCtime is None:
31            fOut.write("time=1\n")       
32        fOut.close()
33
34    def writeFile(self,sPage,sGroup,sData,sFilename):
35        sGroupPath = ''
36        sPagePath = ''
37        if self.__iUploadsPrefix == self.PER_GROUP_UPLOAD:
38            sGroupPath = sGroup
39        if self.__iUploadsPrefix == self.PER_PAGE_UPLOAD:
40            sGroupPath = sGroup
41            sPagePath = sPage
42
43        sOut = os.path.join(self.__sUploadsDir, sGroupPath, sPagePath, sFilename)
44        fOut = file(sOut,"w")
45        # nasty hack for old mysqldb
46        if (type(sData) == type(array.array("c"))):
47            sData = sData.tostring()
48        fOut.write(sData)
49        fOut.close()
50
51class MarkUpTranslator(object):
52    def __init__(self):
53        self.__aTrans = []
54        self.__dMatches = {}
55       
56    def addRule(self,oRegEx,sfReplace,sType):
57        self.__aTrans.append((oRegEx,sfReplace,sType))
58        if not self.__dMatches.has_key(sType):
59            self.__dMatches[sType] = sets.Set()
60           
61    def translate(self,sPageName,sText):
62        for oRegEx, sfReplace, sType in self.__aTrans:
63            sText, n = oRegEx.subn(sfReplace,sText) 
64            if n > 0 and not sPageName is None:
65                self.__dMatches[sType].add(sPageName)
66        return sText
67       
68    def matches(self):
69        for sType, oSet in self.__dMatches.iteritems():
70            yield sType, oSet
71
72def safePageName(s):
73    s = s.replace("/"," ")
74    s = s.replace(":"," ")
75    s = s.replace("."," ")
76    s = s.replace("-"," ")
77    s = s.replace("?"," ")
78    s = s.replace("!"," ")
79    s = s.replace("'"," ")
80    s = s.replace("_"," ")
81    s = s.replace("-"," ")
82    s = " ".join([x[0].upper() + x[1:] for x in s.split()])
83    s = s.replace(" ","")
84    return s
Note: See TracBrowser for help on using the browser.