Python method for formatting compressed JS files
- 2020-04-02 14:38:31
- OfStack
This article illustrates how Python formats a compressed JS file. Share with you for your reference. Specific analysis is as follows:
The script can be compressed js file format on some restore, of course, not 100% perfect, temporarily do not deal with the syntax problem, just to facilitate the reading of js code
lines = open("unformated.js").readlines()[0].split(";")
# Generally compressed file all the code in a line
# Set the index as the case may be 0 The line is the source code.
indent = 0
formatted = []
for line in lines:
newline = []
for char in line:
newline.append(char)
if char=='{': #{ That's the basis of the indentation
indent+=1
newline.append("n")
newline.append("t"*indent)
if char=="}":
indent-=1
newline.append("n")
newline.append("t"*indent)
formatted.append("t"*indent+"".join(newline))
open("formated.js","w").writelines(";n".join(formatted))
I hope this article has helped you with your Python programming.