1 #! /usr/local/bin/python -O
2 # -*- coding: koi8-r -*-
4 __version__ = "$Revision$"[11:-2]
5 __revision__ = "$Id$"[5:-2]
6 __date__ = "$Date$"[7:-2]
7 __author__ = "Oleg BroytMann <phd@phd.pp.ru>"
8 __copyright__ = "Copyright (C) 2006 PhiloSoft Design"
14 import cPickle as pickle
18 from Cheetah.Template import Template
23 blog_filename = sys.argv[1]
25 blog_file = open(blog_filename, "rb")
29 old_blog = pickle.load(blog_file)
33 # blog is a dictionary mapping (year, month, day) => (filename, title, lead)
37 # Walk the directory recursively
38 for dirpath, dirs, files in os.walk(os.curdir):
40 # Ignore index.tmpl and *.html files; supose all other files are *.tmpl
41 if file == "index.tmpl" or file.endswith(".html"):
43 fullpath = os.path.join(dirpath, file)
44 template = Template(file=fullpath)
45 title_parts = template.Title.split()
46 title = ' '.join(title_parts[6:])
47 lead = getattr(template, "Lead", None)
50 key = year, month, day = tuple(dirpath.split(os.sep)[1:])
55 days.append((file, title, lead))
60 months = years[year] = {}
65 days = months[month] = []
67 if day not in days: days.append(day)
70 # Need to save the blog?
72 blog_file = open(blog_filename, "wb")
73 pickle.dump(blog, blog_file, pickle.HIGHEST_PROTOCOL)
77 # Localized month names
80 locale.setlocale(locale.LC_ALL, '')
81 from calendar import _localized_day, _localized_month
83 locale.setlocale(locale.LC_TIME, 'C')
84 months_names_en = list(_localized_month('%B'))
85 months_abbrs_en = list(_localized_month('%b'))
87 locale.setlocale(locale.LC_TIME, '')
88 months_names_ru = [month.lower() for month in _localized_month('%B')]
90 months_names_ru0 = ['', "январь", "февраль", "март", "апрель", "май", "июнь",
91 "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь"
95 def write_template(level, year, month, day, titles):
103 path.append("index.tmpl")
104 index_name = os.path.join(*path)
106 index_tmpl = open(index_name, 'r')
107 old_text = index_tmpl.read()
113 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
120 #attr $Title = "Oleg BroytMann's blog"
121 #attr $Description = "BroytMann Russian Blog Index Document"
122 #attr $Copyright = %(cyear)s
126 """ % {"cyear": year or 2005})
130 #attr $Title = "Oleg BroytMann's blog: %(year)s"
131 #attr $Description = "BroytMann Russian Blog %(year)s Index Document"
132 #attr $Copyright = %(cyear)s
135 <H1>Журнал: %(year)s</H1>
136 """ % {"year": year, "cyear": year or 2005})
141 #attr $Title = "Oleg BroytMann's blog: %(month_abbr_en)s %(year)s"
142 #attr $Description = "BroytMann Russian Blog %(month_name_en)s %(year)s Index Document"
143 #attr $Copyright = %(cyear)s
146 <H1>Журнал: %(month_name_ru0)s %(year)s</H1>
148 "year": year, "cyear": year or 2005,
149 "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
150 "month_name_ru0": months_names_ru0[imonth],
159 #attr $refresh = "0; URL=%s"
163 #attr $Title = "Oleg BroytMann's blog: %(day)d %(month_abbr_en)s %(year)s"
164 #attr $Description = "BroytMann Russian Blog %(day)d %(month_name_en)s %(year)s Index Document"
165 #attr $Copyright = %(cyear)s
168 <H1>Журнал: %(day)d %(month_name_ru0)s %(year)s</H1>
170 "year": year, "cyear": year or 2005,
171 "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
172 "month_name_ru0": months_names_ru0[imonth],
176 save_titles = titles[:]
180 for year, month, day, filename, title, lead in titles:
188 href.append(filename)
189 href = '/'.join(href)
190 if day[0] == '0': day = day[1:]
193 new_text.append('\n<h2>%s %s %s</h2>' % (day, months_names_ru[int(month)], year))
195 new_text.append('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
203 %s<a href="%s">%s</a>.
205 ''' % (lead, href, title))
209 for year, month, day, filename, title, lead in save_titles:
217 for year in sorted(years.keys()):
221 new_text.append(' - ')
222 new_text.append('<a href="%s/">%s</a>' % (year, year))
229 $phd_pp_ru.respond(self)
232 new_text = ''.join(new_text)
233 if old_text <> new_text:
234 print "Writing", index_name
235 index_tmpl = open(index_name, 'w')
236 index_tmpl.write(new_text)
241 for year in sorted(years.keys()):
244 for month in sorted(months.keys()):
246 for day in sorted(months[month]):
248 key = year, month, day
250 for filename, title, lead in blog[key]:
251 if filename.endswith(".tmpl"):
252 filename = filename[:-len("tmpl")] + "html"
253 value = (year, month, day, filename, title, lead)
254 all_titles.append(value)
255 year_titles.append(value)
256 month_titles.append(value)
257 day_titles.append(value)
258 write_template(3, year, month, day, day_titles)
259 write_template(2, year, month, day, month_titles)
260 write_template(1, year, month, day, year_titles)
261 write_template(0, year, month, day, all_titles[-20:])