Last active
May 15, 2016 20:55
-
-
Save HuStmpHrrr/b7de3c49f77a925dc6cf85da16a1d231 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" | |
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' | |
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' | |
elif ext == 'css': | |
print 'Content-Type: text/css' | |
else: | |
print 'Content-Type: ' + os.environ["HTTP_ACCEPT"].split(',')[0] | |
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