]> git.phdru.name Git - phdru.name/phdru.name.git/commitdiff
Added keywords. Made write_template() to test if the template was changed.
authorOleg Broytman <phd@phdru.name>
Sat, 25 Feb 2006 13:06:00 +0000 (13:06 +0000)
committerOleg Broytman <phd@phdru.name>
Sat, 25 Feb 2006 13:06:00 +0000 (13:06 +0000)
git-svn-id: file:///home/phd/archive/SVN/phdru.name/scripts@3 7bb0bf08-9e0d-0410-b083-99cee3bf18b8

reindex_blog.py

index be27835e9a9140c3988bc4a39009bd816a5a644d..43c4f8b4342389643585f4958d655b8f718e4798 100755 (executable)
@@ -1,6 +1,12 @@
 #! /usr/local/bin/python -O
 # -*- coding: koi8-r -*-
 
+__version__ = "$Revision$"[11:-2]
+__revision__ = "$Id$"[5:-2]
+__date__ = "$Date$"[7:-2]
+__author__ = "Oleg BroytMann <phd@phd.pp.ru>"
+__copyright__ = "Copyright (C) 2006 PhiloSoft Design"
+
 import sys, os
 from glob import glob
 
@@ -77,10 +83,21 @@ months_names_ru0 = ['', "
 ]
 
 
-def write_template(show_year, show_month, year, month, titles, cut=None, show_years=False):
-   index_tmpl = open(os.path.join(year, month, "index.tmpl"), 'w')
+def write_template(year, month, titles):
+   index_name = os.path.join(year, month, "index.tmpl")
+   try:
+      index_tmpl = open(index_name, 'r')
+      old_text = index_tmpl.read()
+      index_tmpl.close()
+   except IOError:
+      old_text = None
+
+   new_text = []
+   show_year = not year
+   show_month = not month
+
    if show_year:
-      index_tmpl.write("""\
+      new_text.append("""\
 #extends phd_pp_ru
 #implements respond
 #attr $Title = "Oleg BroytMann's blog"
@@ -93,7 +110,7 @@ def write_template(show_year, show_month, year, month, titles, cut=None, show_ye
 
    elif show_month:
 
-      index_tmpl.write("""\
+      new_text.append("""\
 #extends phd_pp_ru
 #implements respond
 #attr $Title = "Oleg BroytMann's blog: %(year)s"
@@ -107,7 +124,7 @@ def write_template(show_year, show_month, year, month, titles, cut=None, show_ye
    else:
 
       month = int(month)
-      index_tmpl.write("""\
+      new_text.append("""\
 #extends phd_pp_ru
 #implements respond
 #attr $Title = "Oleg BroytMann's blog: %(month_abbr_en)s %(year)s"
@@ -124,8 +141,6 @@ def write_template(show_year, show_month, year, month, titles, cut=None, show_ye
 
    save_titles = titles[:]
    titles.reverse()
-   if cut:
-      titles = titles[:cut]
 
    save_day = None
    for key, tmpl, title, lead in titles:
@@ -141,27 +156,27 @@ def write_template(show_year, show_month, year, month, titles, cut=None, show_ye
       if day[0] == '0': day = day[1:]
       if save_day <> day:
          if show_year:
-            index_tmpl.write('\n<h2>%s %s %s</h2>' % (day, months_names_ru[int(month)], year))
+            new_text.append('\n<h2>%s %s %s</h2>' % (day, months_names_ru[int(month)], year))
          else:
-            index_tmpl.write('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
+            new_text.append('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
          save_day = day
       if lead:
          lead = lead + ' '
       else:
          lead = ''
-      index_tmpl.write('''
+      new_text.append('''
 <p class="head">
    %s<a href="%s">%s</a>.
 </p>
 ''' % (lead, href, title))
 
-   if show_years:
+   if show_year:
       years = {}
       for key, tmpl, title, lead in save_titles:
          year, month, day = key
          years[year] = True
       first_year = True
-      index_tmpl.write('''
+      new_text.append('''
 <hr>
 
 <p class="years">
@@ -170,17 +185,23 @@ def write_template(show_year, show_month, year, month, titles, cut=None, show_ye
          if first_year:
             first_year = False
          else:
-            index_tmpl.write(' - ')
-         index_tmpl.write('<a href="%s/">%s</a>' % (year, year))
-      index_tmpl.write('''
+            new_text.append(' - ')
+         new_text.append('<a href="%s/">%s</a>' % (year, year))
+      new_text.append('''
 </p>
 ''')
 
-   index_tmpl.write("""\
+   new_text.append("""\
 #end def
 $phd_pp_ru.respond(self)
 """)
-   index_tmpl.close()
+
+   new_text = ''.join(new_text)
+   if old_text <> new_text:
+      print "Writing", index_name
+      index_tmpl = open(index_name, 'w')
+      index_tmpl.write(new_test)
+      index_tmpl.close()
 
 
 def translate(tmpl):
@@ -193,6 +214,7 @@ all_titles = []
 for key in sorted(blog.keys()):
    title, tmpl, lead = blog[key]
    all_titles.append((key, translate(tmpl), title, lead))
+all_titles = all_titles[-20:]
 
 for year in sorted(years.keys()):
    year_titles = []
@@ -207,6 +229,6 @@ for year in sorted(years.keys()):
             tmpl = translate(tmpl)
             year_titles.append((key, tmpl, title, lead))
             month_titles.append((key, tmpl, title, lead))
-      write_template(False, False, year, month, month_titles)
-   write_template(False, True, year, '', year_titles)
-write_template(True, True, '', '', all_titles, 5, True)
+      write_template(year, month, month_titles)
+   write_template(year, '', year_titles)
+write_template('', '', all_titles)