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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
#! /usr/bin/env python
"""simple wsgi app for serving mediawiki content
"""
import os
import mimetypes
import StringIO
from mwlib import uparser, htmlwriter, rendermath
class Pngmath(object):
def __init__(self, basedir):
self.basedir = basedir
def __call__(self, env, start_response):
pi = env['PATH_INFO']
path = pi.split('/', 2)[-1]
path = path.strip("/")
path = path[:-len(".png")]
pngfile = os.path.join(self.basedir, path+'.png')
if not os.path.exists(pngfile):
texfile = os.path.join(self.basedir, path+'.tex')
if not os.path.exists(texfile):
start_response('404 Not found', [('Content-Type', 'text/plain')])
return ["404 not found"]
r = rendermath.Renderer()
r._render_file(path, 'png')
d=open(pngfile, 'rb').read()
start_response('200 Ok', [('Content-Type', 'image/png')])
return [d]
class Files(object):
def __init__(self, basedir):
self.basedir = basedir
def __call__(self, env, start_response):
pi = env['PATH_INFO']
path = pi.split('/', 2)[-1]
path = path.strip("/")
assert ".." not in path, "path must not contain '..'"
mt, enc = mimetypes.guess_type(path)
try:
f=open(os.path.join(self.basedir, path), 'rb')
except (IOError, OSError), err:
print "ERROR:", err
start_response('404 Not found', [('Content-Type', 'text/plain')])
return ["404 not found"]
send = start_response('200 OK', [('Content-type', mt or 'text/plain; charset=utf-8')])
while 1:
data=f.read(0x20000)
if not data:
break
send(data)
return []
class Serve(object):
head = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset="utf-8"></meta>
<link rel="stylesheet" href="/resources/pedia.css" />
</head>
<body>
"""
def __init__(self, db, images):
self.db = db
self.images = images
from mwlib import resources
self.resources = Files(os.path.dirname(resources.__file__)) # FIXME
self.image_files = Files(os.path.expanduser("~/images")) # FIXME
self.pngmath = Pngmath(os.path.expanduser("~/pngmath")) # FIXME
self.timeline = Files(os.path.expanduser("~/timeline")) # FIXME
def show(self, env, start_response):
article = unicode(env['PATH_INFO'], 'utf-8').strip('/').replace("_", " ")
article = article[:1].upper()+article[1:] # FIXME: we should redirect instead.
raw=self.db.getRawArticle(article)
if not raw:
start_response('404 Not found', [('Content-Type', 'text/plain')])
return ["Article %r not found" % (article,)]
send = start_response('200 OK', [('Content-type', 'text/html; charset=utf-8')])
send(self.head)
out=StringIO.StringIO(u"")
a=uparser.parseString(article, raw=raw, wikidb=self.db)
w=htmlwriter.HTMLWriter(out, self.images)
w.write(a)
return [out.getvalue().encode('utf-8')]
def __call__(self, env, start_response):
path = env['PATH_INFO']
if path.startswith("/resources/"):
return self.resources(env, start_response)
if path.startswith("/images"):
return self.image_files(env, start_response)
if path.startswith("/pngmath/"):
return self.pngmath(env, start_response)
if path.startswith("/timeline/"):
return self.timeline(env, start_response)
return self.show(env, start_response)
start_response('404 Not found', [('Content-Type', 'text/plain')])
return ["404 Not found"]
|