32 lines
1.3 KiB
Python
32 lines
1.3 KiB
Python
# This file is named that because I am eating cherries :3
|
|
|
|
import json
|
|
import textwrap
|
|
|
|
with open("cerisesfonts/h1.json", "r") as file:
|
|
font_h1 = json.load(file)
|
|
|
|
def process(text: str, wrap_width: int = None) -> str:
|
|
lines = text.split("\n")
|
|
inside_code_block = False
|
|
for i, line in enumerate(lines):
|
|
if line == "```":
|
|
inside_code_block = not inside_code_block
|
|
if not inside_code_block:
|
|
if line[0:2] == "# ":
|
|
final_data = ["", ""]
|
|
for char in line[2:]:
|
|
if char in font_h1:
|
|
char_data = font_h1[char].split("\n")
|
|
if wrap_width and len(final_data[-2]) + len(char_data[0]) > wrap_width:
|
|
final_data.extend(["", ""])
|
|
final_data[-2] = final_data[-2] + char_data[0]
|
|
final_data[-1] = final_data[-1] + char_data[1]
|
|
lines[i] = "\n".join(final_data)
|
|
if line[0:2] != "# ":
|
|
lines[i] = "\n".join(textwrap.wrap(lines[i], wrap_width, tabsize=2))
|
|
|
|
return "\n".join(lines)
|
|
|
|
if __name__ == "__main__":
|
|
print(process("# Hello Guys 123\nThis is a cool Markdown file. Isn't it so cool?\n```\nThis is a code block\n# This is a code comment, not a heading!\n```", wrap_width=16)) |