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
20 blog_filename = sys.argv[1]
22 blog_file = open(blog_filename, "rb")
26 blog = pickle.load(blog_file)
34 for year in os.listdir(os.curdir):
37 for month in os.listdir(year):
38 m = os.path.join(year, month)
44 year, m = month.split(os.sep)
47 years[year][m] = days_of_month = []
48 for day in os.listdir(month):
49 d = os.path.join(month, day)
52 days_of_month.append(d)
55 for tmpl in glob(os.path.join(day, "*.tmpl")):
56 template = Template(file=tmpl)
57 title_parts = template.Title.split()
58 title = ' '.join(title_parts[6:])
59 lead = getattr(template, "Lead", None)
62 day_parts = day.split(os.sep)
63 blog[tuple(day_parts)] = (title, os.path.basename(tmpl), lead)
65 blog_file = open(blog_filename, "wb")
66 pickle.dump(blog, blog_file, pickle.HIGHEST_PROTOCOL)
71 locale.setlocale(locale.LC_ALL, '')
72 from calendar import _localized_day, _localized_month
74 locale.setlocale(locale.LC_TIME, 'C')
75 months_names_en = list(_localized_month('%B'))
76 months_abbrs_en = list(_localized_month('%b'))
78 locale.setlocale(locale.LC_TIME, '')
79 months_names_ru = [month.lower() for month in _localized_month('%B')]
81 months_names_ru0 = ['', "январь", "февраль", "март", "апрель", "май", "июнь",
82 "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь"
86 def write_template(year, month, titles):
87 index_name = os.path.join(year, month, "index.tmpl")
89 index_tmpl = open(index_name, 'r')
90 old_text = index_tmpl.read()
97 show_month = not month
103 #attr $Title = "Oleg BroytMann's blog"
104 #attr $Description = "BroytMann Russian Blog Index Document"
105 #attr $Copyright = %(cyear)s
109 """ % {"cyear": year or 2005})
116 #attr $Title = "Oleg BroytMann's blog: %(year)s"
117 #attr $Description = "BroytMann Russian Blog %(year)s Index Document"
118 #attr $Copyright = %(cyear)s
121 <H1>Журнал: %(year)s</H1>
122 """ % {"year": year, "cyear": year or 2005})
130 #attr $Title = "Oleg BroytMann's blog: %(month_abbr_en)s %(year)s"
131 #attr $Description = "BroytMann Russian Blog %(month_name_en)s %(year)s Index Document"
132 #attr $Copyright = %(cyear)s
135 <H1>Журнал: %(month_name_ru0)s %(year)s</H1>
137 "year": year, "cyear": year or 2005,
138 "month_abbr_en": months_abbrs_en[month], "month_name_en": months_names_en[month],
139 "month_name_ru0": months_names_ru0[month],
142 save_titles = titles[:]
146 for key, tmpl, title, lead in titles:
147 year, month, day = key
155 href = '/'.join(href)
156 if day[0] == '0': day = day[1:]
159 new_text.append('\n<h2>%s %s %s</h2>' % (day, months_names_ru[int(month)], year))
161 new_text.append('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
169 %s<a href="%s">%s</a>.
171 ''' % (lead, href, title))
175 for key, tmpl, title, lead in save_titles:
176 year, month, day = key
184 for year in sorted(years.keys()):
188 new_text.append(' - ')
189 new_text.append('<a href="%s/">%s</a>' % (year, year))
196 $phd_pp_ru.respond(self)
199 new_text = ''.join(new_text)
200 if old_text <> new_text:
201 print "Writing", index_name
202 index_tmpl = open(index_name, 'w')
203 index_tmpl.write(new_text)
208 if tmpl == "index.tmpl": tmpl = ''
209 if tmpl.endswith(".tmpl"): tmpl = tmpl[:-len("tmpl")] + "html"
214 for key in sorted(blog.keys()):
215 title, tmpl, lead = blog[key]
216 all_titles.append((key, translate(tmpl), title, lead))
217 all_titles = all_titles[-20:]
219 for year in sorted(years.keys()):
222 for month in sorted(months.keys()):
224 for day in sorted(months[month]):
225 day_parts = day.split(os.sep)
226 key = tuple(day_parts)
228 title, tmpl, lead = blog[key]
229 tmpl = translate(tmpl)
230 year_titles.append((key, tmpl, title, lead))
231 month_titles.append((key, tmpl, title, lead))
232 write_template(year, month, month_titles)
233 write_template(year, '', year_titles)
234 write_template('', '', all_titles)