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"
15 import cPickle as pickle
19 from Cheetah.Template import Template
24 blog_filename = sys.argv[1]
26 blog_file = open(blog_filename, "rb")
30 old_blog = pickle.load(blog_file)
34 # blog is a dictionary mapping (year, month, day) => (filename, title, lead)
38 # Walk the directory recursively
39 for dirpath, dirs, files in os.walk(os.curdir):
41 # Ignore index.tmpl and *.html files; supose all other files are *.tmpl
42 if file == "index.tmpl" or file.endswith(".html"):
44 fullpath = os.path.join(dirpath, file)
45 template = Template(file=fullpath)
46 title_parts = template.Title.split()
47 title = ' '.join(title_parts[6:])
48 lead = getattr(template, "Lead", None)
51 year, month, day = dirpath.split(os.sep)[1:]
52 blog[year, month, day] = (os.path.basename(file), title, lead)
57 months = years[year] = {}
62 days = months[month] = []
67 # Need to save the blog?
69 blog_file = open(blog_filename, "wb")
70 pickle.dump(blog, blog_file, pickle.HIGHEST_PROTOCOL)
74 # Localized month names
77 locale.setlocale(locale.LC_ALL, '')
78 from calendar import _localized_day, _localized_month
80 locale.setlocale(locale.LC_TIME, 'C')
81 months_names_en = list(_localized_month('%B'))
82 months_abbrs_en = list(_localized_month('%b'))
84 locale.setlocale(locale.LC_TIME, '')
85 months_names_ru = [month.lower() for month in _localized_month('%B')]
87 months_names_ru0 = ['', "январь", "февраль", "март", "апрель", "май", "июнь",
88 "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь"
92 def write_template(year, month, day, titles):
93 index_name = os.path.join(year, month, day, "index.tmpl")
95 index_tmpl = open(index_name, 'r')
96 old_text = index_tmpl.read()
103 show_month = not month
107 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
114 #attr $Title = "Oleg BroytMann's blog"
115 #attr $Description = "BroytMann Russian Blog Index Document"
116 #attr $Copyright = %(cyear)s
120 """ % {"cyear": year or 2005})
125 #attr $Title = "Oleg BroytMann's blog: %(year)s"
126 #attr $Description = "BroytMann Russian Blog %(year)s Index Document"
127 #attr $Copyright = %(cyear)s
130 <H1>Журнал: %(year)s</H1>
131 """ % {"year": year, "cyear": year or 2005})
137 #attr $Title = "Oleg BroytMann's blog: %(month_abbr_en)s %(year)s"
138 #attr $Description = "BroytMann Russian Blog %(month_name_en)s %(year)s Index Document"
139 #attr $Copyright = %(cyear)s
142 <H1>Журнал: %(month_name_ru0)s %(year)s</H1>
144 "year": year, "cyear": year or 2005,
145 "month_abbr_en": months_abbrs_en[month], "month_name_en": months_names_en[month],
146 "month_name_ru0": months_names_ru0[month],
155 #attr $Title = "Oleg BroytMann's blog: %(day)d %(month_abbr_en)s %(year)s"
156 #attr $Description = "BroytMann Russian Blog %(day)d %(month_name_en)s %(year)s Index Document"
157 #attr $Copyright = %(cyear)s
160 <H1>Журнал: %(day)d %(month_name_ru0)s %(year)s</H1>
162 "year": year, "cyear": year or 2005,
163 "month_abbr_en": months_abbrs_en[month], "month_name_en": months_names_en[month],
164 "month_name_ru0": months_names_ru0[month],
168 save_titles = titles[:]
172 for key, tmpl, title, lead in titles:
173 year, month, day = key
182 href = '/'.join(href)
183 if day[0] == '0': day = day[1:]
186 new_text.append('\n<h2>%s %s %s</h2>' % (day, months_names_ru[int(month)], year))
188 new_text.append('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
196 %s<a href="%s">%s</a>.
198 ''' % (lead, href, title))
202 for key, tmpl, title, lead in save_titles:
203 year, month, day = key
211 for year in sorted(years.keys()):
215 new_text.append(' - ')
216 new_text.append('<a href="%s/">%s</a>' % (year, year))
223 $phd_pp_ru.respond(self)
226 new_text = ''.join(new_text)
227 if old_text <> new_text:
228 print "Writing", index_name
229 index_tmpl = open(index_name, 'w')
230 index_tmpl.write(new_text)
235 for year in sorted(years.keys()):
238 for month in sorted(months.keys()):
240 for day in months[month]:
242 key = year, month, day
244 tmpl, title, lead = blog[key]
245 if tmpl.endswith(".tmpl"): tmpl = tmpl[:-len("tmpl")] + "html"
246 all_titles.append((key, tmpl, title, lead))
247 year_titles.append((key, tmpl, title, lead))
248 month_titles.append((key, tmpl, title, lead))
249 day_titles.append((key, tmpl, title, lead))
250 write_template(year, month, day, day_titles)
251 write_template(year, month, '', month_titles)
252 write_template(year, '', '', year_titles)
253 write_template('', '', '', all_titles[-20:])