+
+def by_count_rev_tag_link(t1, t2):
+ """Sort all_tags by count in descending order,
+ and by tags and links in ascending order
+ """
+ r = cmp(t1[0], t2[0])
+ if r:
+ return -r
+ return cmp((t1[1], t1[2]), (t2[1], t2[2]))
+
+all_tags = [(len(links), tag, links) for (tag, links) in all_tags.items()]
+all_tags.sort(by_count_rev_tag_link)
+
+write_template(0, year, month, day, all_titles[-20:], all_tags)
+
+new_text = ["""\
+## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
+#encoding koi8-r
+#extends phd_site
+#implements respond
+#attr $Title = "Oleg Broytman's blog: tags"
+#attr $Description = "Broytman Russian Blog Tags Index Document"
+#attr $Copyright = 2006
+##
+#def body_html
+<h1>Теги</h1>
+
+<p class="head small">
+Форма поиска позволяет искать сообщения в блоге, соответствующие выражению.
+Синтаксис выражения:</p>
+<ul class="small">
+ <li>Тег - если такой тег существует, произойдёт перенаправление на страницу тега.</li>
+ <li>Оператор '!' (NOT, not) - ищет записи, в которых нет этого тега.</li>
+ <li>Оператор '&' (AND, and) - ищет записи, в которых есть оба тега.</li>
+ <li>Оператор '|' (OR, or) - ищет записи, в которых есть любые из тегов.</li>
+ <li>Скобки '()' позволяют объединять выражения.</li>
+</ul>
+<p class="small">
+Примеры выражений: linux - произойдёт перенаправление
+на страницу linux.html; linux&!audio - искать записи в которых есть тег
+linux и нет тега audio; linux and not audio - то же самое.
+</p>
+
+<center>
+<form method=GET action="../../../cgi-bin/blog-ru/search-tags/search-tags.py">
+ <input type=text name=q>
+ <input type=submit name=submit value="Искать">
+</form>
+</center>
+
+<dl>
+"""]
+
+for i, (count, tag, links) in enumerate(all_tags):
+ new_text.append("""\
+ <dt><a href="%s.html">%s (%d)</a></dt>
+""" % (tag, tag, count))
+
+ first = all_tags[0][1]
+ if i == 0:
+ prev = None
+ else:
+ prev = all_tags[i-1][1]
+ if i >= len(all_tags)-1:
+ next = None
+ else:
+ next = all_tags[i+1][1]
+ last = all_tags[-1][1]
+
+ tag_text = ["""\
+## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
+#encoding koi8-r
+#extends phd_site
+#implements respond
+#attr $Title = "Oleg Broytman's blog: tag %s"
+#attr $Description = "Broytman Russian Blog Tag %s Index Document"
+""" % (tag, tag)]
+
+ tag_text.append("""\
+#attr $First = "%s"
+""" % first)
+
+ if prev:
+ tag_text.append("""\
+#attr $Prev = "%s"
+""" % prev)
+
+ if next:
+ tag_text.append("""\
+#attr $Next = "%s"
+""" % next)
+
+ tag_text.append("""\
+#attr $Last = "%s"
+""" % last)
+
+ tag_text.append("""\
+#attr $Copyright = 2006
+##
+#def body_html
+<h1>%s</h1>
+
+<ul>
+""" % tag)
+
+ count = 0
+ for year, month, day, filename, title, lead in reversed(links):
+ link = "../%s/%s/%s/%s" % (year, month, day, filename)
+ item_text = """<li><a href="%s">%s/%s/%s: %s%s</a></li>""" % (link, year, month, day, lead+' ' if lead else '', title)
+
+ count += 1
+ if count <= 5:
+ new_text.append(" <dd>%s</dd>\n" % item_text)
+
+ tag_text.append(" %s\n" % item_text)
+
+ tag_text.append("""\
+</ul>
+#end def
+$phd_site.respond(self)
+""")
+ write_if_changed(os.path.join(blog_root, "tags", tag+".tmpl"), ''.join(tag_text))
+
+new_text.append("""\
+</dl>
+#end def
+$phd_site.respond(self)
+""")
+write_if_changed(os.path.join(blog_root, "tags", "index.tmpl"), ''.join(new_text))
+
+
+from HTMLParser import HTMLParseError
+import cgi
+from urlparse import urljoin
+from m_lib.net.www.html import HTMLParser as _HTMLParser
+
+class HTMLDone(Exception): pass
+
+
+class FirstPHTMLParser(_HTMLParser):
+ def __init__(self):
+ _HTMLParser.__init__(self)
+ self.first_p = None
+
+ def start_p(self, attrs):
+ self.accumulator = '<p>'
+
+ def end_p(self):
+ self.first_p = self.accumulator + '</p>'
+ raise HTMLDone()
+
+def get_first_p(body):
+ parser = FirstPHTMLParser()
+
+ try:
+ parser.feed(body)
+ except (HTMLParseError, HTMLDone):
+ pass
+
+ try:
+ parser.close()
+ except (HTMLParseError, HTMLDone):
+ pass
+
+ return parser.first_p
+
+
+class AbsURLHTMLParser(_HTMLParser):
+ def __init__(self, base):
+ _HTMLParser.__init__(self)
+ self.base = base
+
+ def start_a(self, attrs):
+ self.accumulator += '<a'
+ for attrname, value in attrs:
+ value = cgi.escape(value, True)
+ if attrname == 'href':
+ self.accumulator += ' href="%s"' % urljoin(self.base, value)
+ else:
+ self.accumulator += ' %s="%s"' % (attrname, value)
+ self.accumulator += '>'
+
+ def end_a(self):
+ self.accumulator += '</a>'
+
+ def start_img(self, attrs):
+ self.accumulator += '<img'
+ for attrname, value in attrs:
+ value = cgi.escape(value, True)
+ if attrname == 'src':
+ self.accumulator += ' src="%s"' % urljoin(self.base, value)
+ else:
+ self.accumulator += ' %s="%s"' % (attrname, value)
+ self.accumulator += '>'
+
+ def end_img(self):
+ pass
+
+def absolute_urls(body, base):
+ parser = AbsURLHTMLParser(base)
+
+ try:
+ parser.feed(body)
+ except HTMLParseError:
+ pass
+
+ try:
+ parser.close()
+ except HTMLParseError:
+ pass
+
+ return parser.accumulator
+
+
+from atom_10 import atom_10
+from rss_20 import rss_20
+from news import NewsItem
+
+if blog_root:
+ baseURL = "http://phdru.name/%s/" % blog_root
+else:
+ baseURL = "http://phdru.name/"
+
+items = []
+for item in tuple(reversed(all_titles_tags))[:10]:
+ year, month, day, file, title, lead, tags = item
+ lead = lead.decode('koi8-r').encode('utf-8')
+ title = title.decode('koi8-r').encode('utf-8')
+ url_path = "%s/%s/%s/%s" % (year, month, day, file)
+ item = NewsItem(
+ "%s-%s-%s" % (year, month, day),
+ "%s%s" % (lead+' ' if lead else '', title),
+ url_path)
+ items.append(item)
+ item.baseURL = baseURL
+ item.categoryList = tags
+ body = bodies[(year, month, day, file)]
+ body = absolute_urls(body, baseURL + url_path)
+ item.body = body
+ item.excerpt = get_first_p(body)
+
+namespace = {
+ "title": "Oleg Broytman's blog",
+ "baseURL": baseURL,
+ "indexFile": "",
+ "description": "",
+ "lang": "ru",
+ "author": "Oleg Broytman",
+ "email": "phd@phdru.name",
+ "generator": os.path.basename(sys.argv[0]),
+ "posts": items,
+}
+
+# For english dates
+locale.setlocale(locale.LC_TIME, 'C')
+
+atom_tmpl = unicode(atom_10(searchList=[namespace])).encode('koi8-r')
+write_if_changed(os.path.join(blog_root, "atom_10.xml"), atom_tmpl)
+rss_tmpl = unicode(rss_20(searchList=[namespace])).encode('koi8-r')
+write_if_changed(os.path.join(blog_root, "rss_20.xml"), rss_tmpl)
+
+for item in items:
+ item.excerpt = None
+
+atom_tmpl = unicode(atom_10(searchList=[namespace])).encode('koi8-r')
+write_if_changed(os.path.join(blog_root, "atom_10_titles.xml"), atom_tmpl)
+rss_tmpl = unicode(rss_20(searchList=[namespace])).encode('koi8-r')
+write_if_changed(os.path.join(blog_root, "rss_20_titles.xml"), rss_tmpl)
+
+for item in items:
+ item.content = item.body
+
+atom_tmpl = unicode(atom_10(searchList=[namespace])).encode('koi8-r')
+write_if_changed(os.path.join(blog_root, "atom_10_full.xml"), atom_tmpl)
+rss_tmpl = unicode(rss_20(searchList=[namespace])).encode('koi8-r')
+write_if_changed(os.path.join(blog_root, "rss_20_full.xml"), rss_tmpl)