Skip to content

Commit

Permalink
添加了整合成一个文件的小脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
Littlefean committed Oct 10, 2022
1 parent 20f19f1 commit 3036375
Show file tree
Hide file tree
Showing 2 changed files with 1,411 additions and 0 deletions.
61 changes: 61 additions & 0 deletions htmlFileStringify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# -*- encoding: utf-8 -*-
"""
此模块用于将html文件转化成一个html字符串
将所有的css和js集成在一起
PyCharm htmlFileStringify
2022年07月28日
by littlefean
"""
import os


def lineStrGetComment(string: str) -> str:
"""
获取一个字符串中双引号内部的东西
如果有多对双引号,则找最后一对双引号
:return:
"""
indexArr = [i for i, char in enumerate(string) if char == "\""]
if len(indexArr) < 2:
return ""

left = indexArr[-2]
right = indexArr[-1]
return string[left + 1: right]


def htmlStringify(filePath: str) -> str:
print(1)
try:
print(11)

folderPath = os.sep.join(filePath.split(os.sep)[:-1])
print(folderPath)
print(111)
content = ""
with open(filePath, encoding="utf-8") as f:
arr = f.readlines()
for line in arr:
if line.strip().startswith("<link "):
# 替换css文件
with open(lineStrGetComment(line), encoding="utf-8") as f:
content += f"<style>{f.read()}</style>"
elif line.strip().startswith("<script src="):
# 替换js文件
with open(lineStrGetComment(line), encoding="utf-8") as f:
content += f"<script>{f.read()}</script>"
else:
content += line
except Exception as e:
return str(e)
return content


def main():
with open("show/index.html", "w", encoding="utf-8") as f:
f.write(htmlStringify("index.html"))
return None


if __name__ == "__main__":
main()
Loading

0 comments on commit 3036375

Please sign in to comment.