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"
13 blog_data_root = sys.argv[1]
14 blog_root = sys.argv[2]
15 blog_filename = os.path.join(blog_data_root, "blog_dict.pickle")
18 import cPickle as pickle
22 from Cheetah.Template import Template
28 blog_file = open(blog_filename, "rb")
32 old_blog = pickle.load(blog_file)
36 # blog is a dictionary mapping
37 # (year, month, day) => [list of (file, title, lead, tags)]
42 # Walk the directory recursively
43 for dirpath, dirs, files in os.walk(blog_root):
44 d = os.path.basename(dirpath)
45 if not d.startswith("20") and not d.isdigit():
48 if not file.endswith(".tmpl"):
50 fullpath = os.path.join(dirpath, file)
51 template = Template(file=fullpath)
52 title_parts = template.Title.split()
53 title = ' '.join(title_parts[6:])
54 lead = getattr(template, "Lead", None)
57 if isinstance(tags, basestring):
61 key = year, month, day = tuple(dirpath[len(blog_root):].split(os.sep)[1:])
66 days.append((file, title, lead, tags))
71 months = years[year] = {}
76 days = months[month] = []
78 if day not in days: days.append(day)
81 # Need to save the blog?
83 blog_file = open(blog_filename, "wb")
84 pickle.dump(blog, blog_file, pickle.HIGHEST_PROTOCOL)
88 # Localized month names
91 locale.setlocale(locale.LC_ALL, '')
92 from calendar import _localized_day, _localized_month
94 locale.setlocale(locale.LC_TIME, 'C')
95 months_names_en = list(_localized_month('%B'))
96 months_abbrs_en = list(_localized_month('%b'))
98 locale.setlocale(locale.LC_TIME, '')
99 months_names_ru = [month.lower() for month in _localized_month('%B')]
101 months_names_ru0 = ['', "январь", "февраль", "март", "апрель", "май", "июнь",
102 "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь"
105 from news import write_if_changed
108 def write_template(level, year, month, day, titles, tags=None):
116 path.append("index.tmpl")
117 index_name = os.path.join(*path)
120 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
127 #attr $Title = "Oleg BroytMann's blog"
128 #attr $Description = "BroytMann Russian Blog Index Document"
129 #attr $Copyright = %(cyear)s
130 #attr $alternates = (("application/atom+xml", "News [Atom 1.0]", "atom_10.xml"),
131 ("application/rss+xml", "News [RSS 2.0]", "rss_20.xml")
136 """ % {"cyear": year or 2005})
140 #attr $Title = "Oleg BroytMann's blog: %(year)s"
141 #attr $Description = "BroytMann Russian Blog %(year)s Index Document"
142 #attr $Copyright = %(cyear)s
145 <H1>Журнал: %(year)s</H1>
146 """ % {"year": year, "cyear": year or 2005})
151 #attr $Title = "Oleg BroytMann's blog: %(month_abbr_en)s %(year)s"
152 #attr $Description = "BroytMann Russian Blog %(month_name_en)s %(year)s Index Document"
153 #attr $Copyright = %(cyear)s
156 <H1>Журнал: %(month_name_ru0)s %(year)s</H1>
158 "year": year, "cyear": year or 2005,
159 "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
160 "month_name_ru0": months_names_ru0[imonth],
174 #attr $refresh = "0; URL=%s"
178 #attr $Title = "Oleg BroytMann's blog: %(day)d %(month_abbr_en)s %(year)s"
179 #attr $Description = "BroytMann Russian Blog %(day)d %(month_name_en)s %(year)s Index Document"
180 #attr $Copyright = %(cyear)s
183 <H1>Журнал: %(day)d %(month_name_ru)s %(year)s</H1>
185 "year": year, "cyear": year or 2005,
186 "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
187 "month_name_ru": months_names_ru[imonth],
191 save_titles = titles[:]
195 for year, month, day, file, title, lead in titles:
204 href = '/'.join(href)
205 if day[0] == '0': day = day[1:]
208 new_text.append('\n<h2>%s %s %s</h2>' % (day, months_names_ru[int(month)], year))
210 new_text.append('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
218 %s<a href="%s">%s</a>.
220 ''' % (lead, href, title))
226 <p class="head">Новостевая лента в форматах
227 <A HREF="atom_10.xml">Atom 1.0 <img src="../../Graphics/atom_10.jpg" border=0></A>
228 и <A HREF="rss_20.xml">RSS 2.0 <img src="../../Graphics/rss_20.jpg" border=0></A>.
233 for year, month, day, file, title, lead in save_titles:
236 <p class="head"><a href="tags/">Теги</a>:
239 for count, tag, links in all_tags:
243 new_text.append(' - ')
244 new_text.append("""<a href="tags/%s.html">%s (%d)</a>""" % (tag, tag, count))
250 <p class="head">По годам:
253 for year in sorted(years.keys()):
257 new_text.append(' - ')
258 new_text.append('<a href="%s/">%s</a>' % (year, year))
265 <p class="head"><a href="http://phd.livejournal.com/">ЖЖ</a>
270 $phd_pp_ru.respond(self)
273 write_if_changed(index_name, ''.join(new_text))
280 for year in sorted(years.keys()):
283 for month in sorted(months.keys()):
285 for day in sorted(months[month]):
287 key = year, month, day
289 for file, title, lead, tags in blog[key]:
290 if file.endswith(".tmpl"): file = file[:-len("tmpl")] + "html"
291 value = (year, month, day, file, title, lead)
292 all_titles_tags.append((year, month, day, file, title, lead, tags))
293 all_titles.append(value)
294 year_titles.append(value)
295 month_titles.append(value)
296 day_titles.append(value)
299 tag_links = all_tags[tag]
301 tag_links = all_tags[tag] = []
302 tag_links.append(value)
303 write_template(3, year, month, day, day_titles)
304 write_template(2, year, month, day, month_titles)
305 write_template(1, year, month, day, year_titles)
307 def by_count_rev_tag_link(t1, t2):
308 """Sort all_tags by count in descending order,
309 and by tags and links in ascending order
311 r = cmp(t1[0], t2[0])
314 return cmp((t1[1], t1[2]), (t2[1], t2[2]))
316 all_tags = [(len(links), tag, links) for (tag, links) in all_tags.items()]
317 all_tags.sort(by_count_rev_tag_link)
319 write_template(0, year, month, day, all_titles[-20:], all_tags)
322 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
325 #attr $Title = "Oleg BroytMann's blog: tags"
326 #attr $Description = "BroytMann Russian Blog Tags Index Document"
327 #attr $Copyright = 2006
336 for count, tag, links in all_tags:
338 <dt><a href="%s.html">%s (%d)</a></dt>
339 """ % (tag, tag, count))
342 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
345 #attr $Title = "Oleg BroytMann's blog: tag %s"
346 #attr $Description = "BroytMann Russian Blog Tag %s Index Document"
347 #attr $Copyright = 2006
354 """ % (tag, tag, tag)]
357 for year, month, day, filename, title, lead in reversed(links):
362 link = "../%s/%s/%s/%s" % (year, month, day, filename)
363 item_text = """<li><a href="%s">%s/%s/%s: %s%s</a></li>""" % (link, year, month, day, lead, title)
367 new_text.append(" <dd>%s</dd>\n" % item_text)
369 tag_text.append(" %s\n" % item_text)
375 $phd_pp_ru.respond(self)
377 write_if_changed(os.path.join(blog_root, "tags", tag+".tmpl"), ''.join(tag_text))
383 $phd_pp_ru.respond(self)
385 write_if_changed(os.path.join(blog_root, "tags", "index.tmpl"), ''.join(new_text))
388 from atom_10 import atom_10
389 from rss_20 import rss_20
390 from news import NewsItem
392 baseURL = "http://phd.pp.ru/" + blog_root
395 for item in tuple(reversed(all_titles_tags))[:10]:
396 year, month, day, file, title, lead, tags = item
402 "%s-%s-%s" % (year, month, day),
403 "%s%s" % (lead, title),
404 "%s/%s/%s/%s" % (year, month, day, file)
407 item.baseURL = baseURL
408 item.categoryList = tags
411 "title": "Oleg Broytmann's blog",
416 "author": "Oleg Broytmann",
417 "email": "phd@phd.pp.ru",
418 "generator": os.path.basename(sys.argv[0]),
423 locale.setlocale(locale.LC_TIME, 'C')
425 atom_tmpl = str(atom_10(searchList=[namespace]))
426 write_if_changed(os.path.join(blog_root, "atom_10.xml"), atom_tmpl)
427 rss_tmpl = str(rss_20(searchList=[namespace]))
428 write_if_changed(os.path.join(blog_root, "rss_20.xml"), rss_tmpl)