constance

Scripts for generating (an earlier obsolete version of) my personal web site
git clone https://code.djc.id.au/git/constance/
commit 1ab628a470939d1ab6e2ae9f65768809f96c8fab
parent 066c8dad447012d44c6b30eef810751223e1cc8c
Author: Dan Callaghan <djc@djc.id.au>
Date:   Wed,  3 Sep 2008 11:04:26 +1000

extract URLs from WSGI environ, rather than configuring them

Diffstat:
Mapp.py | 13++++++++++---
Mconfig.py | 2--
Mtemplates/_commonwrapper.xml | 2+-
Mtemplates/_entry.xml | 8++++----
Mtemplates/multiple_atom.xml | 10+++++-----
Mviewutils.py | 4++--
6 files changed, 22 insertions(+), 17 deletions(-)
diff --git a/app.py b/app.py
@@ -2,6 +2,7 @@
 # vim:encoding=utf-8
 
 import os
+import wsgiref.util
 from genshi.template import TemplateLoader
 from colubrid import RegexApplication, HttpResponse, execute
 from colubrid.exceptions import PageNotFound, HttpFound
@@ -25,6 +26,7 @@ class Constance(RegexApplication):
 
     def __init__(self, *args, **kwargs):
         super(Constance, self).__init__(*args, **kwargs)
+        self.request.environ['APP_URI'] = wsgiref.util.application_uri(self.request.environ) # Colubrid ought to do this for us
         self.entries = blog.Entries(config.ENTRIES_DIR, config.READINGLOG_FILE)
 
     def index(self):
