1 #! /usr/local/bin/python -O
2 # -*- coding: koi8-r -*-
8 import cPickle as pickle
12 from Cheetah.Template import Template
14 blog_filename = sys.argv[1]
16 blog_file = open(blog_filename, "rb")
20 blog = pickle.load(blog_file)
28 for year in os.listdir(os.curdir):
31 for month in os.listdir(year):
32 m = os.path.join(year, month)
38 year, m = month.split(os.sep)
39 if month not in years:
41 years[year][m] = days_of_month = []
42 for day in os.listdir(month):
43 d = os.path.join(month, day)
46 days_of_month.append(d)
49 for tmpl in glob(os.path.join(day, "*.tmpl")):
50 template = Template(file=tmpl)
51 title_parts = template.Title.split()
52 title = ' '.join(title_parts[6:])
53 lead = getattr(template, "Lead", None)
56 day_parts = day.split(os.sep)
57 blog[tuple(day_parts)] = (title, os.path.basename(tmpl), lead)
59 blog_file = open(blog_filename, "wb")
60 pickle.dump(blog, blog_file, pickle.HIGHEST_PROTOCOL)
65 locale.setlocale(locale.LC_ALL, '')
66 from calendar import _localized_day, _localized_month
68 locale.setlocale(locale.LC_TIME, 'C')
69 months_names_en = list(_localized_month('%B'))
70 months_abbrs_en = list(_localized_month('%b'))
72 locale.setlocale(locale.LC_TIME, '')
73 months_names_ru = [month.lower() for month in _localized_month('%B')]
75 months_names_ru0 = ['', "январь", "февраль", "март", "апрель", "май", "июнь",
76 "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь"
80 def write_template(show_year, show_month, year, month, titles, cut=None, show_years=False):
81 index_tmpl = open(os.path.join(year, month, "index.tmpl"), 'w')
86 #attr $Title = "Oleg BroytMann's blog"
87 #attr $Description = "BroytMann Russian Blog Index Document"
88 #attr $Copyright = %(cyear)s
92 """ % {"cyear": year or 2005})
99 #attr $Title = "Oleg BroytMann's blog: %(year)s"
100 #attr $Description = "BroytMann Russian Blog %(year)s Index Document"
101 #attr $Copyright = %(cyear)s
104 <H1>Журнал: %(year)s</H1>
105 """ % {"year": year, "cyear": year or 2005})
110 index_tmpl.write("""\
113 #attr $Title = "Oleg BroytMann's blog: %(month_abbr_en)s %(year)s"
114 #attr $Description = "BroytMann Russian Blog %(month_name_en)s %(year)s Index Document"
115 #attr $Copyright = %(cyear)s
118 <H1>Журнал: %(month_name_ru0)s %(year)s</H1>
120 "year": year, "cyear": year or 2005,
121 "month_abbr_en": months_abbrs_en[month], "month_name_en": months_names_en[month],
122 "month_name_ru0": months_names_ru0[month],
125 save_titles = titles[:]
128 titles = titles[:cut]
131 for key, tmpl, title, lead in titles:
132 year, month, day = key
140 href = '/'.join(href)
141 if day[0] == '0': day = day[1:]
144 index_tmpl.write('\n<h2>%s %s %s</h2>' % (day, months_names_ru[int(month)], year))
146 index_tmpl.write('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
154 %s<a href="%s">%s</a>.
156 ''' % (lead, href, title))
160 for key, tmpl, title, lead in save_titles:
161 year, month, day = key
169 for year in sorted(years.keys()):
173 index_tmpl.write(' - ')
174 index_tmpl.write('<a href="%s/">%s</a>' % (year, year))
179 index_tmpl.write("""\
181 $phd_pp_ru.respond(self)
187 if tmpl == "index.tmpl": tmpl = ''
188 if tmpl.endswith(".tmpl"): tmpl = tmpl[:-len("tmpl")] + "html"
193 for key in sorted(blog.keys()):
194 title, tmpl, lead = blog[key]
195 all_titles.append((key, translate(tmpl), title, lead))
197 for year in sorted(years.keys()):
200 for month in sorted(months.keys()):
202 for day in sorted(months[month]):
203 day_parts = day.split(os.sep)
204 key = tuple(day_parts)
206 title, tmpl, lead = blog[key]
207 tmpl = translate(tmpl)
208 year_titles.append((key, tmpl, title, lead))
209 month_titles.append((key, tmpl, title, lead))
210 write_template(False, False, year, month, month_titles)
211 write_template(False, True, year, '', year_titles)
212 write_template(True, True, '', '', all_titles, 5, True)