+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)
+ body = body.decode('koi8-r').encode('utf-8')
+ 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)