Author: ianb
Date: 2004-04-13 00:42:26 -0600 (Tue, 13 Apr 2004)
New Revision: 43
Added:
Wiki/lib/pooledtemplate.py
Modified:
Wiki/Context/Main.py
Wiki/lib/wiki.py
Wiki/lib/wikiconfig.py
Wiki/lib/wikipage.py
Wiki/wiki.ini
Log:
Added static publishing; still need backlink resolution
Modified: Wiki/Context/Main.py
===================================================================
--- Wiki/Context/Main.py 2004-04-13 06:17:09 UTC (rev 42)
+++ Wiki/Context/Main.py 2004-04-13 06:42:26 UTC (rev 43)
@@ -10,6 +10,8 @@
if not name:
self.sendRedirectAndEnd(self.wiki.linkTo('frontpage'))
return
+ if name.endswith('.html'):
+ name = name[:-5]
if name != wikipage.canonicalName(name):
self.sendRedirectAndEnd(self.wiki.linkTo(wikipage.canonicalName(name)))
return
Added: Wiki/lib/pooledtemplate.py
===================================================================
--- Wiki/lib/pooledtemplate.py 2004-04-13 06:17:09 UTC (rev 42)
+++ Wiki/lib/pooledtemplate.py 2004-04-13 06:42:26 UTC (rev 43)
@@ -0,0 +1,51 @@
+"""
+Pooled Cheetah templates
+"""
+
+from Cheetah.Template import Template as CheetahTemplate
+
+class Template(object):
+
+ _keywords = ['cacheSize', 'searchList']
+ cacheSize = 10
+ searchList = []
+
+ def __init__(self, *args, **kw):
+ for name in self._keywords:
+ if kw.has_key(name):
+ setattr(self, name, kw[name])
+ del kw[name]
+ self.args = args
+ self.kw = kw
+ self.pool = []
+
+ def eval(self, **namespace):
+ tmpl = None
+ try:
+ tmpl, tmplNamespace = self.getTemplate()
+ tmplNamespace.clear()
+ tmplNamespace.update(namespace)
+ result = str(tmpl)
+ finally:
+ if tmpl:
+ self.returnTemplate(tmpl, tmplNamespace)
+ return result
+
+ def getTemplate(self):
+ try:
+ return self.pool.pop()
+ except IndexError:
+ return self.newTemplate()
+
+ def newTemplate(self):
+ namespace = {}
+ kw = self.kw.copy()
+ kw['searchList'] = self.searchList + [namespace]
+ tmpl = CheetahTemplate(*self.args, **kw)
+ return tmpl, namespace
+
+ def returnTemplate(self, tmpl, namespace):
+ if len(self.pool) >= self.cacheSize:
+ # Throw it away
+ return
+ self.pool.append((tmpl, namespace))
Modified: Wiki/lib/wiki.py
===================================================================
--- Wiki/lib/wiki.py 2004-04-13 06:17:09 UTC (rev 42)
+++ Wiki/lib/wiki.py 2004-04-13 06:42:26 UTC (rev 43)
@@ -4,6 +4,10 @@
import propertymeta
import threading
import wikiindex
+try:
+ import pooledtemplate
+except ImportError:
+ pooledtemplate = None
class GlobalWiki(object):
@@ -77,15 +81,18 @@
os.mkdir(self.config.basePath)
self.globalWiki = globalWiki
self.canonical = canonical
+ self.template = None
if not canonical:
if config.rebuildIndex or \
- wikiindex.WikiIndex.exists(self.config.basePath):
+ not wikiindex.WikiIndex.exists(self.config.basePath):
needRebuild = True
else:
needRebuild = False
self.index = wikiindex.WikiIndex(self.config.basePath)
if needRebuild:
self._rebuildIndex()
+ if config.rebuildStatic:
+ self._rebuildStatic()
else:
self.index = canonical.index
@@ -104,6 +111,11 @@
pageName = pageName.name
return self.config.baseHref + pageName
+ def staticLinkTo(self, pageName):
+ if isinstance(pageName, wikipage.WikiPage):
+ pageName = pageName.name
+ return self.config.staticHref + pageName
+
def filenameForName(self, filename):
return os.path.join(self.config.basePath, filename + '.txt')
@@ -156,6 +168,9 @@
rss.addItem(item, 10)
rss.writeText()
self.index.setLinks(page.name, page.wikiLinks())
+ # @@: *If* this is a page-create, then any pages that link
+ # to this page should also be published.
+ self.publish(page)
def backlinks(self, page):
return [self.page(name)
@@ -166,6 +181,24 @@
for page in self.allPages():
self.index.setLinks(page.name, page.wikiLinks())
+ def publish(self, page):
+ if not self.config.staticPublish:
+ return
+ if self.template is None:
+ self.template = pooledtemplate.Template(file=self.config.staticTemplate)
+ result = self.template.eval(page=page)
+ if not os.path.exists(self.config.staticPath):
+ os.mkdir(self.config.staticPath)
+ filename = os.path.join(self.config.staticPath, page.name + '.html')
+ f = open(filename, 'w')
+ f.write(result)
+ f.close()
+
+ def _rebuildStatic(self):
+ print "Rebuilding static pages"
+ for page in self.allPages():
+ self.publish(page)
+
def rss__get(self):
"""
Returns the rssobject.RSS object.
@@ -176,3 +209,13 @@
if value:
setattr(rss, attribute, value)
return rss
+
+ def __repr__(self):
+ return '<%s>' % self.shortRepr()
+
+ def shortRepr(self):
+ if not self.canonical:
+
+ return 'Wiki:%s' % self.config.domain
+ else:
+ return 'Wiki:%s aliases %s' % (self.config.domain, self.config.canonicalDomain)
Modified: Wiki/lib/wikiconfig.py
===================================================================
--- Wiki/lib/wikiconfig.py 2004-04-13 06:17:09 UTC (rev 42)
+++ Wiki/lib/wikiconfig.py 2004-04-13 06:42:26 UTC (rev 43)
@@ -93,7 +93,32 @@
def rebuildIndex__get(self):
return self._cascadeGet('rebuildindex', type='getboolean',
default=False)
+ def rebuildStatic__get(self):
+ return self._cascadeGet('rebuildStatic', type='getboolean',
+ default=False)
+ def staticPublish__get(self):
+ return self._cascadeGet('staticpublish',
+ type='getboolean',
+ default=False)
+ def staticTemplate__get(self):
+ value = self._cascadeGet('statictemplate', default=None)
+ value = value or 'static.tmpl'
+ return os.path.join(self.basePath, value)
+ def staticPath__get(self):
+ value = self._cascadeGet('staticpath', default=None)
+ value = value or 'static'
+ return os.path.join(self.basePath, value)
+ def staticHref__get(self):
+ value = self._cascadeGet('statichref', default=None)
+ if not value:
+ return ''
+ else:
+ if not value.endswith('/'):
+ return value + '/'
+ else:
+ return value
+
def normalizeHref(v):
if not v.endswith('/'):
return v + '/'
Modified: Wiki/lib/wikipage.py
===================================================================
--- Wiki/lib/wikipage.py 2004-04-13 06:17:09 UTC (rev 42)
+++ Wiki/lib/wikipage.py 2004-04-13 06:42:26 UTC (rev 43)
@@ -48,6 +48,9 @@
self.version = version
self.metadata = rfc822persist.RFC822Dict(self.basePath + '.meta')
+ def __repr__(self):
+ return '<WikiPage:%s in %s>' % (self.name, self.wiki.shortRepr())
+
def basePath__get(self):
"""
Returns the base path (sans extension) for this page.
@@ -72,6 +75,15 @@
else:
return 'This page has not yet been created.'
+ def staticHTML__get(self):
+ """Returns text of HTML with links to non-existent pages
+ removed (HTML fragment only)"""
+ if self.exists():
+ return self._subStaticLinks(self._rawHTML())
+ else:
+ # @@: Should this signal an error?
+ return 'Page Not Found'
+
def _rawHTML(self):
if not self.exists():
return ''
@@ -98,8 +110,6 @@
_wikiLinkRE = re.compile(r'(<a [^>]* href=")!(.*?)("[^>]*>)(.*?)(</a>)',
re.I+re.S)
- def _subWikiLinks(self, text):
- return self._wikiLinkRE.sub(self._subLink, ' %s ' % text)
def wikiLinks(self):
"""
@@ -113,7 +123,10 @@
def backlinks__get(self):
return self.wiki.backlinks(self)
- def _subLink(self, match):
+ def _subWikiLinks(self, text):
+ return self._wikiLinkRE.sub(self._subWikiLinksSubber, ' %s ' % text)
+
+ def _subWikiLinksSubber(self, match):
if self.wiki.exists(match.group(2)):
return match.group(1) + self.wiki.linkTo(match.group(2)) + match.group(3) + match.group(4) + match.group(5)
else:
@@ -124,6 +137,16 @@
match.group(3),
match.group(5))
+ def _subStaticLinks(self, text):
+ return self._wikiLinkRE.sub(self._subStaticLinksSubber, ' %s ' % text)
+ def _subStaticLinksSubber(self, match):
+ if self.wiki.exists(match.group(2)):
+ # Normal link...
+ return match.group(1) + self.wiki.staticLinkTo(match.group(2)) + match.group(3) + match.group(4) + match.group(5)
+ else:
+ # Don't include any link at all...
+ return match.group(4)
+
def text__get(self):
"""
The text of the page. ReStructuredText is used, though the
Modified: Wiki/wiki.ini
===================================================================
--- Wiki/wiki.ini 2004-04-13 06:17:09 UTC (rev 42)
+++ Wiki/wiki.ini 2004-04-13 06:42:26 UTC (rev 43)
@@ -13,3 +13,7 @@
[wiki.webwareforpython.org]
rss.title = Webware Wiki
rss.description = The Webware For Python Wiki
+StaticPublish = yes
+StaticTemplate = static.tmpl
+StaticPath = /home/ianb/public_html/staticwiki
+rebuildStatic = yes
\ No newline at end of file
|