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
34 # (year, month, day) => [list of (filename, title, lead)]
39 # Walk the directory recursively
40 for dirpath, dirs, files in os.walk(os.curdir):
42 # Ignore index.tmpl and *.html files; supose all other files are *.tmpl
43 if file == "index.tmpl" or file.endswith(".html"):
45 fullpath = os.path.join(dirpath, file)
46 template = Template(file=fullpath)
47 title_parts = template.Title.split()
48 title = ' '.join(title_parts[6:])
49 lead = getattr(template, "Lead", None)
52 key = year, month, day = tuple(dirpath.split(os.sep)[1:])
57 days.append((file, title, lead))
62 months = years[year] = {}
67 days = months[month] = []
69 if day not in days: days.append(day)
72 # Need to save the blog?
74 blog_file = open(blog_filename, "wb")
75 pickle.dump(blog, blog_file, pickle.HIGHEST_PROTOCOL)
79 # Localized month names
82 locale.setlocale(locale.LC_ALL, '')
83 from calendar import _localized_day, _localized_month
85 locale.setlocale(locale.LC_TIME, 'C')
86 months_names_en = list(_localized_month('%B'))
87 months_abbrs_en = list(_localized_month('%b'))
89 locale.setlocale(locale.LC_TIME, '')
90 months_names_ru = [month.lower() for month in _localized_month('%B')]
92 months_names_ru0 = ['', "январь", "февраль", "март", "апрель", "май", "июнь",
93 "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь"
97 def write_template(level, year, month, day, titles):
105 path.append("index.tmpl")
106 index_name = os.path.join(*path)
108 index_tmpl = open(index_name, 'r')
109 old_text = index_tmpl.read()
115 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
122 #attr $Title = "Oleg BroytMann's blog"
123 #attr $Description = "BroytMann Russian Blog Index Document"
124 #attr $Copyright = %(cyear)s
128 """ % {"cyear": year or 2005})
132 #attr $Title = "Oleg BroytMann's blog: %(year)s"
133 #attr $Description = "BroytMann Russian Blog %(year)s Index Document"
134 #attr $Copyright = %(cyear)s
137 <H1>Журнал: %(year)s</H1>
138 """ % {"year": year, "cyear": year or 2005})
143 #attr $Title = "Oleg BroytMann's blog: %(month_abbr_en)s %(year)s"
144 #attr $Description = "BroytMann Russian Blog %(month_name_en)s %(year)s Index Document"
145 #attr $Copyright = %(cyear)s
148 <H1>Журнал: %(month_name_ru0)s %(year)s</H1>
150 "year": year, "cyear": year or 2005,
151 "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
152 "month_name_ru0": months_names_ru0[imonth],
166 #attr $refresh = "0; URL=%s"
170 #attr $Title = "Oleg BroytMann's blog: %(day)d %(month_abbr_en)s %(year)s"
171 #attr $Description = "BroytMann Russian Blog %(day)d %(month_name_en)s %(year)s Index Document"
172 #attr $Copyright = %(cyear)s
175 <H1>Журнал: %(day)d %(month_name_ru0)s %(year)s</H1>
177 "year": year, "cyear": year or 2005,
178 "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
179 "month_name_ru0": months_names_ru0[imonth],
183 save_titles = titles[:]
187 for year, month, day, filename, title, lead in titles:
195 href.append(filename)
196 href = '/'.join(href)
197 if day[0] == '0': day = day[1:]
200 new_text.append('\n<h2>%s %s %s</h2>' % (day, months_names_ru[int(month)], year))
202 new_text.append('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
210 %s<a href="%s">%s</a>.
212 ''' % (lead, href, title))
216 for year, month, day, filename, title, lead in save_titles:
224 for year in sorted(years.keys()):
228 new_text.append(' - ')
229 new_text.append('<a href="%s/">%s</a>' % (year, year))
236 $phd_pp_ru.respond(self)
239 new_text = ''.join(new_text)
240 if old_text <> new_text:
241 print "Writing", index_name
242 index_tmpl = open(index_name, 'w')
243 index_tmpl.write(new_text)
248 for year in sorted(years.keys()):
251 for month in sorted(months.keys()):
253 for day in sorted(months[month]):
255 key = year, month, day
257 for filename, title, lead in blog[key]:
258 if filename.endswith(".tmpl"):
259 filename = filename[:-len("tmpl")] + "html"
260 value = (year, month, day, filename, title, lead)
261 all_titles.append(value)
262 year_titles.append(value)
263 month_titles.append(value)
264 day_titles.append(value)
265 write_template(3, year, month, day, day_titles)
266 write_template(2, year, month, day, month_titles)
267 write_template(1, year, month, day, year_titles)
268 write_template(0, year, month, day, all_titles[-20:])