Skip to content
Snippets Groups Projects
Commit 6e037a57 authored by Greg Wilson's avatar Greg Wilson
Browse files

CSS cataloguing tool

parent d0853662
Branches
Tags
No related merge requests found
#!/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% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment