-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy other python file to make overview
- Loading branch information
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |