Skip to content

Commit

Permalink
CSS cataloguing tool
Browse files Browse the repository at this point in the history
  • Loading branch information
gvwilson committed Jun 14, 2015
1 parent d085366 commit 6e037a5
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions tools/catalog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python

'''Create YAML catalog of CSS styles using in a set of HTML documents.
Usage: catalog.py file [file...]
'''

import sys
import yaml
from bs4 import BeautifulSoup

def main(argv):
'''Main driver.'''

catalog = {}
for filename in argv[1:]:
with open(filename, 'r') as reader:
doc = BeautifulSoup(reader.read())
for node in doc.descendants:
update(catalog, node)
display(catalog)


def update(catalog, node):
'''Record classes used in node.'''

if node.name is None:
return

if node.name not in catalog:
catalog[node.name] = set()

if 'class' in node.attrs:
for cls in node.attrs['class']:
catalog[node.name].add(cls)


def display(catalog):
'''Show the catalog.'''

for name in sorted(catalog.keys()):
catalog[name] = sorted(catalog[name])
yaml.dump(catalog, stream=sys.stdout)


if __name__ == '__main__':
main(sys.argv)

0 comments on commit 6e037a5

Please sign in to comment.