@@ -33,6 +35,7 @@ class Constance(RegexApplication):
         format = self.request.args.get('format', 'html')
         if format == 'html':
             rendered = template_loader.load('multiple.xml').generate(
+                    environ=self.request.environ, 
                     title=None, 
                     sorted_entries=sorted_entries, 
                     offset=offset,
@@ -40,8 +43,9 @@ class Constance(RegexApplication):
             return HttpResponse(rendered, [('Content-Type', 'text/html')], 200)
         elif format == 'atom':
             rendered = template_loader.load('multiple_atom.xml').generate(
+                    environ=self.request.environ, 
                     title=None, 
-                    url=config.ABS_BASE + '/', 
+                    self_url='%s/' % self.request.environ['APP_URI'], 
                     sorted_entries=sorted_entries[:config.ENTRIES_PER_PAGE], 
                     feed_updated=sorted_entries[0].modified_date
                     ).render('xml')
@@ -54,6 +58,7 @@ class Constance(RegexApplication):
         try:
             entry = self.entries[id]
             rendered = template_loader.load('single.xml').generate(
+                    environ=self.request.environ, 
                     entry=entry
                     ).render('xhtml')
             return HttpResponse(rendered, [('Content-Type', 'text/html')], 200)
@@ -71,6 +76,7 @@ class Constance(RegexApplication):
         format = self.request.args.get('format', 'html')
         if format == 'html':
             rendered = template_loader.load('multiple.xml').generate(
+                    environ=self.request.environ, 
                     title=u'“%s” tag' % tag, 
                     sorted_entries=sorted_entries, 
                     offset=offset
@@ -78,8 +84,9 @@ class Constance(RegexApplication):
             return HttpResponse(rendered, [('Content-Type', 'text/html')], 200)
         elif format == 'atom':
             rendered = template_loader.load('multiple_atom.xml').generate(
+                    environ=self.request.environ, 
                     title=u'“%s” tag' % tag, 
-                    url='%s/+tags/%s' % (config.ABS_BASE, tag.encode(self.charset)), 
+                    self_url='%s/+tags/%s' % (self.request.environ['APP_URI'], tag.encode(self.charset)), 
                     sorted_entries=sorted_entries[:config.ENTRIES_PER_PAGE], 
                     feed_updated=sorted_entries[0].modified_date
                     ).render('xml')
@@ -91,4 +98,4 @@ class Constance(RegexApplication):
 if __name__ == '__main__':
     app = Constance
     app = StaticExports(app, {'/static': os.path.join(os.path.dirname(__file__), 'static')})
-    execute(app)
+    execute(app, hostname='0.0.0.0', port=8082)
diff --git a/config.py b/config.py
@@ -5,8 +5,6 @@ import os
 
 ENTRIES_DIR = os.path.join(os.path.dirname(__file__), u'entries')
 READINGLOG_FILE = os.path.join(os.path.dirname(__file__), u'reading_log')
-REL_BASE = ''
-ABS_BASE = 'http://www.djc.id.au%s' % REL_BASE
 BLOG_NAME = u'djc'
 BLOG_AUTHOR = u'djc'
 BLOG_EMAIL = None
diff --git a/templates/_commonwrapper.xml b/templates/_commonwrapper.xml
@@ -18,7 +18,7 @@ import config
 		<title py:if="not title">djc</title>
         <meta http-equiv="content-type" content="text/html; charset=utf-8" />
 		<meta name="generator" content="constance" />
-        <link rel="stylesheet" type="text/css" href="${config.REL_BASE}/static/css/common.css" />
+        <link rel="stylesheet" type="text/css" href="${environ.get('SCRIPT_NAME', '')}/static/css/common.css" />
 	</head>
 </py:match>
 
diff --git a/templates/_entry.xml b/templates/_entry.xml
@@ -14,11 +14,11 @@ from viewutils import mini_markdown, tag_list
 
 	<div class="entrydate">
 		${entry.publication_date.strftime(str('%-1d %b %Y'))}
-	    <a href="${config.REL_BASE}/${entry.id}" rel="bookmark" class="permalink" title="permalink">#</a>
+	    <a href="${environ.get('SCRIPT_NAME', '')}/${entry.id}" rel="bookmark" class="permalink" title="permalink">#</a>
 	</div>
 
     <div py:if="entry.tags" class="entrytags">
-        tagged: ${tag_list(entry.tags)}
+        tagged: ${tag_list(environ.get('SCRIPT_NAME', ''), entry.tags)}
     </div>
   
 	<div class="entrybody">
@@ -26,7 +26,7 @@ from viewutils import mini_markdown, tag_list
     </div>
 
     <div class="entrycommentslink" py:if="not show_comments and entry.has_comments()">
-        <a href="${config.REL_BASE}/${entry.id}#comments" py:choose="len(entry.comments())">
+        <a href="${environ.get('SCRIPT_NAME', '')}/${entry.id}#comments" py:choose="len(entry.comments())">
             <py:when test="0">no comments »</py:when>
             <py:when test="1">1 comment »</py:when>
             <py:otherwise>${len(entry.comments())} comments »</py:otherwise>
@@ -54,7 +54,7 @@ from viewutils import mini_markdown, tag_list
 </div>
 
 <span py:def="stars(rating)" py:strip="True">
-<img src="${config.REL_BASE}/static/images/star.png" alt="[star]" py:for="_ in range(int(rating))" /><img src="${config.REL_BASE}/static/images/star-half.png" alt="[half-star]" py:if="rating > int(rating)" /><img src="${config.REL_BASE}/static/images/star-off.png" alt="" py:for="_ in range(int(5 - rating))" />
+<img src="${environ.get('SCRIPT_NAME', '')}/static/images/star.png" alt="[star]" py:for="_ in range(int(rating))" /><img src="${environ.get('SCRIPT_NAME', '')}/static/images/star-half.png" alt="[half-star]" py:if="rating > int(rating)" /><img src="${environ.get('SCRIPT_NAME', '')}/static/images/star-off.png" alt="" py:for="_ in range(int(5 - rating))" />
 </span>
 
 <div class="entry readinglog" py:if="isinstance(entry, blog.ReadingLogEntry)">
diff --git a/templates/multiple_atom.xml b/templates/multiple_atom.xml
@@ -9,7 +9,7 @@ from viewutils import tag_list
 ATOM_TIME_FORMAT = str('%Y-%m-%dT%H:%M:%S+10:00')
 ?>
 
-<id>${config.ABS_BASE}/?format=atom</id>
+<id>${url}?format=atom</id>
 <title type="text">${config.BLOG_NAME}<py:if test="title"> (${title})</py:if></title>
 <link rel="self" type="application/atom+xml" href="${url}?format=atom" />
 <link rel="alternate" href="${url}" />
@@ -24,12 +24,12 @@ ATOM_TIME_FORMAT = str('%Y-%m-%dT%H:%M:%S+10:00')
 		<name>${config.BLOG_AUTHOR}</name>
 		<email py:if="config.BLOG_EMAIL">${config.BLOG_EMAIL}</email>
 	</author>
-	<category py:for="tag in entry.tags" scheme="${config.ABS_BASE}/+tags/" term="${tag}" />
+	<category py:for="tag in entry.tags" scheme="${environ['APP_URI']}/+tags/" term="${tag}" />
 	<py:if test="isinstance(entry, blog.Entry)">
-		<link rel="alternate" href="${config.ABS_BASE}/${entry.id}" />
+		<link rel="alternate" href="${environ['APP_URI']}/${entry.id}" />
 		<title type="text">${entry.title}</title>
-		<content type="xhtml" xml:base="${config.ABS_BASE}/${entry.id}"><xhtml:div>
-			<p py:if="entry.tags">Tagged: ${tag_list(entry.tags)}</p>
+		<content type="xhtml" xml:base="${environ['APP_URI']}/${entry.id}"><xhtml:div>
+			<p py:if="entry.tags">Tagged: ${tag_list(environ.get('SCRIPT_NAME', ''), entry.tags)}</p>
 			${entry.body}
 		</xhtml:div></content>
 	</py:if>
diff --git a/viewutils.py b/viewutils.py
@@ -10,7 +10,7 @@ def mini_markdown(s):
     the_p, = re.match(u'<p>(.*)\n</p>', m).groups()    
     return genshi.Markup(the_p)
 
-def tag_list(tags):
+def tag_list(script_name, tags):
     return genshi.Markup(u', ').join(
-            genshi.Markup(u'<a rel="tag" href="%s/+tags/%s">%s</a>' % (config.REL_BASE, tag, tag)) 
+            genshi.Markup(u'<a rel="tag" href="%s/+tags/%s">%s</a>' % (script_name, tag, tag)) 
             for tag in tags)