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 # excerpts nd bodies are dictionaries mapping file => excerpt/body
47 # Walk the directory recursively
48 for dirpath, dirs, files in os.walk(blog_root):
49 d = os.path.basename(dirpath)
50 if not d.startswith("20") and not d.isdigit():
53 if not file.endswith(".tmpl"):
55 fullpath = os.path.join(dirpath, file)
56 template = Template(file=fullpath)
57 title_parts = template.Title.split()
58 title = ' '.join(title_parts[6:])
59 lead = getattr(template, "Lead", None)
62 if isinstance(tags, basestring):
66 key = year, month, day = tuple(dirpath[len(blog_root):].split(os.sep)[1:])
71 days.append((file, title, lead, tags))
76 months = years[year] = {}
81 days = months[month] = []
83 if day not in days: days.append(day)
85 file = file[:-len("tmpl")] + "html"
86 key = (year, month, day, file)
87 excerpts[key] = template.get_first_p()
88 bodies[key] = template.Body
90 # Need to save the blog?
92 blog_file = open(blog_filename, "wb")
93 pickle.dump(blog, blog_file, pickle.HIGHEST_PROTOCOL)
96 # Localized month names
99 locale.setlocale(locale.LC_ALL, "ru_RU.KOI8-R")
100 from calendar import _localized_day, _localized_month
102 locale.setlocale(locale.LC_TIME, 'C')
103 months_names_en = list(_localized_month('%B'))
104 months_abbrs_en = list(_localized_month('%b'))
106 locale.setlocale(locale.LC_TIME, "ru_RU.KOI8-R")
107 #months_names_ru = list(_localized_month('%B'))
109 months_names_ru = ['', "января", "февраля", "марта", "апреля", "мая", "июня",
110 "июля", "августа", "сентября", "октября", "ноября", "декабря"
113 months_names_ru0 = ['', "январь", "февраль", "март", "апрель", "май", "июнь",
114 "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь"
117 from news import write_if_changed
120 def write_template(level, year, month, day, titles, tags=None):
128 path.append("index.tmpl")
129 index_name = os.path.join(*path)
132 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
139 #attr $Title = "Oleg Broytman's blog"
140 #attr $Description = "Broytman Russian Blog Index Document"
141 #attr $Copyright = %(cyear)s
142 #attr $alternates = (("Новости [Atom 1.0] только заголовки", "application/atom+xml", "atom_10_titles.xml"),
143 ("Новости [Atom 1.0]", "application/atom+xml", "atom_10.xml"),
144 ("Новости [Atom 1.0] полные тексты", "application/atom+xml", "atom_10_full.xml"),
145 ("Новости [RSS 2.0] только заголовки", "application/rss+xml", "rss_20_titles.xml"),
146 ("Новости [RSS 2.0]", "application/rss+xml", "rss_20.xml"),
147 ("Новости [RSS 2.0] полные тексты", "application/rss+xml", "rss_20_full.xml"),
152 """ % {"cyear": year or 2005})
156 #attr $Title = "Oleg Broytman's blog: %(year)s"
157 #attr $Description = "Broytman Russian Blog %(year)s Index Document"
158 #attr $Copyright = %(cyear)s
161 <h1>Журнал: %(year)s</h1>
162 """ % {"year": year, "cyear": year or 2005})
167 #attr $Title = "Oleg Broytman's blog: %(month_abbr_en)s %(year)s"
168 #attr $Description = "Broytman Russian Blog %(month_name_en)s %(year)s Index Document"
169 #attr $Copyright = %(cyear)s
172 <h1>Журнал: %(month_name_ru0)s %(year)s</h1>
174 "year": year, "cyear": year or 2005,
175 "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
176 "month_name_ru0": months_names_ru0[imonth],
185 #attr $refresh = "0; URL=%s"
189 #attr $Title = "Oleg Broytman's blog: %(day)d %(month_abbr_en)s %(year)s"
190 #attr $Description = "Broytman Russian Blog %(day)d %(month_name_en)s %(year)s Index Document"
191 #attr $Copyright = %(cyear)s
194 <h1>Журнал: %(day)d %(month_name_ru)s %(year)s</h1>
196 "year": year, "cyear": year or 2005,
197 "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
198 "month_name_ru": months_names_ru[imonth],
202 save_titles = titles[:]
206 for year, month, day, file, title, lead in titles:
215 href = '/'.join(href)
216 if day[0] == '0': day = day[1:]
217 if save_date <> (year, month, day):
219 new_text.append('\n<h2>%s %s %s</h2>' % (day, months_names_ru[int(month)], year))
221 new_text.append('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
222 save_date = year, month, day
229 %s<a href="%s">%s</a>.
231 ''' % (lead, href, title))
237 <p class="head">Новостевая лента в форматах
238 <img src="../../Graphics/atom_10.jpg" border=0>
239 <A HREF="atom_10_titles.xml">Atom 1.0 только заголовки</A> /
240 <A HREF="atom_10.xml">Atom 1.0</A> /
241 <A HREF="atom_10_full.xml">Atom 1.0 полные тексты</A>
242 и <img src="../../Graphics/rss_20.jpg" border=0>
243 <A HREF="rss_20_titles.xml">RSS 2.0 только заголовки</A> /
244 <A HREF="rss_20.xml">RSS 2.0</A> /
245 <A HREF="rss_20_full.xml">RSS 2.0 полные тексты</A>.
250 for year, month, day, file, title, lead in save_titles:
253 <p class="head"><a href="tags/">Теги</a>:
256 for count, tag, links in all_tags:
260 new_text.append(' - ')
261 new_text.append("""<a href="tags/%s.html">%s (%d)</a>""" % (tag, tag, count))
266 max_year = int(sorted(years.keys())[-1])
267 years = range(2005, max_year+1)
270 <p class="head">По годам:
277 new_text.append(' - ')
278 new_text.append('<a href="%s/">%s</a>' % (year, year))
285 <p class="head"><a href="http://phd.livejournal.com/">ЖЖ</a>
290 $phd_pp_ru.respond(self)
293 write_if_changed(index_name, ''.join(new_text))
300 for year in sorted(years.keys()):
303 for month in sorted(months.keys()):
305 for day in sorted(months[month]):
307 key = year, month, day
309 for file, title, lead, tags in blog[key]:
310 if file.endswith(".tmpl"): file = file[:-len("tmpl")] + "html"
311 value = (year, month, day, file, title, lead)
312 all_titles_tags.append((year, month, day, file, title, lead, tags))
313 all_titles.append(value)
314 year_titles.append(value)
315 month_titles.append(value)
316 day_titles.append(value)
319 tag_links = all_tags[tag]
321 tag_links = all_tags[tag] = []
322 tag_links.append(value)
323 write_template(3, year, month, day, day_titles)
324 write_template(2, year, month, day, month_titles)
325 write_template(1, year, month, day, year_titles)
327 def by_count_rev_tag_link(t1, t2):
328 """Sort all_tags by count in descending order,
329 and by tags and links in ascending order
331 r = cmp(t1[0], t2[0])
334 return cmp((t1[1], t1[2]), (t2[1], t2[2]))
336 all_tags = [(len(links), tag, links) for (tag, links) in all_tags.items()]
337 all_tags.sort(by_count_rev_tag_link)
339 write_template(0, year, month, day, all_titles[-20:], all_tags)
342 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
345 #attr $Title = "Oleg Broytman's blog: tags"
346 #attr $Description = "Broytman Russian Blog Tags Index Document"
347 #attr $Copyright = 2006
356 for i, (count, tag, links) in enumerate(all_tags):
358 <dt><a href="%s.html">%s (%d)</a></dt>
359 """ % (tag, tag, count))
361 first = all_tags[0][1]
365 prev = all_tags[i-1][1]
366 if i >= len(all_tags)-1:
369 next = all_tags[i+1][1]
370 last = all_tags[-1][1]
373 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
376 #attr $Title = "Oleg Broytman's blog: tag %s"
377 #attr $Description = "Broytman Russian Blog Tag %s Index Document"
399 #attr $Copyright = 2006
409 for year, month, day, filename, title, lead in reversed(links):
414 link = "../%s/%s/%s/%s" % (year, month, day, filename)
415 item_text = """<li><a href="%s">%s/%s/%s: %s%s</a></li>""" % (link, year, month, day, lead, title)
419 new_text.append(" <dd>%s</dd>\n" % item_text)
421 tag_text.append(" %s\n" % item_text)
427 $phd_pp_ru.respond(self)
429 write_if_changed(os.path.join(blog_root, "tags", tag+".tmpl"), ''.join(tag_text))
435 $phd_pp_ru.respond(self)
437 write_if_changed(os.path.join(blog_root, "tags", "index.tmpl"), ''.join(new_text))
440 from atom_10 import atom_10
441 from rss_20 import rss_20
442 from news import NewsItem
445 baseURL = "http://phd.pp.ru/%s/" % blog_root
447 baseURL = "http://phd.pp.ru/"
450 for item in tuple(reversed(all_titles_tags))[:10]:
451 year, month, day, file, title, lead, tags = item
457 "%s-%s-%s" % (year, month, day),
458 "%s%s" % (lead, title),
459 "%s/%s/%s/%s" % (year, month, day, file)
462 item.baseURL = baseURL
463 item.categoryList = tags
464 item.excerpt = excerpts[(year, month, day, file)]
465 item.body = bodies[(year, month, day, file)]
468 "title": "Oleg Broytman's blog",
473 "author": "Oleg Broytman",
474 "email": "phd@phd.pp.ru",
475 "generator": os.path.basename(sys.argv[0]),
480 locale.setlocale(locale.LC_TIME, 'C')
482 atom_tmpl = str(atom_10(searchList=[namespace]))
483 write_if_changed(os.path.join(blog_root, "atom_10.xml"), atom_tmpl)
484 rss_tmpl = str(rss_20(searchList=[namespace]))
485 write_if_changed(os.path.join(blog_root, "rss_20.xml"), rss_tmpl)
490 atom_tmpl = str(atom_10(searchList=[namespace]))
491 write_if_changed(os.path.join(blog_root, "atom_10_titles.xml"), atom_tmpl)
492 rss_tmpl = str(rss_20(searchList=[namespace]))
493 write_if_changed(os.path.join(blog_root, "rss_20_titles.xml"), rss_tmpl)
496 item.content = item.body
498 atom_tmpl = str(atom_10(searchList=[namespace]))
499 write_if_changed(os.path.join(blog_root, "atom_10_full.xml"), atom_tmpl)
500 rss_tmpl = str(rss_20(searchList=[namespace]))
501 write_if_changed(os.path.join(blog_root, "rss_20_full.xml"), rss_tmpl)