Skip to content

Instantly share code, notes, and snippets.

@coldfix
Last active February 6, 2017 00:04
Show Gist options
  • Save coldfix/2e22e1c549754493ea80068b52314226 to your computer and use it in GitHub Desktop.
Save coldfix/2e22e1c549754493ea80068b52314226 to your computer and use it in GitHub Desktop.
primitive texdoc replacement for when you don't have latex documentation installed on disk
#! /bin/zsh
prefix=$HOME/.texdoc
pkg=$1
doc=$(hrefbytext http://ctan.org/pkg/$pkg 'Pack­age doc­u­men­ta­tion') &&
mkdir -p $prefix
cache=$prefix/$(basename $doc) && (
if [[ ! -e $cache ]]; then
wget $doc -O $cache
fi
) &&
evince $cache
#! /usr/bin/env python
# encoding: utf-8
"""
Get link by text content.
Usage:
hrefbytext URL TEXT
Example:
hrefbytext "http://ctan.org/pkg/subcaption" \
"Pack­age doc­u­men­ta­tion"
"""
import sys
from lxml.html import parse, fromstring
def main(args):
url = args[0]
doc = parse(url).getroot()
text = fromstring(args[1]).text_content() # replace HTML entities
for link in doc.cssselect('a'):
if link.text_content() == text:
print(link.get('href'))
if __name__ == '__main__':
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment