Skip to content

Instantly share code, notes, and snippets.

@HuStmpHrrr
Last active May 15, 2016 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HuStmpHrrr/b7de3c49f77a925dc6cf85da16a1d231 to your computer and use it in GitHub Desktop.
Save HuStmpHrrr/b7de3c49f77a925dc6cf85da16a1d231 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# vim: ft=python
import bs4
import cgitb
import os
import sys
cgitb.enable()
def content_head():
print "Content-Type: text/html"
print
def path_to_load():
return os.environ['PATH_INFO']
def target_file():
return os.path.join('/home/hu/.cabal/share/doc/x86_64-linux-ghc-7.10.3/',
*path_to_load().split('/'))
def target_content():
with open(target_file()) as fd:
return bs4.BeautifulSoup(fd.read().decode('utf-8', 'ignore'), "html.parser")
def process(soup, pipeline):
for link in soup.find_all('a'):
if link.has_attr('href'):
for pipe in pipeline:
link['href'] = pipe(link['href'])
return soup
pipeline = [
lambda s: s.replace("file:///home/hu/.cabal/share/doc/x86_64-linux-ghc-7.10.3/",
u""),
lambda s: s.replace("file:///home/hu/Tools/haskell-platform/ghc-7.10.3-x86_64/share/doc/ghc/html/",
u"/ghc-doc/")
]
if __name__ == '__main__':
if 'PATH_INFO' not in os.environ:
print 'Location: ' + os.path.basename(__file__) + '/index.html'
print
sys.exit(1)
tarfile = target_file()
ext = os.path.splitext(tarfile)[1][1:]
if ext in ('html', 'htm'):
content_head()
print process(target_content(), pipeline).prettify('utf-8')
else:
if ext == 'js':
print 'Content-Type: application/javascript'
print
elif ext == 'css':
print 'Content-Type: text/css'
print
else:
print 'Content-Type: ' + os.environ["HTTP_ACCEPT"].split(',')[0]
print
with open(tarfile) as fd:
print fd.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment