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 (file, title, lead, tags)]
39 # Walk the directory recursively
40 for dirpath, dirs, files in os.walk(os.curdir):
41 d = os.path.basename(dirpath)
42 if not d.startswith("20") and not d.isdigit():
45 # Ignore index.tmpl and *.html files; supose all other files are *.tmpl
46 if file == "index.tmpl" or file.endswith(".html"):
48 fullpath = os.path.join(dirpath, file)
49 template = Template(file=fullpath)
50 title_parts = template.Title.split()
51 title = ' '.join(title_parts[6:])
52 lead = getattr(template, "Lead", None)
54 tags = getattr(template, "Tag", None)
55 if isinstance(tags, basestring):
59 key = year, month, day = tuple(dirpath.split(os.sep)[1:])
64 days.append((file, title, lead, tags))
69 months = years[year] = {}
74 days = months[month] = []
76 if day not in days: days.append(day)
79 # Need to save the blog?
81 blog_file = open(blog_filename, "wb")
82 pickle.dump(blog, blog_file, pickle.HIGHEST_PROTOCOL)
86 # Localized month names
89 locale.setlocale(locale.LC_ALL, '')
90 from calendar import _localized_day, _localized_month
92 locale.setlocale(locale.LC_TIME, 'C')
93 months_names_en = list(_localized_month('%B'))
94 months_abbrs_en = list(_localized_month('%b'))
96 locale.setlocale(locale.LC_TIME, '')
97 months_names_ru = [month.lower() for month in _localized_month('%B')]
99 months_names_ru0 = ['', "январь", "февраль", "март", "апрель", "май", "июнь",
100 "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь"
104 def write_if_changed(filename, new_text):
106 infile = open(filename, 'r')
107 old_text = infile.read()
112 if old_text <> new_text:
113 print "Writing", filename
114 outfile = open(filename, 'w')
115 outfile.write(new_text)
119 def write_template(level, year, month, day, titles):
127 path.append("index.tmpl")
128 index_name = os.path.join(*path)
131 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
138 #attr $Title = "Oleg BroytMann's blog"
139 #attr $Description = "BroytMann Russian Blog Index Document"
140 #attr $Copyright = %(cyear)s
144 """ % {"cyear": year or 2005})
148 #attr $Title = "Oleg BroytMann's blog: %(year)s"
149 #attr $Description = "BroytMann Russian Blog %(year)s Index Document"
150 #attr $Copyright = %(cyear)s
153 <H1>Журнал: %(year)s</H1>
154 """ % {"year": year, "cyear": year or 2005})
159 #attr $Title = "Oleg BroytMann's blog: %(month_abbr_en)s %(year)s"
160 #attr $Description = "BroytMann Russian Blog %(month_name_en)s %(year)s Index Document"
161 #attr $Copyright = %(cyear)s
164 <H1>Журнал: %(month_name_ru0)s %(year)s</H1>
166 "year": year, "cyear": year or 2005,
167 "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
168 "month_name_ru0": months_names_ru0[imonth],
182 #attr $refresh = "0; URL=%s"
186 #attr $Title = "Oleg BroytMann's blog: %(day)d %(month_abbr_en)s %(year)s"
187 #attr $Description = "BroytMann Russian Blog %(day)d %(month_name_en)s %(year)s Index Document"
188 #attr $Copyright = %(cyear)s
191 <H1>Журнал: %(day)d %(month_name_ru0)s %(year)s</H1>
193 "year": year, "cyear": year or 2005,
194 "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
195 "month_name_ru0": months_names_ru0[imonth],
199 save_titles = titles[:]
203 for year, month, day, file, title, lead in titles:
212 href = '/'.join(href)
213 if day[0] == '0': day = day[1:]
216 new_text.append('\n<h2>%s %s %s</h2>' % (day, months_names_ru[int(month)], year))
218 new_text.append('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
226 %s<a href="%s">%s</a>.
228 ''' % (lead, href, title))
232 for year, month, day, file, title, lead in save_titles:
240 for year in sorted(years.keys()):
244 new_text.append(' - ')
245 new_text.append('<a href="%s/">%s</a>' % (year, year))
252 $phd_pp_ru.respond(self)
255 write_if_changed(index_name, ''.join(new_text))
261 for year in sorted(years.keys()):
264 for month in sorted(months.keys()):
266 for day in sorted(months[month]):
268 key = year, month, day
270 for file, title, lead, tags in blog[key]:
271 if file.endswith(".tmpl"): file = file[:-len("tmpl")] + "html"
272 value = (year, month, day, file, title, lead)
273 all_titles.append(value)
274 year_titles.append(value)
275 month_titles.append(value)
276 day_titles.append(value)
279 tag_links = all_tags[tag]
281 tag_links = all_tags[tag] = []
282 tag_links.append('/'.join(("..", year, month, day, file)))
283 write_template(3, year, month, day, day_titles)
284 write_template(2, year, month, day, month_titles)
285 write_template(1, year, month, day, year_titles)
286 write_template(0, year, month, day, all_titles[-20:])
288 all_tags = [(len(links), tag, links) for (tag, links) in all_tags.items()]
292 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
295 #attr $Title = "Oleg BroytMann's blog: tags"
296 #attr $Description = "BroytMann Russian Blog Tags Index Document"
297 #attr $Copyright = 2006
303 for count, tag, links in all_tags:
305 <p class="head"><a href="%s.html">%s (%d)</a></p>
306 """ % (tag, tag, count))
309 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
312 #attr $Title = "Oleg BroytMann's blog: tag %s"
313 #attr $Description = "BroytMann Russian Blog Tag %s Index Document"
314 #attr $Copyright = 2006
318 """ % (tag, tag, tag)]
321 junk, year, month, day, filename = link.split('/')
323 <p class="head"><a href="%s">%s/%s/%s: %s</a></p>
324 """ % (link, year, month, day, filename))
328 $phd_pp_ru.respond(self)
330 write_if_changed(os.path.join("tags", tag+".tmpl"), ''.join(tag_text))
334 $phd_pp_ru.respond(self)
336 write_if_changed(os.path.join("tags", "index.tmpl"), ''.join(new_text))