/sw/hturtle/index.cgi
#!/usr/bin/env python
# utf-8 must be used
# comments in CDATA may or may not be parsed
import cgitb
cgitb.enable()
import sys, os, re, urllib
sys.path = ['..', '../trio'] + sys.path
import trio
def homepage():
print 'Content-Type: text/html'
print
f = open('about.html')
for line in f:
print line.rstrip('\r\n')
f.close()
sys.exit()
requri = os.environ.get('REQUEST_URI')
uri = requri.split('hturtle/', 1).pop()
if not uri or uri.startswith('?'):
try:
uri = __import__('cgi').FieldStorage()['uri'].value
# print 'Content-Type: text/plain'
# print
if uri.startswith('http://'):
uri = uri[7:]
# print 'URI:', uri
print 'Status: 307'
print 'Location: http://inamidst.com/sw/hturtle/' + uri
print
print '<a href="http://inamidst.com/sw/hturtle/%s">Go!</a>' % uri
sys.exit()
except KeyError: homepage()
print 'Content-Type: text/xml'
print
if not uri.startswith('http://'):
uri = 'http://' + uri
r_comment = re.compile(r'<!--\{((?:[^-]|(?:-[^-]))*)\}-->')
G = trio.Graph(baseURI=uri)
u = urllib.urlopen(uri)
bytes = u.read()
u.close()
bindings = """\
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix doap: <http://usefulinc.com/ns/doap#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
"""
turtle = bindings + '\n'.join(r_comment.findall(bytes)) + '\n'
turtle = turtle.decode('utf-8')
G.parseText(turtle, 'turtle')
print '<xsl:stylesheet version="1.0" '
print ' xmlns:xsl="http://www.w3.org/1999/XSL/Transform">'
print '<xsl:template match="/">'
for line in G.serialiseToLines('rdfxml'):
print line
print '</xsl:template>'
print '</xsl:stylesheet>'
# EOF