]> git.phdru.name Git - phdru.name/phdru.name.git/blob - reindex_blog.py
Added keywords. Made write_template() to test if the template was changed.
[phdru.name/phdru.name.git] / reindex_blog.py
1 #! /usr/local/bin/python -O
2 # -*- coding: koi8-r -*-
3
4 __version__ = "$Revision$"[11:-2]
5 __revision__ = "$Id$"[5:-2]
6 __date__ = "$Date$"[7:-2]
7 __author__ = "Oleg BroytMann <phd@phd.pp.ru>"
8 __copyright__ = "Copyright (C) 2006 PhiloSoft Design"
9
10 import sys, os
11 from glob import glob
12
13 try:
14    import cPickle as pickle
15 except ImportError:
16    import pickle
17
18 from Cheetah.Template import Template
19
20 blog_filename = sys.argv[1]
21 try:
22    blog_file = open(blog_filename, "rb")
23 except IOError:
24    blog = {}
25 else:
26    blog = pickle.load(blog_file)
27    blog_file.close()
28
29 years = {}
30 months = sys.argv[2:]
31 isdir = os.path.isdir
32
33 if not months:
34    for year in os.listdir(os.curdir):
35       if isdir(year):
36          years[year] = {}
37          for month in os.listdir(year):
38             m = os.path.join(year, month)
39             if isdir(m):
40                months.append(m)
41
42 days = []
43 for month in months:
44    year, m = month.split(os.sep)
45    if month not in years:
46       years[year] = {}
47    years[year][m] = days_of_month = []
48    for day in os.listdir(month):
49          d = os.path.join(month, day)
50          if isdir(d):
51             days.append(d)
52             days_of_month.append(d)
53
54 for day in days:
55    for tmpl in glob(os.path.join(day, "*.tmpl")):
56       template = Template(file=tmpl)
57       title_parts = template.Title.split()
58       title = ' '.join(title_parts[6:])
59       lead = getattr(template, "Lead", None)
60
61       if title:
62          day_parts = day.split(os.sep)
63          blog[tuple(day_parts)] = (title, os.path.basename(tmpl), lead)
64
65 blog_file = open(blog_filename, "wb")
66 pickle.dump(blog, blog_file, pickle.HIGHEST_PROTOCOL)
67 blog_file.close()
68
69
70 import locale
71 locale.setlocale(locale.LC_ALL, '')
72 from calendar import _localized_day, _localized_month
73
74 locale.setlocale(locale.LC_TIME, 'C')
75 months_names_en = list(_localized_month('%B'))
76 months_abbrs_en = list(_localized_month('%b'))
77
78 locale.setlocale(locale.LC_TIME, '')
79 months_names_ru = [month.lower() for month in _localized_month('%B')]
80
81 months_names_ru0 = ['', "январь", "февраль", "март", "апрель", "май", "июнь",
82    "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь"
83 ]
84
85
86 def write_template(year, month, titles):
87    index_name = os.path.join(year, month, "index.tmpl")
88    try:
89       index_tmpl = open(index_name, 'r')
90       old_text = index_tmpl.read()
91       index_tmpl.close()
92    except IOError:
93       old_text = None
94
95    new_text = []
96    show_year = not year
97    show_month = not month
98
99    if show_year:
100       new_text.append("""\
101 #extends phd_pp_ru
102 #implements respond
103 #attr $Title = "Oleg BroytMann's blog"
104 #attr $Description = "BroytMann Russian Blog Index Document"
105 #attr $Copyright = %(cyear)s
106 ##
107 #def body_html
108 <H1>Журнал</H1>
109 """ % {"cyear": year or 2005})
110
111    elif show_month:
112
113       new_text.append("""\
114 #extends phd_pp_ru
115 #implements respond
116 #attr $Title = "Oleg BroytMann's blog: %(year)s"
117 #attr $Description = "BroytMann Russian Blog %(year)s Index Document"
118 #attr $Copyright = %(cyear)s
119 ##
120 #def body_html
121 <H1>Журнал: %(year)s</H1>
122 """ % {"year": year, "cyear": year or 2005})
123
124    else:
125
126       month = int(month)
127       new_text.append("""\
128 #extends phd_pp_ru
129 #implements respond
130 #attr $Title = "Oleg BroytMann's blog: %(month_abbr_en)s %(year)s"
131 #attr $Description = "BroytMann Russian Blog %(month_name_en)s %(year)s Index Document"
132 #attr $Copyright = %(cyear)s
133 ##
134 #def body_html
135 <H1>Журнал: %(month_name_ru0)s %(year)s</H1>
136 """ % {
137       "year": year, "cyear": year or 2005,
138       "month_abbr_en": months_abbrs_en[month], "month_name_en": months_names_en[month],
139       "month_name_ru0": months_names_ru0[month],
140    })
141
142    save_titles = titles[:]
143    titles.reverse()
144
145    save_day = None
146    for key, tmpl, title, lead in titles:
147       year, month, day = key
148       href = []
149       if show_year:
150          href.append(year)
151       if show_month:
152          href.append(month)
153       href.append(day)
154       href.append(tmpl)
155       href = '/'.join(href)
156       if day[0] == '0': day = day[1:]
157       if save_day <> day:
158          if show_year:
159             new_text.append('\n<h2>%s %s %s</h2>' % (day, months_names_ru[int(month)], year))
160          else:
161             new_text.append('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
162          save_day = day
163       if lead:
164          lead = lead + ' '
165       else:
166          lead = ''
167       new_text.append('''
168 <p class="head">
169    %s<a href="%s">%s</a>.
170 </p>
171 ''' % (lead, href, title))
172
173    if show_year:
174       years = {}
175       for key, tmpl, title, lead in save_titles:
176          year, month, day = key
177          years[year] = True
178       first_year = True
179       new_text.append('''
180 <hr>
181
182 <p class="years">
183 ''')
184       for year in sorted(years.keys()):
185          if first_year:
186             first_year = False
187          else:
188             new_text.append(' - ')
189          new_text.append('<a href="%s/">%s</a>' % (year, year))
190       new_text.append('''
191 </p>
192 ''')
193
194    new_text.append("""\
195 #end def
196 $phd_pp_ru.respond(self)
197 """)
198
199    new_text = ''.join(new_text)
200    if old_text <> new_text:
201       print "Writing", index_name
202       index_tmpl = open(index_name, 'w')
203       index_tmpl.write(new_test)
204       index_tmpl.close()
205
206
207 def translate(tmpl):
208    if tmpl == "index.tmpl": tmpl = ''
209    if tmpl.endswith(".tmpl"): tmpl = tmpl[:-len("tmpl")] + "html"
210    return tmpl
211
212
213 all_titles = []
214 for key in sorted(blog.keys()):
215    title, tmpl, lead = blog[key]
216    all_titles.append((key, translate(tmpl), title, lead))
217 all_titles = all_titles[-20:]
218
219 for year in sorted(years.keys()):
220    year_titles = []
221    months = years[year]
222    for month in sorted(months.keys()):
223       month_titles = []
224       for day in sorted(months[month]):
225          day_parts = day.split(os.sep)
226          key = tuple(day_parts)
227          if key in blog:
228             title, tmpl, lead = blog[key]
229             tmpl = translate(tmpl)
230             year_titles.append((key, tmpl, title, lead))
231             month_titles.append((key, tmpl, title, lead))
232       write_template(year, month, month_titles)
233    write_template(year, '', year_titles)
234 write_template('', '', all_titles)