Mercurial > hg > config
diff python/html2flux.py @ 292:fbc033540a34
resurrect this ole beast
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Sat, 11 May 2013 03:18:55 -0700 |
parents | 069a739d88ad |
children | 45b970048ae2 |
line wrap: on
line diff
--- 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 <dl> file into a fluxbox menu +if no file give, read from stdin + +<dl><a>submenu name</a> + <dt>program label</dt><dd>command</dd> + <dt>another program label</dt><dd>command2</dd> +</dl> + +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: <stdout>]") + 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__':