Guest User

Untitled

a guest
Jan 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """
  4. concatenates JavaScript files in the order specified in an HTML file
  5.  
  6.  
  7. Usage:
  8. $ concat_js.py <input> [output]
  9.  
  10. input is name of the respective HTML file, output that of the resulting
  11. JavaScript file (STDOUT by default)
  12. """
  13.  
  14. import sys
  15.  
  16. import html5lib
  17.  
  18.  
  19. def main(args):
  20. args = [unicode(arg, "utf-8") for arg in args]
  21. html = args[1]
  22. try:
  23. outfile = args[2]
  24. except IndexError, exc:
  25. outfile = None
  26.  
  27. with open(html, "r") as f:
  28. doc = html5lib.parse(f, treebuilder="dom")
  29. els = doc.getElementsByTagName("script")
  30.  
  31. code = []
  32. for el in els:
  33. filepath = el.getAttribute("src")
  34. if filepath:
  35. with open(filepath, "r") as f:
  36. code.append(f.read().decode("UTF-8"))
  37. else:
  38. nodes = [node.data for node in el.childNodes]
  39. code.append(u"".join(nodes))
  40.  
  41. code = u"\n".join(code).encode("UTF-8")
  42. if outfile:
  43. with open(outfile, "w") as f:
  44. f.write(code)
  45. else:
  46. print code
  47.  
  48. return True
  49.  
  50.  
  51. if __name__ == "__main__":
  52. status = not main(sys.argv)
  53. sys.exit(status)
Add Comment
Please, Sign In to add comment