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