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_filename = sys.argv[1]
14 blog_root = sys.argv[2]
17 import cPickle as pickle
21 from Cheetah.Template import Template
27 blog_file = open(blog_filename, "rb")
31 old_blog = pickle.load(blog_file)
35 # blog is a dictionary mapping
36 # (year, month, day) => [list of (file, title, lead, tags)]
41 # Walk the directory recursively
42 for dirpath, dirs, files in os.walk(blog_root):
43 d = os.path.basename(dirpath)
44 if not d.startswith("20") and not d.isdigit():
47 # Ignore index.tmpl and *.html files; supose all other files are *.tmpl
48 if file == "index.tmpl" or file.endswith(".html"):
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)
56 tags = getattr(template, "Tag", 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
133 """ % {"cyear": year or 2005})
137 #attr $Title = "Oleg BroytMann's blog: %(year)s"
138 #attr $Description = "BroytMann Russian Blog %(year)s Index Document"
139 #attr $Copyright = %(cyear)s
142 <H1>Журнал: %(year)s</H1>
143 """ % {"year": year, "cyear": year or 2005})
148 #attr $Title = "Oleg BroytMann's blog: %(month_abbr_en)s %(year)s"
149 #attr $Description = "BroytMann Russian Blog %(month_name_en)s %(year)s Index Document"
150 #attr $Copyright = %(cyear)s
153 <H1>Журнал: %(month_name_ru0)s %(year)s</H1>
155 "year": year, "cyear": year or 2005,
156 "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
157 "month_name_ru0": months_names_ru0[imonth],
171 #attr $refresh = "0; URL=%s"
175 #attr $Title = "Oleg BroytMann's blog: %(day)d %(month_abbr_en)s %(year)s"
176 #attr $Description = "BroytMann Russian Blog %(day)d %(month_name_en)s %(year)s Index Document"
177 #attr $Copyright = %(cyear)s
180 <H1>Журнал: %(day)d %(month_name_ru0)s %(year)s</H1>
182 "year": year, "cyear": year or 2005,
183 "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
184 "month_name_ru0": months_names_ru0[imonth],
188 save_titles = titles[:]
192 for year, month, day, file, title, lead in titles:
201 href = '/'.join(href)
202 if day[0] == '0': day = day[1:]
205 new_text.append('\n<h2>%s %s %s</h2>' % (day, months_names_ru[int(month)], year))
207 new_text.append('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
215 %s<a href="%s">%s</a>.
217 ''' % (lead, href, title))
223 <p class="head">Новостевая лента в форматах
224 <A HREF="atom_10.xml">Atom 1.0 <img src="../../Graphics/feed-icon-16x16.png" border=0></A>
225 и <A HREF="rss_20.xml">RSS 2.0 <img src="../../Graphics/feed-icon-16x16.png" border=0></A>.
230 for year, month, day, file, title, lead in save_titles:
233 <p class="head"><a href="tags/">Теги</a>:
236 for count, tag, links in all_tags:
240 new_text.append(' - ')
241 new_text.append("""<a href="tags/%s.html">%s (%d)</a>""" % (tag, tag, count))
247 <p class="head">По годам:
250 for year in sorted(years.keys()):
254 new_text.append(' - ')
255 new_text.append('<a href="%s/">%s</a>' % (year, year))
262 $phd_pp_ru.respond(self)
265 write_if_changed(index_name, ''.join(new_text))
272 for year in sorted(years.keys()):
275 for month in sorted(months.keys()):
277 for day in sorted(months[month]):
279 key = year, month, day
281 for file, title, lead, tags in blog[key]:
282 if file.endswith(".tmpl"): file = file[:-len("tmpl")] + "html"
283 value = (year, month, day, file, title, lead)
284 all_titles_tags.append((year, month, day, file, title, lead, tags))
285 all_titles.append(value)
286 year_titles.append(value)
287 month_titles.append(value)
288 day_titles.append(value)
291 tag_links = all_tags[tag]
293 tag_links = all_tags[tag] = []
294 tag_links.append(value)
295 write_template(3, year, month, day, day_titles)
296 write_template(2, year, month, day, month_titles)
297 write_template(1, year, month, day, year_titles)
299 all_tags = [(len(links), tag, links) for (tag, links) in all_tags.items()]
302 write_template(0, year, month, day, all_titles[-20:], all_tags)
305 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
308 #attr $Title = "Oleg BroytMann's blog: tags"
309 #attr $Description = "BroytMann Russian Blog Tags Index Document"
310 #attr $Copyright = 2006
319 for count, tag, links in all_tags:
321 <dt><a href="%s.html">%s (%d)</a></dt>
322 """ % (tag, tag, count))
325 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
328 #attr $Title = "Oleg BroytMann's blog: tag %s"
329 #attr $Description = "BroytMann Russian Blog Tag %s Index Document"
330 #attr $Copyright = 2006
337 """ % (tag, tag, tag)]
340 for year, month, day, filename, title, lead in reversed(links):
345 link = "../%s/%s/%s/%s" % (year, month, day, filename)
346 item_text = """<li><a href="%s">%s/%s/%s: %s%s</a></li>""" % (link, year, month, day, lead, title)
350 new_text.append(" <dd>%s</dd>\n" % item_text)
352 tag_text.append(" %s\n" % item_text)
358 $phd_pp_ru.respond(self)
360 write_if_changed(os.path.join(blog_root, "tags", tag+".tmpl"), ''.join(tag_text))
366 $phd_pp_ru.respond(self)
368 write_if_changed(os.path.join(blog_root, "tags", "index.tmpl"), ''.join(new_text))
371 from atom_10 import atom_10
372 from rss_20 import rss_20
373 from news import NewsItem
375 baseURL = "http://phd.pp.ru/Russian/blog/"
378 for item in tuple(reversed(all_titles_tags))[:10]:
379 year, month, day, file, title, lead, tags = item
385 "%s-%s-%s" % (year, month, day),
386 "%s%s" % (lead, title),
387 "%s/%s/%s/%s" % (year, month, day, file)
390 item.baseURL = baseURL
391 item.categoryList = tags
394 "title": "Oleg Broytmann's blog",
399 "author": "Oleg Broytmann",
400 "email": "phd@phd.pp.ru",
405 locale.setlocale(locale.LC_TIME, 'C')
407 atom_tmpl = str(atom_10(searchList=[namespace]))
408 write_if_changed(os.path.join(blog_root, "atom_10.xml"), atom_tmpl)
409 rss_tmpl = str(rss_20(searchList=[namespace]))
410 write_if_changed(os.path.join(blog_root, "rss_20.xml"), rss_tmpl)