Fixing typo
[rnaseq-cwl-training.git] / tools / catalog.py
1 #!/usr/bin/env python
2
3 '''Create YAML catalog of CSS styles used in a set of HTML documents.
4
5 Usage: catalog.py file [file...]
6 '''
7
8 import sys
9 import yaml
10 from bs4 import BeautifulSoup
11
12 def main(argv):
13     '''Main driver.'''
14
15     catalog = {}
16     for filename in argv[1:]:
17         with open(filename, 'r') as reader:
18             doc = BeautifulSoup(reader.read())
19             for node in doc.descendants:
20                 update(catalog, node)
21     display(catalog)
22
23
24 def update(catalog, node):
25     '''Record classes used in node.'''
26
27     if node.name is None:
28         return
29
30     if node.name not in catalog:
31         catalog[node.name] = set()
32
33     if 'class' in node.attrs:
34         for cls in node.attrs['class']:
35             catalog[node.name].add(cls)
36
37
38 def display(catalog):
39     '''Show the catalog.'''
40
41     for name in sorted(catalog.keys()):
42         catalog[name] = sorted(catalog[name])
43     yaml.dump(catalog, stream=sys.stdout)
44
45
46 if __name__ == '__main__':
47     main(sys.argv)