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, tags=None):
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:
237 <p class="noindent"><a href="tags/">Теги</a>:
240 for count, tag, links in all_tags:
244 new_text.append(' - ')
245 new_text.append("""<a href="tags/%s.html">%s (%d)</a>""" % (tag, tag, count))
251 <p class="noindent">По годам:
254 for year in sorted(years.keys()):
258 new_text.append(' - ')
259 new_text.append('<a href="%s/">%s</a>' % (year, year))
266 $phd_pp_ru.respond(self)
269 write_if_changed(index_name, ''.join(new_text))
275 for year in sorted(years.keys()):
278 for month in sorted(months.keys()):
280 for day in sorted(months[month]):
282 key = year, month, day
284 for file, title, lead, tags in blog[key]:
285 if file.endswith(".tmpl"): file = file[:-len("tmpl")] + "html"
286 value = (year, month, day, file, title, lead)
287 all_titles.append(value)
288 year_titles.append(value)
289 month_titles.append(value)
290 day_titles.append(value)
293 tag_links = all_tags[tag]
295 tag_links = all_tags[tag] = []
296 tag_links.append(value)
297 write_template(3, year, month, day, day_titles)
298 write_template(2, year, month, day, month_titles)
299 write_template(1, year, month, day, year_titles)
301 all_tags = [(len(links), tag, links) for (tag, links) in all_tags.items()]
304 write_template(0, year, month, day, all_titles[-20:], all_tags)
307 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
310 #attr $Title = "Oleg BroytMann's blog: tags"
311 #attr $Description = "BroytMann Russian Blog Tags Index Document"
312 #attr $Copyright = 2006
321 for count, tag, links in all_tags:
323 <dt><a href="%s.html">%s (%d)</a></dt>
324 """ % (tag, tag, count))
327 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
330 #attr $Title = "Oleg BroytMann's blog: tag %s"
331 #attr $Description = "BroytMann Russian Blog Tag %s Index Document"
332 #attr $Copyright = 2006
339 """ % (tag, tag, tag)]
342 for year, month, day, filename, title, lead in reversed(links):
347 link = "../%s/%s/%s/%s" % (year, month, day, filename)
348 item_text = """<li><a href="%s">%s/%s/%s: %s%s</a></li>""" % (link, year, month, day, lead, title)
352 new_text.append(" <dd>%s</dd>\n" % item_text)
354 tag_text.append(" %s\n" % item_text)
360 $phd_pp_ru.respond(self)
362 write_if_changed(os.path.join("tags", tag+".tmpl"), ''.join(tag_text))
368 $phd_pp_ru.respond(self)
370 write_if_changed(os.path.join("tags", "index.tmpl"), ''.join(new_text))