From 6e037a57288bce7e8f97a11fde9ac8c5bb00fbf4 Mon Sep 17 00:00:00 2001 From: Greg Wilson Date: Sun, 14 Jun 2015 17:04:37 -0400 Subject: [PATCH] CSS cataloguing tool --- tools/catalog.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tools/catalog.py diff --git a/tools/catalog.py b/tools/catalog.py new file mode 100644 index 0000000..4f449bb --- /dev/null +++ b/tools/catalog.py @@ -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)