altworld /compile.py

Language Python Lines 69
MD5 Hash 5e966ae1c811fd4eb87263a05ef5340f Estimated Cost $1,139 (why?)
Repository https://github.com/gvx/altworld.git View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#import BaseHTTPServer
#import SimpleHTTPServer
import re
import os.path
#import posixpath
#import urllib

__version__ = "0.0"

f = open('template.txt')
TemplateText = f.read()
f.close()

def InsertPage(matchobj):
    page = os.path.join('src', matchobj.group(1))
    #if page:
    #    ctype, page = page
    try:
        f = open(page, 'r')
        txt = f.read()
        f.close()
      ##  print Wikify(txt)
        return Wikify(txt)
    except IOError as e:
        return 'ERROR: ' + str(e)

WikiSub = ((re.compile(r'\*(.*?)\*'), r'<strong>\1</strong>'),
           (re.compile(r'[[][[](.*?)[|](.*?)[]][]]'), r'<a href="\1.html">\2</a>'),
           (re.compile(r'[[][[](.*?)[]][]]'), r'<a href="\1.html">\1</a>'),
           ##(re.compile(r'[[]IMG[[](.*?)[]][]]'), r'<img src="\1">'),
           (re.compile(r"''(.*?)''"), r'<em>\1</em>'),
           (re.compile(r'^:(.*?)$', re.M), r'<h2>\1</h2>'),
           (re.compile(r'\n{2,}'), r'</p>\n\n<p>'),
##           (re.compile(r'(\r\n){2,}'), r'</p>\r\n\r\n<p>'),
           (re.compile(r'^'), r'<p>'),
           (re.compile(r'$'), r'</p>'),
           (re.compile(r'<p><h2>'), r'<h2>'),
           (re.compile(r'</h2></p>'), r'</h2>'),
           (re.compile(r'<p>{{(.*?)}}</p>'), InsertPage),
          )

def Wikify(text):
    for Regex, To in WikiSub:
        text = Regex.sub(To, text)
        ##print text
    return text

def Make(filename, source, outputfile):
    WikiText = source.read()
    WikiText = Wikify(WikiText)
    htmlText = TemplateText % {'text': WikiText, 'title': filename}
    outputfile.write(htmlText)

def CompileFile(arg, dir, files):
    for file in files:
        #continue
        try:
            print 'compiling', os.path.join('src', file), 'to', os.path.join('out', file)+'.html'
            fin = open(os.path.join('src', file))
            fout = open(os.path.join('out', file+'.html'), 'w')
            Make(file.replace('_', ' '), fin, fout)
        except:
            print 'error in compiling', os.path.join('src', file)
        finally:
            fin.close()
            fout.close()

os.path.walk('src', CompileFile, None)
Back to Top