Skip to content

Instantly share code, notes, and snippets.

@denibertovic
Created May 18, 2016 20:27
Show Gist options
  • Save denibertovic/982165bd5b55dcce6e144d49356a501d to your computer and use it in GitHub Desktop.
Save denibertovic/982165bd5b55dcce6e144d49356a501d to your computer and use it in GitHub Desktop.
Get local docs for haskell libs (with stack)
#!/usr/bin/env python
# Author: Deni Bertovic <deni@denibertovic.com>
# LICENSE: BSD3
# A simple script that greps through local stack dependencies and tries to build
# a file url that you can click and open in your browser. This is useful for reading
# docs offline when they're not built on hackage for whatever reason.
# Obviously you need to build the docs first with "stack haddock" otherwise the
# links that get printed wont work.
# Installation: Put this script somewhere in your PATH. For example: /usr/local/bin/hdoc.py
import os
import sys
from subprocess import Popen, PIPE
arg = sys.argv[1]
cwd=os.getcwd()
if not os.path.exists(os.path.join(cwd, 'stack.yaml')):
print "ERROR: No stack.yml found. Exiting."
sys.exit(1)
process = Popen(["stack", "path", "--snapshot-doc-root"], stdout=PIPE)
(output, err) = process.communicate()
exit_code = process.wait()
if exit_code != 0:
print "Failed to call stack."
sys.exit(exit_code)
doc_root = output.strip("\n")
process = Popen(["stack", "list-dependencies"], stdout=PIPE)
(output, err) = process.communicate()
exit_code = process.wait()
if exit_code != 0:
print "Failed to call stack."
sys.exit(exit_code)
deps = output.split('\n')
grep = filter(lambda x: x.startswith(arg), deps)
if grep == []:
print "No documentation matching your query was found!"
sys.exit(0)
print "These documents match your query:\n\n"
for i, p in enumerate(grep):
doc_dir_name = p.replace(" ", "-")
print "{0}. file://{1}/{2}/index.html".format(i+1, doc_root, doc_dir_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment