]> git.llucax.com Git - software/blitiri.git/blobdiff - blitiri.cgi
Avoid code repetition by adding Article.to_vars() method
[software/blitiri.git] / blitiri.cgi
index dd873877b8607bdbff696ff5395c49a086c085b9..15c820ee9bd9333e7dc31bfb034ce7a77428fbbd 100755 (executable)
@@ -50,6 +50,9 @@ import urllib
 import cgi
 from docutils.core import publish_parts
 
+# Before importing the config, add our cwd to the Python path
+sys.path.append(os.getcwd())
+
 # Load the config file, if there is one
 try:
        from config import *
@@ -57,17 +60,22 @@ except:
        pass
 
 
+# Pimp *_path config variables to support relative paths
+data_path = os.path.realpath(data_path)
+templates_path = os.path.realpath(templates_path)
+
 # Default template
 
-default_main_header = """
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+default_main_header = """\
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
-<html>
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
 <link rel="alternate" title="%(title)s" href="%(fullurl)s/atom"
        type="application/atom+xml" />
-<link href="%(css_url)s" rel="stylesheet"
-       type="text/css" />
+<link href="%(css_url)s" rel="stylesheet" type="text/css" />
 <title>%(title)s</title>
 </head>
 
@@ -79,12 +87,12 @@ default_main_header = """
 """
 
 default_main_footer = """
-</div><p/>
-<hr/><br/>
+</div>
 <div class="footer">
   %(showyear)s: %(monthlinks)s<br/>
   years: %(yearlinks)s<br/>
   subscribe: <a href="%(url)s/atom">atom</a><br/>
+  views: <a href="%(url)s/">blog</a> <a href="%(url)s/list">list</a><br/>
 </div>
 
 </body>
@@ -123,16 +131,17 @@ default_css = """
 body {
        font-family: sans-serif;
        font-size: small;
+       width: 52em;
 }
 
 div.content {
-       width: 50%;
+       width: 96%;
 }
 
 h1 {
        font-size: large;
        border-bottom: 2px solid #99F;
-       width: 60%;
+       width: 100%;
        margin-bottom: 1em;
 }
 
@@ -170,15 +179,11 @@ div.article {
        margin-bottom: 2em;
 }
 
-hr {
-       float: left;
-       height: 2px;
-       border: 0;
-       background-color: #99F;
-       width: 60%;
-}
-
 div.footer {
+       margin-top: 1em;
+       padding-top: 0.4em;
+       width: 100%;
+       border-top: 2px solid #99F;
        font-size: x-small;
 }
 
@@ -244,29 +249,7 @@ class Templates (object):
 
        def get_article_header(self, article):
                avars = self.vars.copy()
-               avars.update( {
-                       'arttitle': article.title,
-                       'author': article.author,
-                       'date': article.created.isoformat(' '),
-                       'uuid': article.uuid,
-                       'created': article.created.isoformat(' '),
-                       'updated': article.updated.isoformat(' '),
-                       'tags': article.get_tags_links(),
-
-                       'cyear': article.created.year,
-                       'cmonth': article.created.month,
-                       'cday': article.created.day,
-                       'chour': article.created.hour,
-                       'cminute': article.created.minute,
-                       'csecond': article.created.second,
-
-                       'uyear': article.updated.year,
-                       'umonth': article.updated.month,
-                       'uday': article.updated.day,
-                       'uhour': article.updated.hour,
-                       'uminute': article.updated.minute,
-                       'usecond': article.updated.second,
-               } )
+               avars.update(article.to_vars())
 
                p = self.tpath + '/art_header.html'
                if os.path.isfile(p):
@@ -275,29 +258,7 @@ class Templates (object):
 
        def get_article_footer(self, article):
                avars = self.vars.copy()
-               avars.update( {
-                       'arttitle': article.title,
-                       'author': article.author,
-                       'date': article.created.isoformat(' '),
-                       'uuid': article.uuid,
-                       'created': article.created.isoformat(' '),
-                       'updated': article.updated.isoformat(' '),
-                       'tags': article.get_tags_links(),
-
-                       'cyear': article.created.year,
-                       'cmonth': article.created.month,
-                       'cday': article.created.day,
-                       'chour': article.created.hour,
-                       'cminute': article.created.minute,
-                       'csecond': article.created.second,
-
-                       'uyear': article.updated.year,
-                       'umonth': article.updated.month,
-                       'uday': article.updated.day,
-                       'uhour': article.updated.hour,
-                       'uminute': article.updated.minute,
-                       'usecond': article.updated.second,
-               } )
+               avars.update(article.to_vars())
 
                p = self.tpath + '/art_footer.html'
                if os.path.isfile(p):
@@ -357,6 +318,10 @@ class Article (object):
                        return -1
                return 1
 
+       def title_cmp(self, other):
+               return cmp(self.title, other.title)
+
+
        def load(self):
                try:
                        raw = open(data_path + '/' + self.path).readlines()
@@ -398,6 +363,33 @@ class Article (object):
                                writer_name = "html")
                return parts['body'].encode('utf8')
 
+       def to_vars(self):
+               return {
+                       'arttitle': self.title,
+                       'author': self.author,
+                       'date': self.created.isoformat(' '),
+                       'uuid': self.uuid,
+                       'tags': self.get_tags_links(),
+
+                       'created': self.created.isoformat(' '),
+                       'ciso': self.created.isoformat(),
+                       'cyear': self.created.year,
+                       'cmonth': self.created.month,
+                       'cday': self.created.day,
+                       'chour': self.created.hour,
+                       'cminute': self.created.minute,
+                       'csecond': self.created.second,
+
+                       'updated': self.updated.isoformat(' '),
+                       'uiso': self.updated.isoformat(),
+                       'uyear': self.updated.year,
+                       'umonth': self.updated.month,
+                       'uday': self.updated.day,
+                       'uhour': self.updated.hour,
+                       'uminute': self.updated.minute,
+                       'usecond': self.updated.second,
+               }
+
        def get_tags_links(self):
                l = []
                tags = list(self.tags)
@@ -503,6 +495,20 @@ def render_html(articles, db, actyear = None):
                print template.get_article_footer(a)
        print template.get_main_footer()
 
+def render_artlist(articles, db, actyear = None):
+       template = Templates(templates_path, db, actyear)
+       print 'Content-type: text/html; charset=utf-8\n'
+       print template.get_main_header()
+       print '<h2>Articles</h2>'
+       for a in articles:
+               print '<li><a href="%(url)s/uuid/%(uuid)s">%(title)s</a></li>' \
+                       % {     'url': blog_url,
+                               'uuid': a.uuid,
+                               'title': a.title,
+                               'author': a.author,
+                       }
+       print template.get_main_footer()
+
 def render_atom(articles):
        if len(articles) > 0:
                updated = articles[0].updated.isoformat()
@@ -526,6 +532,11 @@ def render_atom(articles):
        }
 
        for a in articles:
+               vars = a.to_vars()
+               vars.update( {
+                       'url': full_url,
+                       'contents': a.to_html(),
+               } )
                print """
   <entry>
     <title>%(arttitle)s</title>
@@ -533,29 +544,20 @@ def render_atom(articles):
     <link href="%(url)s/post/%(uuid)s" />
     <id>%(url)s/post/%(uuid)s</id>
     <summary>%(arttitle)s</summary>
-    <published>%(created)sZ</published>
-    <updated>%(updated)sZ</updated>
+    <published>%(ciso)sZ</published>
+    <updated>%(uiso)sZ</updated>
     <content type="xhtml">
       <div xmlns="http://www.w3.org/1999/xhtml"><p>
 %(contents)s
       </p></div>
     </content>
   </entry>
-               """ % {
-                       'arttitle': a.title,
-                       'author': a.author,
-                       'uuid': a.uuid,
-                       'url': full_url,
-                       'created': a.created.isoformat(),
-                       'updated': a.updated.isoformat(),
-                       'contents': a.to_html(),
-               }
-
+               """ % vars
        print "</feed>"
 
 
 def render_style():
-       print 'Content-type: text/plain\n'
+       print 'Content-type: text/css\r\n\r\n',
        print default_css
 
 def handle_cgi():
@@ -570,6 +572,7 @@ def handle_cgi():
        atom = False
        style = False
        post = False
+       artlist = False
 
        if os.environ.has_key('PATH_INFO'):
                path_info = os.environ['PATH_INFO']
@@ -577,7 +580,9 @@ def handle_cgi():
                atom = path_info == '/atom'
                tag = path_info.startswith('/tag/')
                post = path_info.startswith('/post/')
-               if not style and not atom and not post and not tag:
+               artlist = path_info.startswith('/list')
+               if not style and not atom and not post and not tag \
+                               and not artlist:
                        date = path_info.split('/')[1:]
                        try:
                                if len(date) > 1 and date[0]:
@@ -605,7 +610,11 @@ def handle_cgi():
        elif style:
                render_style()
        elif post:
-               render_html( [db.get_article(uuid)], year )
+               render_html( [db.get_article(uuid)], db, year )
+       elif artlist:
+               articles = db.get_articles()
+               articles.sort(cmp = Article.title_cmp)
+               render_artlist(articles, db)
        else:
                articles = db.get_articles(year, month, day, tags)
                articles.sort(reverse = True)