#!/usr/bin/env python
# encoding: utf-8
"""
generate-blog.py

Created by Vassilios Karakoidas on 2007-09-14.
"""

# import statements
from xml.dom import minidom
import re
import sys

############### statics ###################

header = '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Vassilios Karakoidas Weblog</title>
<link href="../images/styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="content">
	<h1 class="logo">Vassilios Karakoidas Weblog </h1>
'''

footer = '''
	<p class="logo">Referenced by  ... (weblogs)</p>
	<ul>
		<li><a href="http://www.spinellis.gr/blog/20061013/index.html">Research on Domain-Specific Languages</a> (2006.10.13, Diomidis Spinellis)</li>
		<li><a href="http://istlab.dmst.aueb.gr/~vbill/blog.html">On Window Managers</a> (2005.10.23, Vasileios Vlachos)</li>
		<li><a href="http://www.spinellis.gr/blog/20050413/index.html">A Pipe Namespace in the Portal Filesystem</a> (2005.04.13, Diomidis Spinellis)</li>
		<li><a href="http://www.spinellis.gr/blog/20050218/index.html">XML Versus Text Files</a> (2005.02.18, Diomidis Spinellis)</li>
		<li><a href="http://www.spinellis.gr/blog/20050215/index.html">The Efficiency of Java and C++, Revisited</a> (2005.02.15, Diomidis Spinellis)</li>
		<li> <a href="http://www.spinellis.gr/blog/20040925/index.html">A Survey of Language Popularity</a> (2004.09.25, Diomidis Spinellis) </li>
	</ul>
	<small>## generated by <a href="../programs/misc/index.html#blog-gen">generate-blog.py</a> ##</small><br/><br/>
</div>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script>
<script type="text/javascript">
	_uacct = "UA-1844074-1";
	urchinTracker();
</script>
</body>
</html>
'''

rss_header = '''<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE rss [<!ENTITY % HTMLlat1 PUBLIC "-//W3C//ENTITIES Latin 1 for XHTML//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent">]>
<!-- $Id: weblog-rss.xml 604 2007-10-10 08:39:45Z bkarak $ -->
<!-- tags: Personal, Hobbies, Software, Computers, SQO-OSS -->
<rss version="0.92" xml:base="http://gaijin.dmst.aueb.gr/~bkarak/weblog">
<channel>
	<title>Vassilios Karakoidas Weblog</title>
	<link>http://gaijin.dmst.aueb.gr/~bkarak/weblog</link>
	<description>Vassilios Karakoidas Weblog</description>
	<language>en</language>
'''

rss_footer = '''</channel>
</rss>
'''

##### program definitions

class RssItem:
	category = ''
	title = ''
	url = ''
	description = ''
	date = ''
	xml = ''
	
	def add_title(self, text):
		pat = re.compile("([/?&!0-9'a-zA-Z .-]*)\((.*)\)")
		mo = pat.search(text)
		self.title = mo.group(1).strip()
		self.category = mo.group(2).strip()
	
	def to_xml(self):
		return self.xml

##
# main program
##

rssItems = []
categories = []
rssFile = sys.argv[1]

# do the parsing
rssdoc = minidom.parse(rssFile)
elements = rssdoc.getElementsByTagName('item')

for node in elements:
	item = RssItem()
	for child in node.childNodes:
		if child.nodeName == 'title':
			item.add_title(child.firstChild.nodeValue)
		elif child.nodeName == 'link':
			item.url = child.firstChild.nodeValue
		elif child.nodeName == 'description':
			item.description = child.firstChild.nodeValue
		elif child.nodeName == 'pubDate':
			item.date = child.firstChild.nodeValue
	item.xml = node.toxml()
	rssItems.append(item)

for rssItem in rssItems:
	try:
		categories.index(rssItem.category)
	except ValueError:
		categories.append(rssItem.category)

# open the file
output_file = open("index.html","w")

# write the header
output_file.writelines(header)

# create the anchors
output_file.writelines('<p class="logo">Available Categories</p>');
output_file.writelines('<ul>');

for categ in categories:
	output_file.writelines('<li><a href="#' + categ +'">' + categ + '</a></li>');

output_file.writelines('</ul>');

# create rss categories and write rss files
for categ in categories:
	rss_file = open("rss/" + categ.lower() + "-rss.xml","w");
	rss_file.writelines(rss_header);
	for rssItem in rssItems:
		if rssItem.category == categ:
			rss_file.writelines(rssItem.to_xml())
	rss_file.writelines(rss_footer);
	rss_file.close();

output_file.writelines('<p class="logo">\n')
output_file.writelines('Availlable RSS Feeds <img src="../images/xml.gif" width="36" height="14" border="0" align="absmiddle"/> <br/>\n')
output_file.writelines('<ul>\n')
output_file.writelines('<li><a href="' + rssFile + '">All</a></li>\n')

for categ in categories:
	output_file.writelines('<li><a href="rss/' + categ.lower() + '-rss.xml">' + categ + '</a></li>')

output_file.writelines('</ul>\n<hr/><hr/>')

output_file.writelines('<h1 class="logo">Blog Entries</h1>')

# create the actual list
for categ in categories:
	output_file.writelines('<p class="logo">' + categ + '</p>')
	output_file.writelines('<a name="' + categ + '"/>');
	output_file.writelines('<ul>')
	for rssItem in rssItems:
		if rssItem.category == categ:
			output_file.writelines('<li><a href="'+ rssItem.url + '">' + rssItem.date + ' - ' + rssItem.title + '</a></li>')
	output_file.writelines('</ul>')

# write the footer and close the files
output_file.writelines(footer)
output_file.close()