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-2010 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 = (("News [Atom 1.0]", "application/atom+xml", "atom_10.xml"),
134 ("News [RSS 2.0]", "application/rss+xml", "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],
172 #attr $refresh = "0; URL=%s"
176 #attr $Title = "Oleg Broytman's blog: %(day)d %(month_abbr_en)s %(year)s"
177 #attr $Description = "Broytman Russian Blog %(day)d %(month_name_en)s %(year)s Index Document"
178 #attr $Copyright = %(cyear)s
181 <h1>Журнал: %(day)d %(month_name_ru)s %(year)s</h1>
183 "year": year, "cyear": year or 2005,
184 "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
185 "month_name_ru": months_names_ru[imonth],
189 save_titles = titles[:]
193 for year, month, day, file, title, lead in titles:
202 href = '/'.join(href)
203 if day[0] == '0': day = day[1:]
204 if save_date <> (year, month, day):
206 new_text.append('\n<h2>%s %s %s</h2>' % (day, months_names_ru[int(month)], year))
208 new_text.append('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
209 save_date = year, month, day
216 %s<a href="%s">%s</a>.
218 ''' % (lead, href, title))
224 <p class="head">Новостевая лента в форматах
225 <A HREF="atom_10.xml">Atom 1.0 <img src="../../Graphics/atom_10.jpg" border=0></A>
226 и <A HREF="rss_20.xml">RSS 2.0 <img src="../../Graphics/rss_20.jpg" border=0></A>.
231 for year, month, day, file, title, lead in save_titles:
234 <p class="head"><a href="tags/">Теги</a>:
237 for count, tag, links in all_tags:
241 new_text.append(' - ')
242 new_text.append("""<a href="tags/%s.html">%s (%d)</a>""" % (tag, tag, count))
247 max_year = int(sorted(years.keys())[-1])
248 years = range(2005, max_year+1)
251 <p class="head">По годам:
258 new_text.append(' - ')
259 new_text.append('<a href="%s/">%s</a>' % (year, year))
266 <p class="head"><a href="http://phd.livejournal.com/">ЖЖ</a>
271 $phd_pp_ru.respond(self)
274 write_if_changed(index_name, ''.join(new_text))
281 for year in sorted(years.keys()):
284 for month in sorted(months.keys()):
286 for day in sorted(months[month]):
288 key = year, month, day
290 for file, title, lead, tags in blog[key]:
291 if file.endswith(".tmpl"): file = file[:-len("tmpl")] + "html"
292 value = (year, month, day, file, title, lead)
293 all_titles_tags.append((year, month, day, file, title, lead, tags))
294 all_titles.append(value)
295 year_titles.append(value)
296 month_titles.append(value)
297 day_titles.append(value)
300 tag_links = all_tags[tag]
302 tag_links = all_tags[tag] = []
303 tag_links.append(value)
304 write_template(3, year, month, day, day_titles)
305 write_template(2, year, month, day, month_titles)
306 write_template(1, year, month, day, year_titles)
308 def by_count_rev_tag_link(t1, t2):
309 """Sort all_tags by count in descending order,
310 and by tags and links in ascending order
312 r = cmp(t1[0], t2[0])
315 return cmp((t1[1], t1[2]), (t2[1], t2[2]))
317 all_tags = [(len(links), tag, links) for (tag, links) in all_tags.items()]
318 all_tags.sort(by_count_rev_tag_link)
320 write_template(0, year, month, day, all_titles[-20:], all_tags)
323 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
326 #attr $Title = "Oleg Broytman's blog: tags"
327 #attr $Description = "Broytman Russian Blog Tags Index Document"
328 #attr $Copyright = 2006
337 for count, tag, links in all_tags:
339 <dt><a href="%s.html">%s (%d)</a></dt>
340 """ % (tag, tag, count))
343 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
346 #attr $Title = "Oleg Broytman's blog: tag %s"
347 #attr $Description = "Broytman Russian Blog Tag %s Index Document"
348 #attr $Copyright = 2006
355 """ % (tag, tag, tag)]
358 for year, month, day, filename, title, lead in reversed(links):
363 link = "../%s/%s/%s/%s" % (year, month, day, filename)
364 item_text = """<li><a href="%s">%s/%s/%s: %s%s</a></li>""" % (link, year, month, day, lead, title)
368 new_text.append(" <dd>%s</dd>\n" % item_text)
370 tag_text.append(" %s\n" % item_text)
376 $phd_pp_ru.respond(self)
378 write_if_changed(os.path.join(blog_root, "tags", tag+".tmpl"), ''.join(tag_text))
384 $phd_pp_ru.respond(self)
386 write_if_changed(os.path.join(blog_root, "tags", "index.tmpl"), ''.join(new_text))
389 from atom_10 import atom_10
390 from rss_20 import rss_20
391 from news import NewsItem
394 baseURL = "http://phd.pp.ru/%s/" % blog_root
396 baseURL = "http://phd.pp.ru/"
399 for item in tuple(reversed(all_titles_tags))[:10]:
400 year, month, day, file, title, lead, tags = item
406 "%s-%s-%s" % (year, month, day),
407 "%s%s" % (lead, title),
408 "%s/%s/%s/%s" % (year, month, day, file)
411 item.baseURL = baseURL
412 item.categoryList = tags
415 "title": "Oleg Broytman's blog",
420 "author": "Oleg Broytman",
421 "email": "phd@phd.pp.ru",
422 "generator": os.path.basename(sys.argv[0]),
427 locale.setlocale(locale.LC_TIME, 'C')
429 atom_tmpl = str(atom_10(searchList=[namespace]))
430 write_if_changed(os.path.join(blog_root, "atom_10.xml"), atom_tmpl)
431 rss_tmpl = str(rss_20(searchList=[namespace]))
432 write_if_changed(os.path.join(blog_root, "rss_20.xml"), rss_tmpl)