Skip to content

Commit

Permalink
Copy other python file to make overview
Browse files Browse the repository at this point in the history
  • Loading branch information
Garanas committed Nov 13, 2024
1 parent 1fd942f commit 55f2458
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions .github/workflows/scripts/python/changelog_overview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import mdformat

from argparse import ArgumentParser
from pathlib import Path

SCRIPT_NAME = Path(__file__).name

MAX_LINE_LENGTH = 150

LUA_DESC_LINE = ' "{line}"'
LUA_FILE = """Changelog = {{
version = {version},
description = {{
{description}
}}
}}
"""

HEADER_SEPARATOR = "--" * (MAX_LINE_LENGTH // len("--"))
HEADER_LINE_CENTER = MAX_LINE_LENGTH - 2 * len("--")
HEADER = f"""{HEADER_SEPARATOR}
--{f"This file was autogenerated using {SCRIPT_NAME:}":^{HEADER_LINE_CENTER}}--
--{{source:^{HEADER_LINE_CENTER}}}--
{HEADER_SEPARATOR}
"""


def get_parser():
ap = ArgumentParser(description="Creates an overview of the changelogs in a folder.")
ap.add_argument(
"input_folder",
help="Folder with markdown files",
type=Path,
)
ap.add_argument(
"output_file",
help="Lua file",
type=Path,
)
return ap


def convert_changelog(markdown: Path, lua: Path):
version = markdown.stem

source_info = f"Source: {markdown}"
header = HEADER.format(source=source_info)



lua_content = markdown2lua(version, markdown.read_text())
lua.write_text(header + lua_content)


def markdown2lua(version: str, content: str) -> str:
formatted_md = mdformat.text(
content,
options={
"wrap": MAX_LINE_LENGTH,
},
)

escaped_md = escape_special_symbols(formatted_md)
return LUA_FILE.format(
version=version,
description=",\n".join(
LUA_DESC_LINE.format(line=line) for line in escaped_md.splitlines()
),
)


def escape_special_symbols(text: str) -> str:
return text.replace("\\", "\\\\").replace('"', '\\"')


if __name__ == "__main__":
parser = get_parser()
args = parser.parse_args()
convert_changelog(args.input_file, args.output_file)

0 comments on commit 55f2458

Please sign in to comment.