#!/usr/bin/env python
# Copyright (C) 2007 Matt Kraai
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# .
import datetime
import re
import sys
import textwrap
import blog
def xhtmlify(indent, tag, body, style=None):
if style == None:
style = ''
else:
style = ' style="%s"' % style
email = 'kraai@ftbfs.org'
for pattern, replacement in [('\n', ' '),
(email,
'[mailto:' + email + ' ' + email + ']'),
('\\[([^ ]+) ([^]]+)\\]',
'\\2'),
('&', '&'),
('---', '—'),
('--', '–')]:
body = re.sub(pattern, replacement, body)
unwrapped_result = '%s<%s%s>%s%s>' % (indent, tag, style, body, tag)
if len(unwrapped_result) <= 70:
return unwrapped_result + '\n'
wrapped_body = body.replace('-', u'\N{NON-BREAKING HYPHEN}')
wrapped_body = textwrap.fill(wrapped_body, initial_indent=indent + ' ',
subsequent_indent=indent + ' ',
break_long_words=False)
wrapped_body = wrapped_body.replace(u'\N{NON-BREAKING HYPHEN}', '-')
return '%s<%s%s>\n%s\n%s%s>\n' % (indent, tag, style, wrapped_body,
indent, tag)
def xhtmlify_file(text_file, header_level=1, header_link=None):
body = ''
paragraph = ''
list_item = ''
list_tag = None
for line in file(text_file).readlines():
if line != '\n':
if re.match(' [*#] ', line):
if list_item != '':
body += xhtmlify(' ', 'li', list_item)
list_item = ''
if list_tag == None:
if line[1] == '*':
list_tag = 'ul'
else:
list_tag = 'ol'
body += '\n <' + list_tag + '>\n'
else:
body += '\n'
list_item = line[2:-1]
elif list_tag != None:
list_item += ' ' + line[2:-1]
elif paragraph == '':
paragraph = line[:-1]
elif re.match('=+', line[:-1]):
if sys.argv[-1] != 'index.txt':
title = paragraph + ' - '
if header_link != None:
paragraph = '%s' % (header_link,
paragraph)
body += '\n' + xhtmlify(' ', 'h' + str(header_level),
paragraph)
paragraph = ''
elif re.match('-+', line[:-1]):
body += '\n' + xhtmlify(' ', 'h' + str(header_level + 1),
paragraph)
paragraph = ''
else:
paragraph += ' ' + line[:-1]
elif list_tag != None:
if list_item != '':
body += xhtmlify(' ', 'li', list_item)
list_item = ''
body += ' ' + list_tag + '>\n'
list_tag = None
elif paragraph != '':
body += '\n' + xhtmlify(' ', 'p', paragraph)
paragraph = ''
if list_tag != None:
if list_item != '':
body += xhtmlify(' ', 'li', list_item)
body += ' ' + list_tag + '>\n'
if paragraph != '':
body += '\n' + xhtmlify(' ', 'p', paragraph)
return body
if len(sys.argv) == 3 and sys.argv[1] == '--version=1.0':
header = '''\
'''
else:
header = '''\
'''
document_template = '''\
%s\
%sftbfs.org
\
%s
%s\
'''
footer_template = u'''\
This web page and its source code are licensed under a Creative \
Commons Attribution-Share Alike 3.0 License. The former was generated \
by Makefile and xhtmlify on \
%s.'''
text_file = sys.argv[-1]
title = ''
body = xhtmlify_file(text_file)
if text_file == 'index.txt':
if blog.entries != []:
body += '\n' + xhtmlify(' ', 'h2', 'Blog')
for entry in blog.entries:
body += xhtmlify_file(entry.path + '.txt', 3, entry.path)
now = datetime.datetime.now()
hour = now.hour % 12
if hour == 0:
hour = 12
date_and_time = now.strftime('%%A, %%B %d, %%Y at %d:%%M %%p'
% (now.day, hour))
footer = xhtmlify(' ', 'p', footer_template % (sys.argv[-1], date_and_time),
'font-size: smaller')
print document_template % (header, title, body, footer),