2 # -*- coding: koi8-r -*-
4 __version__ = "$Revision$"[11:-2]
5 __revision__ = "$Id$"[5:-2]
6 __date__ = "$Date$"[7:-2]
7 __author__ = "Oleg Broytman <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)
87 # Localized month names
90 locale.setlocale(locale.LC_ALL, "ru_RU.KOI8-R")
91 from calendar import _localized_day, _localized_month
93 locale.setlocale(locale.LC_TIME, 'C')
94 months_names_en = list(_localized_month('%B'))
95 months_abbrs_en = list(_localized_month('%b'))
97 locale.setlocale(locale.LC_TIME, "ru_RU.KOI8-R")
98 #months_names_ru = list(_localized_month('%B'))
100 months_names_ru = ['', "января", "февраля", "марта", "апреля", "мая", "июня",
101 "июля", "августа", "сентября", "октября", "ноября", "декабря"
104 months_names_ru0 = ['', "январь", "февраль", "март", "апрель", "май", "июнь",
105 "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь"
108 from news import write_if_changed
111 def write_template(level, year, month, day, titles, tags=None):
119 path.append("index.tmpl")
120 index_name = os.path.join(*path)
123 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
130 #attr $Title = "Oleg Broytman's blog"
131 #attr $Description = "Broytman Russian Blog Index Document"
132 #attr $Copyright = %(cyear)s
133 #attr $alternates = (("application/atom+xml", "News [Atom 1.0]", "atom_10.xml"),
134 ("application/rss+xml", "News [RSS 2.0]", "rss_20.xml")
139 """ % {"cyear": year or 2005})
143 #attr $Title = "Oleg Broytman's blog: %(year)s"
144 #attr $Description = "Broytman Russian Blog %(year)s Index Document"
145 #attr $Copyright = %(cyear)s
148 <h1>Журнал: %(year)s</h1>
149 """ % {"year": year, "cyear": year or 2005})
154 #attr $Title = "Oleg Broytman's blog: %(month_abbr_en)s %(year)s"
155 #attr $Description = "Broytman Russian Blog %(month_name_en)s %(year)s Index Document"
156 #attr $Copyright = %(cyear)s
159 <h1>Журнал: %(month_name_ru0)s %(year)s</h1>
161 "year": year, "cyear": year or 2005,
162 "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
163 "month_name_ru0": months_names_ru0[imonth],
177 #attr $refresh = "0; URL=%s"
181 #attr $Title = "Oleg Broytman's blog: %(day)d %(month_abbr_en)s %(year)s"
182 #attr $Description = "Broytman Russian Blog %(day)d %(month_name_en)s %(year)s Index Document"
183 #attr $Copyright = %(cyear)s
186 <h1>Журнал: %(day)d %(month_name_ru)s %(year)s</h1>
188 "year": year, "cyear": year or 2005,
189 "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
190 "month_name_ru": months_names_ru[imonth],
194 save_titles = titles[:]
198 for year, month, day, file, title, lead in titles:
207 href = '/'.join(href)
208 if day[0] == '0': day = day[1:]
209 if save_date <> (year, month, day):
211 new_text.append('\n<h2>%s %s %s</h2>' % (day, months_names_ru[int(month)], year))
213 new_text.append('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
214 save_date = year, month, day
221 %s<a href="%s">%s</a>.
223 ''' % (lead, href, title))
229 <p class="head">Новостевая лента в форматах
230 <A HREF="atom_10.xml">Atom 1.0 <img src="../../Graphics/atom_10.jpg" border=0></A>
231 и <A HREF="rss_20.xml">RSS 2.0 <img src="../../Graphics/rss_20.jpg" border=0></A>.
236 for year, month, day, file, title, lead in save_titles:
239 <p class="head"><a href="tags/">Теги</a>:
242 for count, tag, links in all_tags:
246 new_text.append(' - ')
247 new_text.append("""<a href="tags/%s.html">%s (%d)</a>""" % (tag, tag, count))
252 max_year = int(sorted(years.keys())[-1])
253 years = range(2005, max_year+1)
256 <p class="head">По годам:
263 new_text.append(' - ')
264 new_text.append('<a href="%s/">%s</a>' % (year, year))
271 <p class="head"><a href="http://phd.livejournal.com/">ЖЖ</a>
276 $phd_pp_ru.respond(self)
279 write_if_changed(index_name, ''.join(new_text))
286 for year in sorted(years.keys()):
289 for month in sorted(months.keys()):
291 for day in sorted(months[month]):
293 key = year, month, day
295 for file, title, lead, tags in blog[key]:
296 if file.endswith(".tmpl"): file = file[:-len("tmpl")] + "html"
297 value = (year, month, day, file, title, lead)
298 all_titles_tags.append((year, month, day, file, title, lead, tags))
299 all_titles.append(value)
300 year_titles.append(value)
301 month_titles.append(value)
302 day_titles.append(value)
305 tag_links = all_tags[tag]
307 tag_links = all_tags[tag] = []
308 tag_links.append(value)
309 write_template(3, year, month, day, day_titles)
310 write_template(2, year, month, day, month_titles)
311 write_template(1, year, month, day, year_titles)
313 def by_count_rev_tag_link(t1, t2):
314 """Sort all_tags by count in descending order,
315 and by tags and links in ascending order
317 r = cmp(t1[0], t2[0])
320 return cmp((t1[1], t1[2]), (t2[1], t2[2]))
322 all_tags = [(len(links), tag, links) for (tag, links) in all_tags.items()]
323 all_tags.sort(by_count_rev_tag_link)
325 write_template(0, year, month, day, all_titles[-20:], all_tags)
328 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
331 #attr $Title = "Oleg Broytman's blog: tags"
332 #attr $Description = "Broytman Russian Blog Tags Index Document"
333 #attr $Copyright = 2006
342 for count, tag, links in all_tags:
344 <dt><a href="%s.html">%s (%d)</a></dt>
345 """ % (tag, tag, count))
348 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
351 #attr $Title = "Oleg Broytman's blog: tag %s"
352 #attr $Description = "Broytman Russian Blog Tag %s Index Document"
353 #attr $Copyright = 2006
360 """ % (tag, tag, tag)]
363 for year, month, day, filename, title, lead in reversed(links):
368 link = "../%s/%s/%s/%s" % (year, month, day, filename)
369 item_text = """<li><a href="%s">%s/%s/%s: %s%s</a></li>""" % (link, year, month, day, lead, title)
373 new_text.append(" <dd>%s</dd>\n" % item_text)
375 tag_text.append(" %s\n" % item_text)
381 $phd_pp_ru.respond(self)
383 write_if_changed(os.path.join(blog_root, "tags", tag+".tmpl"), ''.join(tag_text))
389 $phd_pp_ru.respond(self)
391 write_if_changed(os.path.join(blog_root, "tags", "index.tmpl"), ''.join(new_text))
394 from atom_10 import atom_10
395 from rss_20 import rss_20
396 from news import NewsItem
399 baseURL = "http://phd.pp.ru/%s/" % blog_root
401 baseURL = "http://phd.pp.ru/"
404 for item in tuple(reversed(all_titles_tags))[:10]:
405 year, month, day, file, title, lead, tags = item
411 "%s-%s-%s" % (year, month, day),
412 "%s%s" % (lead, title),
413 "%s/%s/%s/%s" % (year, month, day, file)
416 item.baseURL = baseURL
417 item.categoryList = tags
420 "title": "Oleg Broytman's blog",
425 "author": "Oleg Broytman",
426 "email": "phd@phd.pp.ru",
427 "generator": os.path.basename(sys.argv[0]),
432 locale.setlocale(locale.LC_TIME, 'C')
434 atom_tmpl = str(atom_10(searchList=[namespace]))
435 write_if_changed(os.path.join(blog_root, "atom_10.xml"), atom_tmpl)
436 rss_tmpl = str(rss_20(searchList=[namespace]))
437 write_if_changed(os.path.join(blog_root, "rss_20.xml"), rss_tmpl)