[r7423]: tags / 0.8a6 / docgen.py Maximize Restore History

Download this file

docgen.py    241 lines (214 with data), 9.6 kB

  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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import os
import sys
import shutil
import time
from setuptools import Command
import pkg_resources
pkg_resources.require("Kid >= 0.6.4")
import kid
import re
from distutils import log
class GenSite(Command):
"setuptools command to generate the TurboGears website"
user_options = [
("srcdirs=", "s", "directories containing the source files (default: docs)"),
("destdir=", "d", "destination output directory (default: dist/site)"),
("encoding=", "e", "encoding for output (default: utf8)"),
("force", "f", "regenerate all files"),
("ignoredirs=", "i", "directories to ignore (default: ['.svn', '.cvs'])"),
("ignorefiles=", "x", "files to ignore (default: ['.*\\.pyc', '.DS_Store'])"),
("templates=", "t", "mapping of templates to load (format: name=templatefile,name=templatefile)"),
("copydirs=", "c", "copy files from these directories without template proc. (destdir=srcdir,...)")
]
boolean_options=["force"]
srcdirs = None
destdir = "dist/site"
encoding = "utf8"
force = False
ignoredirs = None
ignorefiles = None
templates = None
copydirs = None
def initialize_options(self):
pass
def finalize_options(self):
if self.srcdirs is None:
self.srcdirs = ["docs"]
self.ensure_string_list("srcdirs")
self.ensure_string("destdir", "dist/site")
self.ensure_string("encoding", "utf8")
if self.ignoredirs is None:
self.ignoredirs = [".svn", ".cvs"]
self.ensure_string_list("ignoredirs")
if self.ignorefiles is None:
self.ignorefiles = ['.*\\.pyc', '.DS_Store']
self.ensure_string_list("ignorefiles")
regexes = []
for pat in self.ignorefiles:
regexes.append(re.compile(pat))
self.ignorepatterns = regexes
self.templates = self._split_mapping(self.templates)
self.copydirs = self._split_mapping(self.copydirs)
def _split_mapping(self, valToSplit):
mapping = {}
if valToSplit and isinstance(valToSplit, basestring):
pairs = re.split(",\s*", valToSplit)
for pair in pairs:
name, filename = re.split("\s*=\s*", pair)
mapping[name] = os.path.abspath(filename)
return mapping
def check_if_newer(self, src, dest):
srcmtime = os.path.getmtime(src)
if os.path.exists(dest):
destmtime = os.path.getmtime(dest)
else:
destmtime = 0
return srcmtime > destmtime
def copy_if_newer(self, src, dest):
if self.force or self.check_if_newer(src, dest):
d = os.path.dirname(dest)
if not os.path.exists(d):
os.makedirs(d)
self.copy_file(src, dest)
def render_template(self, src, dest, depth):
if not self.force and not self.check_if_newer(src, dest):
return
if not self.dry_run:
log.info("rendering %s" % dest)
else:
log.info("skipping rendering %s" % dest)
return
template = kid.load_template(src, cache=False)
template.Template.serializer = self.serializer
toroot = "../" * depth
destfile = dest[len(self.destdir)+1:]
updated = time.strftime("%b %d, %Y", time.localtime(os.path.getmtime(src)))
output = template.serialize(encoding=self.encoding, root=toroot, updated=updated,
destfile=destfile)
output = output.replace("$$", "$")
destfile = open(dest, "w")
destfile.write(output)
destfile.close()
def update_site_files(self, srcdir, processTemplates = True, destroot=None):
if not destroot:
destroot = self.destdir
for root, dirs, files in os.walk(srcdir):
if root != srcdir:
fromroot = root[len(srcdir)+1:]
segments = fromroot.split(os.sep)
if set(segments).intersection(self.ignoredirs):
continue
depth = len(segments)
else:
fromroot = ""
depth = 0
destdir = os.path.join(destroot, fromroot)
if not os.path.exists(destdir):
if not self.dry_run:
log.info("creating directory %s" % (destdir))
os.makedirs(destdir)
else:
log.info("skipping creating directory %s" % (destdir))
for file in files:
ignore = False
abs = os.path.abspath(file)
for pat in self.ignorepatterns:
if pat.match(file):
ignore = True
break
if ignore:
continue
for tempfile in self.templates.values():
if tempfile == abs:
ignore = True
break
if ignore:
continue
ext = os.path.splitext(file)[1]
dest = os.path.join(destdir, file)
self.currentfiles.add(dest)
if not processTemplates or ext != ".html":
self.copy_if_newer(os.path.join(root, file),
dest)
else:
self.render_template(os.path.join(root, file),
dest, depth)
def delete_excess_files(self):
for root, dirs, files in os.walk(self.destdir):
for file in files:
dest = os.path.join(root, file)
if dest not in self.currentfiles:
if not self.dry_run:
log.info("deleting %s" % dest)
os.unlink(dest)
else:
log.info("skipping deleting %s" % dest)
def run(self):
destdir = self.destdir
log.info("generating website to %s" % destdir)
if not os.path.exists(destdir):
log.info("creating %s" % destdir)
os.mkdir(destdir)
for name, filename in self.templates.items():
log.info("template %s loaded as %s" % (filename, name))
kid.load_template(filename, name=name)
self.currentfiles = set()
self.serializer = kid.HTMLSerializer(encoding=self.encoding)
for d in self.srcdirs:
self.update_site_files(d)
for dest, src in self.copydirs.items():
if os.path.isdir(src):
self.update_site_files(src, processTemplates=False,
destroot=os.path.join(self.destdir, dest))
else:
destfile = os.path.join(self.destdir, os.path.normpath(dest))
self.copy_if_newer(src, destfile)
self.currentfiles.add(destfile)
self.printable_tutorial()
self.delete_excess_files()
def printable_tutorial(self):
import cElementTree as elementtree
tutdir = os.path.join("docs", "docs", "wiki20")
masterdoc = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?python import printable ?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://purl.org/kid/ns#" py:extends="printable">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type" />
<link rel="stylesheet" type="text/css" href="../../default.css" py:attrs="href=root+'default.css'"/>
<link type="text/css" rel="stylesheet" href="../../sh/SyntaxHighlighter.css" py:attrs="href=root+'sh/SyntaxHighlighter.css'"></link>
<title>TurboGears: 20 Minute Wiki Tutorial</title>
</head>
<body>
"""
docs = os.listdir(tutdir)
docs.sort()
for doc in docs:
if not doc.endswith(".html"):
continue
log.info("combining %s" % doc)
tree = elementtree.parse(os.path.join(tutdir, doc))
body = tree.find("{http://www.w3.org/1999/xhtml}body")
map(body.remove, body.findall("{http://www.w3.org/1999/xhtml}script"))
bodytext = elementtree.tostring(body)
bodytext = bodytext.replace("</html:body>", "")
bodytext = bodytext.replace('<html:body xmlns:html="http://www.w3.org/1999/xhtml">', "")
masterdoc += bodytext
masterdoc += """<script src="../../sh/shCore.js" py:attrs="src=root+'sh/shCore.js'"></script>
<script src="../../sh/shBrushPython.js" py:attrs="src=root+'sh/shBrushPython.js'"></script>
<script src="../../sh/shBrushXml.js" py:attrs="src=root+'sh/shBrushXml.js'"></script>
<script src="../../sh/shBrushJScript.js" py:attrs="src=root+'sh/shBrushJScript.js'"></script>
<script language="javascript">
dp.SyntaxHighlighter.HighlightAll('code');
</script>
</body></html>"""
masterdoc = masterdoc.replace("html:", "")
template = kid.Template(source=masterdoc, root="../../")
template.serializer = self.serializer
outfn = os.path.join("dist", "site", "docs", "wiki20",
"printable.html")
outfile = open(outfn, "w")
masterdoc = template.serialize(encoding=self.encoding)
masterdoc = masterdoc.replace("$${", "${")
outfile.write(masterdoc)
outfile.close()
self.currentfiles.add(outfn)