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(year, month, day, titles):
96 index_name = os.path.join(year, month, day, "index.tmpl")
98 index_tmpl = open(index_name, 'r')
99 old_text = index_tmpl.read()
106 show_month = not month
110 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
117 #attr $Title = "Oleg BroytMann's blog"
118 #attr $Description = "BroytMann Russian Blog Index Document"
119 #attr $Copyright = %(cyear)s
123 """ % {"cyear": year or 2005})
128 #attr $Title = "Oleg BroytMann's blog: %(year)s"
129 #attr $Description = "BroytMann Russian Blog %(year)s Index Document"
130 #attr $Copyright = %(cyear)s
133 <H1>Журнал: %(year)s</H1>
134 """ % {"year": year, "cyear": year or 2005})
140 #attr $Title = "Oleg BroytMann's blog: %(month_abbr_en)s %(year)s"
141 #attr $Description = "BroytMann Russian Blog %(month_name_en)s %(year)s Index Document"
142 #attr $Copyright = %(cyear)s
145 <H1>Журнал: %(month_name_ru0)s %(year)s</H1>
147 "year": year, "cyear": year or 2005,
148 "month_abbr_en": months_abbrs_en[month], "month_name_en": months_names_en[month],
149 "month_name_ru0": months_names_ru0[month],
158 #attr $Title = "Oleg BroytMann's blog: %(day)d %(month_abbr_en)s %(year)s"
159 #attr $Description = "BroytMann Russian Blog %(day)d %(month_name_en)s %(year)s Index Document"
160 #attr $Copyright = %(cyear)s
163 <H1>Журнал: %(day)d %(month_name_ru0)s %(year)s</H1>
165 "year": year, "cyear": year or 2005,
166 "month_abbr_en": months_abbrs_en[month], "month_name_en": months_names_en[month],
167 "month_name_ru0": months_names_ru0[month],
171 save_titles = titles[:]
175 for key, tmpl, title, lead in titles:
176 year, month, day = key
185 href = '/'.join(href)
186 if day[0] == '0': day = day[1:]
189 new_text.append('\n<h2>%s %s %s</h2>' % (day, months_names_ru[int(month)], year))
191 new_text.append('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
199 %s<a href="%s">%s</a>.
201 ''' % (lead, href, title))
205 for key, tmpl, title, lead in save_titles:
206 year, month, day = key
214 for year in sorted(years.keys()):
218 new_text.append(' - ')
219 new_text.append('<a href="%s/">%s</a>' % (year, year))
226 $phd_pp_ru.respond(self)
229 new_text = ''.join(new_text)
230 if old_text <> new_text:
231 print "Writing", index_name
232 index_tmpl = open(index_name, 'w')
233 index_tmpl.write(new_text)
238 for year in sorted(years.keys()):
241 for month in sorted(months.keys()):
243 for day in sorted(months[month]):
245 key = year, month, day
247 for tmpl, title, lead in blog[key]:
248 if tmpl.endswith(".tmpl"): tmpl = tmpl[:-len("tmpl")] + "html"
249 all_titles.append((key, tmpl, title, lead))
250 year_titles.append((key, tmpl, title, lead))
251 month_titles.append((key, tmpl, title, lead))
252 day_titles.append((key, tmpl, title, lead))
253 write_template(year, month, day, day_titles)
254 write_template(year, month, '', month_titles)
255 write_template(year, '', '', year_titles)
256 write_template('', '', '', all_titles[-20:])