]> git.phdru.name Git - phdru.name/phdru.name.git/blobdiff - reindex_blog.py
I've merged .gvimrc into .vimrc.
[phdru.name/phdru.name.git] / reindex_blog.py
index 0ba0804d4c1d458628744a4c9d8ba5f6c2781744..beba7c12823ae151a8f5de6619c9776758b9d713 100755 (executable)
@@ -39,6 +39,10 @@ else:
 blog = {}
 years = {}
 
+# bodies is a dictionary mapping file => body
+
+bodies = {}
+
 # Walk the directory recursively
 for dirpath, dirs, files in os.walk(blog_root):
    d = os.path.basename(dirpath)
@@ -51,7 +55,7 @@ for dirpath, dirs, files in os.walk(blog_root):
       template = Template(file=fullpath)
       title_parts = template.Title.split()
       title = ' '.join(title_parts[6:])
-      lead = getattr(template, "Lead", None)
+      lead = template.Lead
 
       tags = template.Tag
       if isinstance(tags, basestring):
@@ -77,6 +81,9 @@ for dirpath, dirs, files in os.walk(blog_root):
 
          if day not in days: days.append(day)
 
+         file = file[:-len("tmpl")] + "html"
+         key = (year, month, day, file)
+         bodies[key] = template.body()
 
 # Need to save the blog?
 if blog <> old_blog:
@@ -130,8 +137,12 @@ def write_template(level, year, month, day, titles, tags=None):
 #attr $Title = "Oleg Broytman's blog"
 #attr $Description = "Broytman Russian Blog Index Document"
 #attr $Copyright = %(cyear)s
-#attr $alternates = (("News [Atom 1.0]", "application/atom+xml", "atom_10.xml"),
-                     ("News [RSS 2.0]",  "application/rss+xml",  "rss_20.xml")
+#attr $alternates = (("Новости [Atom 1.0] только заголовки", "application/atom+xml", "atom_10_titles.xml"),
+                     ("Новости [Atom 1.0]", "application/atom+xml", "atom_10.xml"),
+                     ("Новости [Atom 1.0] полные тексты", "application/atom+xml", "atom_10_full.xml"),
+                     ("Новости [RSS 2.0] только заголовки",  "application/rss+xml",  "rss_20_titles.xml"),
+                     ("Новости [RSS 2.0]",  "application/rss+xml",  "rss_20.xml"),
+                     ("Новости [RSS 2.0] полные тексты",  "application/rss+xml",  "rss_20_full.xml"),
 )
 ##
 #def body_html
@@ -207,23 +218,25 @@ def write_template(level, year, month, day, titles, tags=None):
          else:
             new_text.append('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
          save_date = year, month, day
-      if lead:
-         lead = lead + ' '
-      else:
-         lead = ''
       new_text.append('''
 <p class="head">
    %s<a href="%s">%s</a>.
 </p>
-''' % (lead, href, title))
+''' % (lead+' ' if lead else '', href, title))
 
    if level == 0:
       new_text.append("""
 <hr>
 
 <p class="head">Новостевая лента в форматах
-<A HREF="atom_10.xml">Atom 1.0 <img src="../../Graphics/atom_10.jpg" border=0></A>
-и <A HREF="rss_20.xml">RSS 2.0 <img src="../../Graphics/rss_20.jpg" border=0></A>.
+<img src="../../Graphics/atom_10.jpg" border=0>
+<A HREF="atom_10_titles.xml">Atom 1.0 только заголовки</A> /
+<A HREF="atom_10.xml">Atom 1.0</A> /
+<A HREF="atom_10_full.xml">Atom 1.0 полные тексты</A>
+и <img src="../../Graphics/rss_20.jpg" border=0>
+<A HREF="rss_20_titles.xml">RSS 2.0 только заголовки</A> /
+<A HREF="rss_20.xml">RSS 2.0</A> /
+<A HREF="rss_20_full.xml">RSS 2.0 полные тексты</A>.
 </p>
 """)
 
@@ -388,12 +401,8 @@ for i, (count, tag, links) in enumerate(all_tags):
 
    count = 0
    for year, month, day, filename, title, lead in reversed(links):
-      if lead:
-         lead = lead + ' '
-      else:
-         lead = ''
       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, title)
+      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:
@@ -418,6 +427,76 @@ $phd_pp_ru.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 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
@@ -430,18 +509,18 @@ else:
 items = []
 for item in tuple(reversed(all_titles_tags))[:10]:
    year, month, day, file, title, lead, tags = item
-   if lead:
-      lead = lead + ' '
-   else:
-      lead = ''
+   url_path = "%s/%s/%s/%s" % (year, month, day, file)
    item = NewsItem(
       "%s-%s-%s" % (year, month, day),
-      "%s%s" % (lead, title),
-      "%s/%s/%s/%s" % (year, month, day, file)
-   )
+      "%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",
@@ -462,3 +541,19 @@ atom_tmpl = str(atom_10(searchList=[namespace]))
 write_if_changed(os.path.join(blog_root, "atom_10.xml"), atom_tmpl)
 rss_tmpl = str(rss_20(searchList=[namespace]))
 write_if_changed(os.path.join(blog_root, "rss_20.xml"), rss_tmpl)
+
+for item in items:
+    item.excerpt = None
+
+atom_tmpl = str(atom_10(searchList=[namespace]))
+write_if_changed(os.path.join(blog_root, "atom_10_titles.xml"), atom_tmpl)
+rss_tmpl = str(rss_20(searchList=[namespace]))
+write_if_changed(os.path.join(blog_root, "rss_20_titles.xml"), rss_tmpl)
+
+for item in items:
+    item.content = item.body
+
+atom_tmpl = str(atom_10(searchList=[namespace]))
+write_if_changed(os.path.join(blog_root, "atom_10_full.xml"), atom_tmpl)
+rss_tmpl = str(rss_20(searchList=[namespace]))
+write_if_changed(os.path.join(blog_root, "rss_20_full.xml"), rss_tmpl)