#!/usr/bin/python

from rdflib.graph import Graph
from rdflib.namespace import RDF, RDFS, OWL, XSD, Namespace

FOAF = Namespace("http://xmlns.com/foaf/0.1/")

class RDFaProfileGenerator():
	def __init__(self, title, infolink, graph, mainprefix, prefixes):
		self.title      = title
		self.infolink   = infolink
		self.graph      = graph
		self.mainprefix = mainprefix
		self.prefixes   = prefixes
	def outputHTML(self):
		return self._outputHTML_Header() + self._outputHTML_Classes() + self._outputHTML_Properties() + self._outputHTML_Prefixes() + self._outputHTML_Footer()
	def _outputHTML_Header(self):
		return '''<!DOCTYPE html
	PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN"
	"http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head profile="http://www.w3.org/1999/xhtml/vocab">
		<title>{title}</title>
		<style type="text/css">
			dt {{ font-weight: bold; }}
			dd {{ font-family: monospace; }}
		</style>
	</head>
	<body xmlns:dc="http://purl.org/dc/terms/" xmlns:rdfa="http://www.w3.org/ns/rdfa#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#">
		<h1 property="dc:title">{title}</h1>
		<p>For more information see
		&lt;<a rel="help" href="{infolink}">{infolink}</a>&gt;.</p>
			'''.format(title=self.title, infolink=self.infolink) + "\n"
	def _outputHTML_Footer(self):
		return '''
	</body>
</html>
			'''
	def _outputHTML_Classes(self):
		classes = {}
		for s, p, o in self.graph.triples((None, RDF["type"], RDFS["Class"])):
			if (s.startswith(self.mainprefix)):
				classes[ "%s" % s ] = s
		for s, p, o in self.graph.triples((None, RDF["type"], OWL["Class"])):
			if (s.startswith(self.mainprefix)):
				classes[ "%s" % s ] = s
		rv = "\t\t<h2>Classes</h2>\n" 
		rv += "\t\t<dl>\n"
		keys = classes.keys()
		keys.sort()
		for key in keys:
			rv += "\t\t\t<dt about=\"[_:term_{term}]\" property=\"rdfa:term\" datatype=\"xsd:NMTOKEN\">{term}</dt>\n".format(term=key[len("{0}".format(self.mainprefix)):])
			rv += "\t\t\t<dd about=\"[_:term_{term}]\" property=\"rdfa:uri\" datatype=\"xsd:anyURI\">{uri}</dd>\n".format(term=key[len("{0}".format(self.mainprefix)):], uri=key)
		rv += "\t\t</dl>\n";
		return rv
	def _outputHTML_Properties(self):
		props = {}
		for s, p, o in self.graph.triples((None, RDF["type"], RDF["Property"])):
			if (s.startswith(self.mainprefix)):
				props[ "%s" % s ] = s
		rv = "\t\t<h2>Properties</h2>\n" 
		rv += "\t\t<dl>\n"
		keys = props.keys()
		keys.sort()
		for key in keys:
			rv += "\t\t\t<dt about=\"[_:term_{term}]\" property=\"rdfa:term\" datatype=\"xsd:NMTOKEN\">{term}</dt>\n".format(term=key[len("{0}".format(self.mainprefix)):])
			rv += "\t\t\t<dd about=\"[_:term_{term}]\" property=\"rdfa:uri\" datatype=\"xsd:anyURI\">{uri}</dd>\n".format(term=key[len("{0}".format(self.mainprefix)):], uri=key)
		rv += "\t\t</dl>\n";
		return rv
	def _outputHTML_Prefixes(self):
		rv = "\t\t<h2>Prefixes</h2>\n" 
		rv += "\t\t<dl>\n"
		keys = self.prefixes.keys()
		keys.sort()
		for key in keys:
			rv += "\t\t\t<dt about=\"[_:prefix_{term}]\" property=\"rdfa:prefix\" datatype=\"xsd:NMTOKEN\">{term}</dt>\n".format(term=key)
			rv += "\t\t\t<dd about=\"[_:prefix_{term}]\" property=\"rdfa:uri\" datatype=\"xsd:string\">{uri}</dd>\n".format(term=key, uri=self.prefixes[key])
		rv += "\t\t</dl>\n";
		return rv
	
gr = Graph()
gr.parse('http://xmlns.com/foaf/0.1/index.rdf')

prefixes = {
	"foaf":  "http://xmlns.com/foaf/0.1/",
	"sioc":  "http://rdfs.org/sioc/ns#",
	"rdfs":  "http://www.w3.org/2000/01/rdf-schema#",
	"skos":  "http://www.w3.org/2004/02/skos/core#",
	"dc":    "http://purl.org/dc/terms/",
	"cert":  "http://www.w3.org/ns/auth/cert#",
	"rsa":   "http://www.w3.org/ns/auth/rsa#",
	"rdfa":  "http://www.w3.org/ns/rdfa#",
	"rel":   "http://purl.org/vocab/relationship/",
	"xsd":   "http://www.w3.org/2001/XMLSchema#",
	"bio":   "http://purl.org/vocab/bio/0.1/",
	"xsd":   "http://www.w3.org/2001/XMLSchema#",
	"owl":   "http://www.w3.org/2002/07/owl#",
	"geo":   "http://www.w3.org/2003/01/geo/wgs84_pos#",
	"rss":   "http://purl.org/rss/1.0/",
	"xhv":   "http://www.w3.org/1999/xhtml/vocab#",
	"org":   "http://www.w3.org/ns/org#",
	"doap":  "http://usefulinc.com/ns/doap#",
	"wot":   "http://xmlns.com/wot/0.1/",
	"xfn":   "http://vocab.sindice.com/xfn#",
	"rev":   "http://purl.org/stuff/rev#",
	"event": "http://purl.org/NET/c4dm/event.owl#",
	"og":    "http://ogp.me/ns#",
	"v":     "http://rdf.data-vocabulary.org/#",
	"ctag":  "http://commontag.org/ns#"
	}
foaf_profile = RDFaProfileGenerator("FOAF Profile 2010", "http://wiki.foaf-project.org/w/RDFa_Profile", gr, FOAF, prefixes)

print foaf_profile.outputHTML()
