源转换源

发布于 2025-01-28 18:25:28 字数 2346 浏览 2 评论 0原文

我想将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ映画 2025-02-04 18:25:28

这可以使用字符串编解码器完成。将UTF8编码用于文件,然后将输入转换为字节。然后使用“ unidecode_escape”来解码,以逃避unicode字符,例如新行“ \ n”,然后输出到文件输出。查看 python中的流程逃生序列解释:

with open('input.js', 'r', encoding="utf8") as f,
     open('output.js', 'w', encoding="utf8") as of:
    for line in f:
        of.write(bytes(line, "utf8").decode('unicode_escape'))

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:

with open('input.js', 'r', encoding="utf8") as f,
     open('output.js', 'w', encoding="utf8") as of:
    for line in f:
        of.write(bytes(line, "utf8").decode('unicode_escape'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文