Returns a compressed Javascript string with comments and whitespace removed.
(js)
| 1106 | #--- HTML CANVAS GRAPH RENDERER -------------------------------------------------------------------- |
| 1107 | |
| 1108 | def minify(js): |
| 1109 | """ Returns a compressed Javascript string with comments and whitespace removed. |
| 1110 | """ |
| 1111 | import re |
| 1112 | W = ( |
| 1113 | "\(\[\{\,\;\=\-\+\*\/", |
| 1114 | "\)\]\}\,\;\=\-\+\*\/" |
| 1115 | ) |
| 1116 | for a, b in ( |
| 1117 | (re.compile(r"\/\*.*?\*\/", re.S), ""), # multi-line comments /**/ |
| 1118 | (re.compile(r"\/\/.*"), ""), # singe line comments // |
| 1119 | (re.compile(r";\n"), "; "), # statements (correctly) terminated with ; |
| 1120 | (re.compile(r"[ \t]+"), " "), # spacing and indentation |
| 1121 | (re.compile(r"[ \t]([\(\[\{\,\;\=\-\+\*\/])"), "\\1"), |
| 1122 | (re.compile(r"([\)\]\}\,\;\=\-\+\*\/])[ \t]"), "\\1"), |
| 1123 | (re.compile(r"\s+\n"), "\n"), |
| 1124 | (re.compile(r"\n+"), "\n")): |
| 1125 | js = a.sub(b, js) |
| 1126 | return js.strip() |
| 1127 | |
| 1128 | DEFAULT, INLINE = "default", "inline" |
| 1129 | HTML, CANVAS, STYLE, SCRIPT, DATA = "html", "canvas", "style", "script", "data" |