]> git.phdru.name Git - phdru.name/phdru.name.git/blob - reindex_blog.py
Allow a few blog entries for one day.
[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
11 import sys, os
12
13 try:
14    import cPickle as pickle
15 except ImportError:
16    import pickle
17
18 from Cheetah.Template import Template
19
20
21 # Load old blog
22
23 blog_filename = sys.argv[1]
24 try:
25    blog_file = open(blog_filename, "rb")
26 except IOError:
27    old_blog = {}
28 else:
29    old_blog = pickle.load(blog_file)
30    blog_file.close()
31
32
33 # blog is a dictionary mapping (year, month, day) => (filename, title, lead)
34 blog = {}
35 years = {}
36
37 # Walk the directory recursively
38 for dirpath, dirs, files in os.walk(os.curdir):
39    for file in files:
40       # Ignore index.tmpl and *.html files; supose all other files are *.tmpl
41       if file == "index.tmpl" or file.endswith(".html"):
42          continue
43       fullpath = os.path.join(dirpath, file)
44       template = Template(file=fullpath)
45       title_parts = template.Title.split()
46       title = ' '.join(title_parts[6:])
47       lead = getattr(template, "Lead", None)
48
49       if title:
50          key = year, month, day = tuple(dirpath.split(os.sep)[1:])
51          if key in blog:
52             days = blog[key]
53          else:
54             days = blog[key] = []
55          days.append((file, title, lead))
56
57          if year in years:
58             months = years[year]
59          else:
60             months = years[year] = {}
61
62          if month in months:
63             days = months[month]
64          else:
65             days = months[month] = []
66
67          if day not in days: days.append(day)
68
69
70 # Need to save the blog?
71 if blog <> old_blog:
72    blog_file = open(blog_filename, "wb")
73    pickle.dump(blog, blog_file, pickle.HIGHEST_PROTOCOL)
74    blog_file.close()
75
76
77 # Localized month names
78
79 import locale
80 locale.setlocale(locale.LC_ALL, '')
81 from calendar import _localized_day, _localized_month
82
83 locale.setlocale(locale.LC_TIME, 'C')
84 months_names_en = list(_localized_month('%B'))
85 months_abbrs_en = list(_localized_month('%b'))
86
87 locale.setlocale(locale.LC_TIME, '')
88 months_names_ru = [month.lower() for month in _localized_month('%B')]
89
90 months_names_ru0 = ['', "январь", "февраль", "март", "апрель", "май", "июнь",
91    "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь"
92 ]
93
94
95 def write_template(year, month, day, titles):
96    index_name = os.path.join(year, month, day, "index.tmpl")
97    try:
98       index_tmpl = open(index_name, 'r')
99       old_text = index_tmpl.read()
100       index_tmpl.close()
101    except IOError:
102       old_text = None
103
104    new_text = []
105    show_year = not year
106    show_month = not month
107    show_day = not day
108
109    new_text.append("""\
110 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
111 #extends phd_pp_ru
112 #implements respond
113 """)
114
115    if show_year:
116       new_text.append("""\
117 #attr $Title = "Oleg BroytMann's blog"
118 #attr $Description = "BroytMann Russian Blog Index Document"
119 #attr $Copyright = %(cyear)s
120 ##
121 #def body_html
122 <H1>Журнал</H1>
123 """ % {"cyear": year or 2005})
124
125    elif show_month:
126
127       new_text.append("""\
128 #attr $Title = "Oleg BroytMann's blog: %(year)s"
129 #attr $Description = "BroytMann Russian Blog %(year)s Index Document"
130 #attr $Copyright = %(cyear)s
131 ##
132 #def body_html
133 <H1>Журнал: %(year)s</H1>
134 """ % {"year": year, "cyear": year or 2005})
135
136    elif show_day:
137
138       month = int(month)
139       new_text.append("""\
140 #attr $Title = "Oleg BroytMann's blog: %(month_abbr_en)s %(year)s"
141 #attr $Description = "BroytMann Russian Blog %(month_name_en)s %(year)s Index Document"
142 #attr $Copyright = %(cyear)s
143 ##
144 #def body_html
145 <H1>Журнал: %(month_name_ru0)s %(year)s</H1>
146 """ % {
147       "year": year, "cyear": year or 2005,
148       "month_abbr_en": months_abbrs_en[month], "month_name_en": months_names_en[month],
149       "month_name_ru0": months_names_ru0[month],
150    })
151
152    else:
153
154       day = int(day)
155       month = int(month)
156
157       new_text.append("""\
158 #attr $Title = "Oleg BroytMann's blog: %(day)d %(month_abbr_en)s %(year)s"
159 #attr $Description = "BroytMann Russian Blog %(day)d %(month_name_en)s %(year)s Index Document"
160 #attr $Copyright = %(cyear)s
161 ##
162 #def body_html
163 <H1>Журнал: %(day)d %(month_name_ru0)s %(year)s</H1>
164 """ % {
165       "year": year, "cyear": year or 2005,
166       "month_abbr_en": months_abbrs_en[month], "month_name_en": months_names_en[month],
167       "month_name_ru0": months_names_ru0[month],
168       "day": day
169    })
170
171    save_titles = titles[:]
172    titles.reverse()
173
174    save_day = None
175    for key, tmpl, title, lead in titles:
176       year, month, day = key
177       href = []
178       if show_year:
179          href.append(year)
180       if show_month:
181          href.append(month)
182       if show_day:
183          href.append(day)
184       href.append(tmpl)
185       href = '/'.join(href)
186       if day[0] == '0': day = day[1:]
187       if save_day <> day:
188          if show_year:
189             new_text.append('\n<h2>%s %s %s</h2>' % (day, months_names_ru[int(month)], year))
190          else:
191             new_text.append('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
192          save_day = day
193       if lead:
194          lead = lead + ' '
195       else:
196          lead = ''
197       new_text.append('''
198 <p class="head">
199    %s<a href="%s">%s</a>.
200 </p>
201 ''' % (lead, href, title))
202
203    if show_year:
204       years = {}
205       for key, tmpl, title, lead in save_titles:
206          year, month, day = key
207          years[year] = True
208       first_year = True
209       new_text.append('''
210 <hr>
211
212 <p class="years">
213 ''')
214       for year in sorted(years.keys()):
215          if first_year:
216             first_year = False
217          else:
218             new_text.append(' - ')
219          new_text.append('<a href="%s/">%s</a>' % (year, year))
220       new_text.append('''
221 </p>
222 ''')
223
224    new_text.append("""\
225 #end def
226 $phd_pp_ru.respond(self)
227 """)
228
229    new_text = ''.join(new_text)
230    if old_text <> new_text:
231       print "Writing", index_name
232       index_tmpl = open(index_name, 'w')
233       index_tmpl.write(new_text)
234       index_tmpl.close()
235
236
237 all_titles = []
238 for year in sorted(years.keys()):
239    year_titles = []
240    months = years[year]
241    for month in sorted(months.keys()):
242       month_titles = []
243       for day in sorted(months[month]):
244          day_titles = []
245          key = year, month, day
246          if key in blog:
247             for tmpl, title, lead in blog[key]:
248                if tmpl.endswith(".tmpl"): tmpl = tmpl[:-len("tmpl")] + "html"
249                all_titles.append((key, tmpl, title, lead))
250                year_titles.append((key, tmpl, title, lead))
251                month_titles.append((key, tmpl, title, lead))
252                day_titles.append((key, tmpl, title, lead))
253          write_template(year, month, day, day_titles)
254       write_template(year, month, '', month_titles)
255    write_template(year, '', '', year_titles)
256 write_template('', '', '', all_titles[-20:])