X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=reindex_blog.py;h=beba7c12823ae151a8f5de6619c9776758b9d713;hb=6922322a54539e39cbba59653868fa9d749fa274;hp=fd82c1839111ae59ddcd7a7e86eace33d7b9a715;hpb=d68f2dd8fdab1ec22049d14f274ec34a2ed8431f;p=phdru.name%2Fphdru.name.git diff --git a/reindex_blog.py b/reindex_blog.py index fd82c18..beba7c1 100755 --- a/reindex_blog.py +++ b/reindex_blog.py @@ -39,9 +39,8 @@ else: blog = {} years = {} -# excerpts nd bodies are dictionaries mapping file => excerpt/body +# bodies is a dictionary mapping file => body -excerpts = {} bodies = {} # Walk the directory recursively @@ -56,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): @@ -84,8 +83,7 @@ for dirpath, dirs, files in os.walk(blog_root): file = file[:-len("tmpl")] + "html" key = (year, month, day, file) - excerpts[key] = template.get_first_p() - bodies[key] = template.Body + bodies[key] = template.body() # Need to save the blog? if blog <> old_blog: @@ -220,15 +218,11 @@ def write_template(level, year, month, day, titles, tags=None): else: new_text.append('\n

%s %s

' % (day, months_names_ru[int(month)])) save_date = year, month, day - if lead: - lead = lead + ' ' - else: - lead = '' new_text.append('''

%s%s.

-''' % (lead, href, title)) +''' % (lead+' ' if lead else '', href, title)) if level == 0: new_text.append(""" @@ -407,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 = """
  • %s/%s/%s: %s%s
  • """ % (link, year, month, day, lead, title) + item_text = """
  • %s/%s/%s: %s%s
  • """ % (link, year, month, day, lead+' ' if lead else '', title) count += 1 if count <= 5: @@ -437,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 = '

    ' + + def end_p(self): + self.first_p = self.accumulator + '

    ' + 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 += '