Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- """
- concatenates JavaScript files in the order specified in an HTML file
- Usage:
- $ concat_js.py <input> [output]
- input is name of the respective HTML file, output that of the resulting
- JavaScript file (STDOUT by default)
- """
- import sys
- import html5lib
- def main(args):
- args = [unicode(arg, "utf-8") for arg in args]
- html = args[1]
- try:
- outfile = args[2]
- except IndexError, exc:
- outfile = None
- with open(html, "r") as f:
- doc = html5lib.parse(f, treebuilder="dom")
- els = doc.getElementsByTagName("script")
- code = []
- for el in els:
- filepath = el.getAttribute("src")
- if filepath:
- with open(filepath, "r") as f:
- code.append(f.read().decode("UTF-8"))
- else:
- nodes = [node.data for node in el.childNodes]
- code.append(u"".join(nodes))
- code = u"\n".join(code).encode("UTF-8")
- if outfile:
- with open(outfile, "w") as f:
- f.write(code)
- else:
- print code
- return True
- if __name__ == "__main__":
- status = not main(sys.argv)
- sys.exit(status)
Add Comment
Please, Sign In to add comment