]> git.llucax.com Git - software/blitiri.git/blobdiff - blitiri.cgi
Avoid code repetition by adding a helper function Template.get_template()
[software/blitiri.git] / blitiri.cgi
index f78ee0bbe8c3e7feb765eb2ab2fb29a7a2e5a64e..0992fd2f1f7a25ec14213b71dfcadc28415cb9fd 100755 (executable)
@@ -235,79 +235,31 @@ class Templates (object):
                        'yearlinks': ' '.join(db.get_year_links()),
                }
 
-       def get_main_header(self):
-               p = self.tpath + '/header.html'
+       def get_template(self, page_name, default_template, extra_vars = None):
+               if extra_vars is None:
+                       vars = self.vars
+               else:
+                       vars = self.vars.copy()
+                       vars.update(extra_vars)
+
+               p = '%s/%s.html' % (self.tpath, page_name)
                if os.path.isfile(p):
-                       return open(p).read() % self.vars
-               return default_main_header % self.vars
+                       return open(p).read() % vars
+               return default_template % vars
+
+       def get_main_header(self):
+               return self.get_template('header', default_main_header)
 
        def get_main_footer(self):
-               p = self.tpath + '/footer.html'
-               if os.path.isfile(p):
-                       return open(p).read() % self.vars
-               return default_main_footer % self.vars
+               return self.get_template('footer', default_main_footer)
 
        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,
-               } )
-
-               p = self.tpath + '/art_header.html'
-               if os.path.isfile(p):
-                       return open(p).read() % avars
-               return default_article_header % avars
+               return self.get_template(
+                       'art_header', default_article_header, article.to_vars())
 
        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,
-               } )
-
-               p = self.tpath + '/art_footer.html'
-               if os.path.isfile(p):
-                       return open(p).read() % avars
-               return default_article_footer % avars
+               return self.get_template(
+                       'art_footer', default_article_footer, article.to_vars())
 
 
 class Article (object):
@@ -407,6 +359,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)
@@ -549,6 +528,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>
@@ -556,24 +540,15 @@ 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>"