源转换源
我想将input.js
转换为output.js
使用python中的字符串操纵
以下是input.js
let k=document.createElement("div");
k.innerHTML='<style>\n .wrapper {\n opacity: 0;\n transition: visibility 0s, opacity 0.25s ease-in;\n }\n\n .overlay {\n height: 100%;\n position: fixed;\n top: 0;\n right: 0;\n }\n\n button {\n cursor: pointer;\n font-size: 1.25rem;\n }\n </style>\n <div class="wrapper">\n <div class="overlay"></div>\n <div>\n <button class="close">️x</button>\n <h1 id="title">Hello world</h1>\n <div id="content" class="content">\n <p>This is content outside of the shadow DOM</p>\n </div>\n </div>\n </div>';
以下是output。 js
let k=document.createElement("div");
k.innerHTML=`<style>
.wrapper {
opacity: 0;
transition: visibility 0s, opacity 0.25s ease-in;
}
.overlay {
height: 100%;
position: fixed;
top: 0;
right: 0;
}
button {
cursor: pointer;
font-size: 1.25rem;
}
</style>
<div class="wrapper">
<div class="overlay"></div>
<div>
<button class="close">️x</button>
<h1 id="title">Hello world</h1>
<div id="content" class="content">
<p>This is content outside of the shadow DOM</p>
</div>
</div>
</div>`;
输入。JS中存在所有要输出的信息。没有信息丢失。
以下是不起作用的
import html
import sys
import io
def sprint(*args, end='', **kwargs):
sio = io.StringIO()
print(*args, **kwargs, end=end, file=sio)
return sio.getvalue()
with open('input.js') as f, open('output.js', 'w') as of:
for line in f:
if 'innerHTML' in line:
arr = line.split("=")
nline = arr[0]+"=`"+sprint(html.unescape(arr[1].strip(" '\"")))+"`\n"
of.write(nline)
continue
of.write(line)
,我想从Minified JavaScript文件中打印出我的Interhtml字符串。有没有干净的方法可以在Python中进行此操作。
I would like to transform input.js
to output.js
using string manipulation in python
here is input.js
let k=document.createElement("div");
k.innerHTML='<style>\n .wrapper {\n opacity: 0;\n transition: visibility 0s, opacity 0.25s ease-in;\n }\n\n .overlay {\n height: 100%;\n position: fixed;\n top: 0;\n right: 0;\n }\n\n button {\n cursor: pointer;\n font-size: 1.25rem;\n }\n </style>\n <div class="wrapper">\n <div class="overlay"></div>\n <div>\n <button class="close">️x</button>\n <h1 id="title">Hello world</h1>\n <div id="content" class="content">\n <p>This is content outside of the shadow DOM</p>\n </div>\n </div>\n </div>';
here is output.js
let k=document.createElement("div");
k.innerHTML=`<style>
.wrapper {
opacity: 0;
transition: visibility 0s, opacity 0.25s ease-in;
}
.overlay {
height: 100%;
position: fixed;
top: 0;
right: 0;
}
button {
cursor: pointer;
font-size: 1.25rem;
}
</style>
<div class="wrapper">
<div class="overlay"></div>
<div>
<button class="close">️x</button>
<h1 id="title">Hello world</h1>
<div id="content" class="content">
<p>This is content outside of the shadow DOM</p>
</div>
</div>
</div>`;
All the information to get to output.js is present in input.js. There is no loss of information.
Below does not work
import html
import sys
import io
def sprint(*args, end='', **kwargs):
sio = io.StringIO()
print(*args, **kwargs, end=end, file=sio)
return sio.getvalue()
with open('input.js') as f, open('output.js', 'w') as of:
for line in f:
if 'innerHTML' in line:
arr = line.split("=")
nline = arr[0]+"=`"+sprint(html.unescape(arr[1].strip(" '\"")))+"`\n"
of.write(nline)
continue
of.write(line)
I want to prettty print my innerHTML strings from minified javascript files. Is there a clean way to do this in python.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可以使用字符串编解码器完成。将UTF8编码用于文件,然后将输入转换为字节。然后使用“ unidecode_escape”来解码,以逃避unicode字符,例如新行“ \ n”,然后输出到文件输出。查看 python中的流程逃生序列解释:
This can be done using string codecs. Use utf8-encoding for the files, and convert the input to bytes. Then decode using 'unidecode_escape', to escape unicode characters, such as new lines "\n", and output to file output.js. Look at Process escape sequences in a string in Python for further explanation: