Last active
August 29, 2015 14:20
-
-
Save kadrach/012d5607373e46790529 to your computer and use it in GitHub Desktop.
Alfred2 workflow for texdoc
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
query = "{query}" | |
import subprocess | |
import os | |
from xml.etree import ElementTree | |
from xml.etree.ElementTree import Element | |
from xml.etree.ElementTree import SubElement | |
out = [] | |
files = [] | |
def file_xml(path): | |
item = Element('item', arg=path, type='file', valid='YES') | |
SubElement(item, 'title').text = os.path.basename(path) | |
SubElement(item, 'subtitle').text = os.path.dirname(path) | |
SubElement(item, 'icon', type='fileicon').text = path | |
return item | |
# Try collecting output from texdoc | |
try: | |
out = subprocess.check_output(['/usr/texbin/texdoc', '-l', '-M', query], | |
env={'PATH': '/usr/texbin'}) | |
except: | |
raise | |
try: | |
files = [line.split('\t')[2] for line in out.rstrip().split('\n')] | |
except IndexError: | |
pass | |
# Create XML tree | |
items = Element('items') | |
items_elements = [file_xml(path) for path in files] | |
[items.insert(idx, element) for idx, element in enumerate(items_elements)] | |
print('<?xml version="1.0"?>') | |
print(ElementTree.tostring(items)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment