comparison 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
comparison
equal deleted inserted replaced
291:8f92aa15406f 292:fbc033540a34
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 """
4 transform an HTML <dl> file into a fluxbox menu
5 if no file give, read from stdin
6
7 <dl><a>submenu name</a>
8 <dt>program label</dt><dd>command</dd>
9 <dt>another program label</dt><dd>command2</dd>
10 </dl>
11
12 x-form -> internal format:
13
14 ('submenu name': [('program label', 'command'),
15 ('another program label', 'command2')])
16 """
17
18 import optparse
3 import sys 19 import sys
4 from lxml import etree 20 from lxml import etree
21 from lsex import lsex # local import
5 22
6 from lsex import lsex # local import 23 # available executables
7 executables = set([i.rsplit('/', 1)[-1] for i in lsex() ]) 24 executables = set([i.rsplit('/', 1)[-1] for i in lsex() ])
8 25
9 def printmenu(dl, output, top=True): 26 def printmenu(dl, output, top=True):
10 27
11 # XXX should do more checking 28 menu_items = []
29 name = None # menu name
12 for child in dl.iterchildren(): 30 for child in dl.iterchildren():
31
13 if not top and child.tag == 'a': 32 if not top and child.tag == 'a':
33 # TODO: better way of labeling this!
14 print >> output, '[submenu] (%s)' % child.text 34 print >> output, '[submenu] (%s)' % child.text
35
15 if child.tag == 'dt': 36 if child.tag == 'dt':
16 label = ' '.join([ i.strip() for i in child.itertext() if i.strip() ]) 37 # item label
38 label = ' '.join([i.strip() for i in child.itertext() if i.strip()])
17 if child.tag == 'dd': 39 if child.tag == 'dd':
18 command = ' '.join([ i.strip() for i in child.itertext() if i.strip() ]) 40 command = ' '.join([i.strip() for i in child.itertext() if i.strip()])
19 executable = command.split()[0] 41 executable = command.split()[0]
20 if executable in executables: 42 if executable in executables or os.path.isabs(executable):
21 print >> output, '[exec] (%s) {%s}' % (label, command) 43 print >> output, '[exec] (%s) {%s}' % (label, command)
44
22 if child.tag == 'dl': 45 if child.tag == 'dl':
23 printmenu(child, output, top=False) 46 printmenu(child, output, top=False)
24 if not top: 47 if not top:
25 print >> output, '[end]' 48 print >> output, '[end]'
26 49
27 def main(args = sys.argv[1:]): 50 def main(args=sys.argv[1:]):
51
52 # parse command line option
53 usage = '%prog [options] [menu.html]'
54 parser = optparse.OptionParser(usage=usage,
55 description=__doc__)
56 parser.add_option('--collapse', dest='collapse',
57 action='store_true', default=False,
58 help="collapse menus with a single item to that item")
59 parser.add_option('-o', '--output', dest='output',
60 help="output file [Default: <stdout>]")
61 options, args = parser.parse_args(args)
28 62
29 # setup input, output 63 # setup input, output
30 if args: 64 if args:
31 htmlfile = file(args[0]) 65 htmlfile = file(args[0])
32 else: 66 else:
36 70
37 # get first element 71 # get first element
38 dom = etree.fromstring(html) 72 dom = etree.fromstring(html)
39 dl = dom.find('.//dl') 73 dl = dom.find('.//dl')
40 74
75 # print to stdout
41 printmenu(dl, fluxout) 76 printmenu(dl, fluxout)
42 77
43 if __name__ == '__main__': 78 if __name__ == '__main__':
44 main() 79 main()