# HG changeset patch # User Jeff Hammel # Date 1368267535 25200 # Node ID fbc033540a34b049b03751b03c9bfc6a4192a9fe # Parent 8f92aa15406f1e7f1213385d33903b2412c39455 resurrect this ole beast diff -r 8f92aa15406f -r fbc033540a34 python/html2flux.py --- a/python/html2flux.py Fri May 10 20:44:58 2013 -0700 +++ b/python/html2flux.py Sat May 11 03:18:55 2013 -0700 @@ -1,30 +1,64 @@ #!/usr/bin/env python +""" +transform an HTML
file into a fluxbox menu +if no file give, read from stdin + +
submenu name +
program label
command
+
another program label
command2
+
+ +x-form -> internal format: + +('submenu name': [('program label', 'command'), + ('another program label', 'command2')]) +""" + +import optparse import sys from lxml import etree +from lsex import lsex # local import -from lsex import lsex # local import +# available executables executables = set([i.rsplit('/', 1)[-1] for i in lsex() ]) def printmenu(dl, output, top=True): - - # XXX should do more checking + + menu_items = [] + name = None # menu name for child in dl.iterchildren(): + if not top and child.tag == 'a': + # TODO: better way of labeling this! print >> output, '[submenu] (%s)' % child.text + if child.tag == 'dt': - label = ' '.join([ i.strip() for i in child.itertext() if i.strip() ]) + # item label + label = ' '.join([i.strip() for i in child.itertext() if i.strip()]) if child.tag == 'dd': - command = ' '.join([ i.strip() for i in child.itertext() if i.strip() ]) + command = ' '.join([i.strip() for i in child.itertext() if i.strip()]) executable = command.split()[0] - if executable in executables: + if executable in executables or os.path.isabs(executable): print >> output, '[exec] (%s) {%s}' % (label, command) + if child.tag == 'dl': printmenu(child, output, top=False) if not top: print >> output, '[end]' -def main(args = sys.argv[1:]): +def main(args=sys.argv[1:]): + + # parse command line option + usage = '%prog [options] [menu.html]' + parser = optparse.OptionParser(usage=usage, + description=__doc__) + parser.add_option('--collapse', dest='collapse', + action='store_true', default=False, + help="collapse menus with a single item to that item") + parser.add_option('-o', '--output', dest='output', + help="output file [Default: ]") + options, args = parser.parse_args(args) # setup input, output if args: @@ -38,6 +72,7 @@ dom = etree.fromstring(html) dl = dom.find('.//dl') + # print to stdout printmenu(dl, fluxout) if __name__ == '__main__':