0

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.

jumpdiffusion
  • 704
  • 1
  • 7
  • 22

1 Answers1

1

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'))
PythonMCSJ
  • 133
  • 5