1 #! /usr/bin/env python3
2 # -*- coding: koi8-r -*-
4 __author__ = "Oleg Broytman <phd@phdru.name>"
5 __copyright__ = "Copyright (C) 2006-2024 PhiloSoft Design"
7 from calendar import _localized_month
8 from html import escape
11 from urllib.parse import quote, urljoin
13 from Cheetah.Template import Template
14 from Cheetah.compat import string_type
15 from m_lib.net.www.html import HTMLParser as _HTMLParser
17 from atom_10 import atom_10
18 from blog_db import blog_root, load_blog, save_blog
19 from news import NewsItem, write_if_changed
20 from rss_20 import rss_20
23 old_blog = load_blog()
25 # blog is a dictionary mapping
26 # (year, month, day) => [list of (file, title, lead, tags)]
31 # bodies is a dictionary mapping file => body
35 # Walk the directory recursively
36 for dirpath, dirs, files in os.walk(blog_root):
37 d = os.path.basename(dirpath)
38 if not d.startswith("20") and not d.isdigit():
41 if not file.endswith(".tmpl"):
43 fullpath = os.path.join(dirpath, file)
44 template = Template(file=fullpath)
45 title_parts = template.Title.split()
46 title = ' '.join(title_parts[6:])
50 if isinstance(tags, string_type):
54 key = year, month, day = \
55 tuple(dirpath[len(blog_root):].split(os.sep)[1:])
60 days.append((file, title, lead, tags))
65 months = years[year] = {}
70 days = months[month] = []
72 if day not in days: days.append(day)
74 file = file[:-len("tmpl")] + "html"
75 key = (year, month, day, file)
76 body = template.body()
79 # Need to save the blog?
83 # Localized month names
84 locale.setlocale(locale.LC_ALL, "ru_RU.KOI8-R")
86 locale.setlocale(locale.LC_TIME, 'C')
87 months_names_en = list(_localized_month('%B'))
88 months_abbrs_en = list(_localized_month('%b'))
90 locale.setlocale(locale.LC_TIME, "ru_RU.KOI8-R")
91 # months_names_ru = list(_localized_month('%B'))
94 '', "������", "�������", "�����", "������", "���", "����",
95 "����", "�������", "��������", "�������", "������", "�������"
99 '', "������", "�������", "����", "������", "���", "����",
100 "����", "������", "��������", "�������", "������", "�������"
105 return quote(tag.replace(' ', '_'), encoding='koi8-r')
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.
128 #attr $Title = "Oleg Broytman's blog"
129 #attr $Description = "Broytman Russian Blog Index Document"
130 #attr $Copyright = %(cyear)s
131 #attr $alternates = (("������� [Atom 1.0] ������ ���������", "application/atom+xml", "atom_10_titles.xml"),
132 ("������� [Atom 1.0]", "application/atom+xml", "atom_10.xml"),
133 ("������� [Atom 1.0] ������ ������", "application/atom+xml", "atom_10_full.xml"),
134 ("������� [RSS 2.0] ������ ���������", "application/rss+xml", "rss_20_titles.xml"),
135 ("������� [RSS 2.0]", "application/rss+xml", "rss_20.xml"),
136 ("������� [RSS 2.0] ������ ������", "application/rss+xml", "rss_20_full.xml"),
141 """ % {"cyear": year or 2005})
145 #attr $Title = "Oleg Broytman's blog: %(year)s"
146 #attr $Description = "Broytman Russian Blog %(year)s Index Document"
147 #attr $Copyright = %(cyear)s
150 <h1>������: %(year)s</h1>
151 """ % {"year": year, "cyear": year or 2005})
156 #attr $Title = "Oleg Broytman's blog: %(month_abbr_en)s %(year)s"
157 #attr $Description = "Broytman Russian Blog %(month_name_en)s %(year)s Index Document"
158 #attr $Copyright = %(cyear)s
161 <h1>������: %(month_name_ru0)s %(year)s</h1>
163 "year": year, "cyear": year or 2005,
164 "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
165 "month_name_ru0": months_names_ru0[imonth],
174 #attr $Refresh = "0; URL=%s"
178 #attr $Title = "Oleg Broytman's blog: %(day)d %(month_abbr_en)s %(year)s"
179 #attr $Description = "Broytman Russian Blog %(day)d %(month_name_en)s %(year)s Index Document"
180 #attr $Copyright = %(cyear)s
183 <h1>������: %(day)d %(month_name_ru)s %(year)s</h1>
185 "year": year, "cyear": year or 2005,
186 "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
187 "month_name_ru": months_names_ru[imonth],
191 save_titles = titles[:]
195 for year, month, day, file, title, lead in titles:
204 href = '/'.join(href)
205 if day[0] == '0': day = day[1:]
206 if save_date != (year, month, day):
208 new_text.append('\n<h2>%s %s %s</h2>' % (day, months_names_ru[int(month)], year))
210 new_text.append('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
211 save_date = year, month, day
214 %s<a href="%s">%s</a>.
216 ''' % (lead+' ' if lead else '', href, title))
222 <p class="head">���������� ����� � ��������
223 <img src="../../Graphics/atom_10.jpg" border=0>
224 <A HREF="atom_10_titles.xml">Atom 1.0 ������ ���������</A> /
225 <A HREF="atom_10.xml">Atom 1.0</A> /
226 <A HREF="atom_10_full.xml">Atom 1.0 ������ ������</A>
227 � <img src="../../Graphics/rss_20.jpg" border=0>
228 <A HREF="rss_20_titles.xml">RSS 2.0 ������ ���������</A> /
229 <A HREF="rss_20.xml">RSS 2.0</A> /
230 <A HREF="rss_20_full.xml">RSS 2.0 ������ ������</A>.
235 for year, month, day, file, title, lead in save_titles:
238 <p class="head"><a href="tags/">����</a>:
241 for count, tag, links in all_tags:
245 new_text.append(' - ')
246 new_text.append("""<a href="tags/%s.html">%s (%d)</a>""" % (
247 encode_tag(tag), tag, count))
252 max_year = int(sorted(years.keys())[-1])
253 years = range(max_year, 2005, -1)
256 <p class="head">�� �����:
260 for year, month, day, file, title, lead in all_titles:
261 year_counts[year] = 0
262 for year, month, day, file, title, lead in all_titles:
263 year_counts[year] += 1
270 new_text.append(' - ')
271 new_text.append('<a href="%s/">%s (%d)</a>' % (year, year, year_counts[str(year)]))
278 <p class="head"><a href="https://phd-ru.dreamwidth.org/">DW</a> /
279 <a href="http://phd.livejournal.com/">��</a></p>
284 $phd_site.respond(self)
287 write_if_changed(index_name, ''.join(new_text))
294 for year in sorted(years.keys()):
297 for month in sorted(months.keys()):
299 for day in sorted(months[month]):
301 key = year, month, day
303 for file, title, lead, tags in blog[key]:
304 if file.endswith(".tmpl"): file = file[:-len("tmpl")] + "html"
305 value = (year, month, day, file, title, lead)
306 all_titles_tags.append((year, month, day, file, title, lead, tags))
307 all_titles.append(value)
308 year_titles.append(value)
309 month_titles.append(value)
310 day_titles.append(value)
313 tag_links = all_tags[tag]
315 tag_links = all_tags[tag] = []
316 tag_links.append(value)
317 write_template(3, year, month, day, day_titles)
318 write_template(2, year, month, day, month_titles)
319 write_template(1, year, month, day, year_titles)
321 def by_count_rev_tag_link(tag):
322 """Sort all_tags by count in descending order,
323 and by tags and links in ascending order
325 return -tag[0], tag[1], tag[2]
327 all_tags = [(len(links), tag, links) for (tag, links) in all_tags.items()]
328 all_tags.sort(key=by_count_rev_tag_link)
330 write_template(0, year, month, day, all_titles[-20:], all_tags)
333 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
337 #attr $Title = "Oleg Broytman's blog: tags"
338 #attr $Description = "Broytman Russian Blog Tags Index Document"
339 #attr $Copyright = 2006
344 <p class="head small">
345 ����� ������ ��������� ������ ��������� � �����, ��������������� ���������.
346 ��������� ���������:</p>
348 <li>��� - ���� ����� ��� ����������, �������ģ� ��������������� �� �������� ����.</li>
349 <li>�������� '!' (NOT, not, ��, ��) - ���� ������, � ������� ��� ����� ����.</li>
350 <li>�������� '&' (AND, and, �, �) - ���� ������, � ������� ���� ��� ����.</li>
351 <li>�������� '|' (OR, or, ���, ���) - ���� ������, � ������� ���� ����� �� �����.</li>
352 <li>��������� �������� �����������: NOT > AND > OR. ������ '()' ��������� ���������� ���������.</li>
353 <li>����� ���������� �������: ���� Linux � linux ������������.</li>
356 ������� ���������: linux - �������ģ� ���������������
357 �� �������� Linux.html; linux&!debian - ������ ������ � ������� ���� ���
358 Linux � ��� ���� Debian; Linux and not Debian - �� �� �����. ���� � ���� ����
359 ������ ("������ ����", "����� ����") - ��� ���� �������� �� ���ޣ��������;
360 ��������: "������� � �� ͣ�����_����", "������ � �� ޣ����_����".
364 <form method=GET action="../../../cgi-bin/blog-ru/search-tags.cgi">
365 <input type=text name=q>
366 <input type=submit name=submit value="������">
373 for i, (count, tag, links) in enumerate(all_tags):
375 <dt><a href="%s.html">%s (%d)</a></dt>
376 """ % (encode_tag(tag), tag, count))
378 first = all_tags[0][1]
382 prev = all_tags[i-1][1]
383 if i >= len(all_tags)-1:
386 next = all_tags[i+1][1]
387 last = all_tags[-1][1]
390 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
394 #attr $Title = "Oleg Broytman's blog: tag %s"
395 #attr $Description = "Broytman Russian Blog Tag %s Index Document"
417 #attr $Copyright = 2006
426 for year, month, day, filename, title, lead in reversed(links):
427 link = "../%s/%s/%s/%s" % (year, month, day, filename)
428 item_text = """<li><a href="%s">%s/%s/%s: %s%s</a></li>""" % (link, year, month, day, lead+' ' if lead else '', title)
432 new_text.append(" <dd>%s</dd>\n" % item_text)
434 tag_text.append(" %s\n" % item_text)
439 $phd_site.respond(self)
441 write_if_changed(os.path.join(blog_root, "tags",
442 tag.replace(' ', '_') + ".tmpl"),
448 $phd_site.respond(self)
450 write_if_changed(os.path.join(blog_root, "tags", "index.tmpl"), ''.join(new_text))
453 class HTMLDone(Exception): pass
456 class FirstPHTMLParser(_HTMLParser):
458 _HTMLParser.__init__(self)
461 def start_p(self, attrs):
462 self.accumulator = '<p>'
465 self.first_p = self.accumulator + '</p>'
468 def get_first_p(body):
469 parser = FirstPHTMLParser()
481 return parser.first_p
484 class AbsURLHTMLParser(_HTMLParser):
485 def __init__(self, base):
486 _HTMLParser.__init__(self)
489 def start_a(self, attrs):
490 self.accumulator += '<a'
491 for attrname, value in attrs:
492 value = escape(value, True)
493 if attrname == 'href':
494 self.accumulator += ' href="%s"' % urljoin(self.base, value)
496 self.accumulator += ' %s="%s"' % (attrname, value)
497 self.accumulator += '>'
500 self.accumulator += '</a>'
502 def start_img(self, attrs):
503 self.accumulator += '<img'
504 for attrname, value in attrs:
505 value = escape(value, True)
506 if attrname == 'src':
507 self.accumulator += ' src="%s"' % urljoin(self.base, value)
509 self.accumulator += ' %s="%s"' % (attrname, value)
510 self.accumulator += '>'
515 def absolute_urls(body, base):
516 parser = AbsURLHTMLParser(base)
528 return parser.accumulator
532 blog_root_url = blog_root[
533 blog_root.find('/htdocs/phdru.name/') + len('/htdocs/phdru.name/'):]
534 baseURL = "https://phdru.name/%s/" % blog_root_url
536 baseURL = "https://phdru.name/"
539 for item in tuple(reversed(all_titles_tags))[:10]:
540 year, month, day, file, title, lead, tags = item
541 url_path = "%s/%s/%s/%s" % (year, month, day, file)
543 "%s-%s-%s" % (year, month, day),
544 "%s%s" % (lead+' ' if lead else '', title),
547 item.baseURL = baseURL
548 item.categoryList = tags
549 body = bodies[(year, month, day, file)]
550 body = absolute_urls(body, baseURL + url_path)
552 excerpt = get_first_p(body)
553 item.excerpt = excerpt
556 "title": "Oleg Broytman's blog",
561 "author": "Oleg Broytman",
562 "email": "phd@phdru.name",
563 "generator": os.path.basename(sys.argv[0]),
568 locale.setlocale(locale.LC_TIME, 'C')
570 atom_tmpl = atom_10(searchList=[namespace])
571 write_if_changed(os.path.join(blog_root, "atom_10.xml"), str(atom_tmpl))
572 rss_tmpl = rss_20(searchList=[namespace])
573 write_if_changed(os.path.join(blog_root, "rss_20.xml"), str(rss_tmpl))
578 atom_tmpl = atom_10(searchList=[namespace])
579 write_if_changed(os.path.join(blog_root, "atom_10_titles.xml"), str(atom_tmpl))
580 rss_tmpl = rss_20(searchList=[namespace])
581 write_if_changed(os.path.join(blog_root, "rss_20_titles.xml"), str(rss_tmpl))
584 item.content = item.body
586 atom_tmpl = atom_10(searchList=[namespace])
587 write_if_changed(os.path.join(blog_root, "atom_10_full.xml"), str(atom_tmpl))
588 rss_tmpl = rss_20(searchList=[namespace])
589 write_if_changed(os.path.join(blog_root, "rss_20_full.xml"), str(rss_tmpl))
591 # vim: set ts=8 sts=4 sw=4 et :