diff --git a/.gitignore b/.gitignore index b60de5b..79efafb 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ **/target +.venv/ +**/__pycache__ diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..2397bf8 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,13 @@ +[project] +name = "balloon" +version = "0.0.1" +description = "Reuse chunks of HTML in other HTML files via preprocessing." +authors = ["Cutieguwu "] +dependencies = [ + "result", +] + +requires-python = ">=3.13" + +[tool.uv.sources] +result = { git = "https://github.com/montasaurus/result" } diff --git a/src/main.py b/src/main.py index a465ad3..b9f2879 100644 --- a/src/main.py +++ b/src/main.py @@ -3,9 +3,7 @@ from __future__ import annotations from dataclasses import dataclass from types import NoneType from typing import Optional -from icecream.icecream import print_function from result import Result, Ok, Err -from icecream import ic import os import sys @@ -49,14 +47,14 @@ class Tag: return param_value def write(self) -> str: - return f'<{self.value}>{self.trail if self.trail != None else ''}' + return f'<{self.value}>{self.trail if self.trail != None else ""}' @dataclass class HTML: value: str # Returns all tags in order in the html file. - def tags(self) -> list[Tag]: + def tags(self) -> Result[list[Tag], str]: tag = str() trail: Optional[str] = str() tags = list() @@ -72,16 +70,25 @@ class HTML: record = not record # why can't I have ! operator... elif record == True: tag += c + else: - trail += c + try: + assert type(trail) == str + trail += c + except AssertionError: + return Err('Invalid HTML Structure') tags.append(Tag(tag, trail)) - return tags + return Ok(tags) def inflate(self) -> Result[str, str]: file = str() - for tag in self.tags(): + tags = self.tags() + if tags.is_err(): + return Err(tags.err()) # type: ignore[arg-type] + + for tag in tags.ok(): # type: ignore[union-attr] if tag.type() == 'include': chunk = tag.get_param('src') if isinstance(chunk, NoneType): @@ -98,36 +105,32 @@ class HTML: def write(self) -> str: return self.inflate().unwrap() - def main() -> None: # If: # Incorrect number of arguments # Long help flag # Short help flag - if len(sys.argv) != 2 or ( + if len(sys.argv) != 3 or ( sys.argv[0] == '--help' or sys.argv[0] == '-h' ): help() return - file_name = sys.argv[1].removeprefix('src/') - - with open(str(WORK_DIR) + 'src/' + file_name, 'rt') as f: + with open(str(WORK_DIR) + sys.argv[1], 'rt') as f: html_src = HTML(f.read()) + # Patch to make sure that target paths are available. try: - os.makedirs(str(WORK_DIR) + 'target/' + os.path.dirname(file_name)) + os.makedirs(str(WORK_DIR) + os.path.dirname(sys.argv[2])) except FileExistsError: pass - with open(str(WORK_DIR) + 'target/' + file_name, 'w') as f: + with open(str(WORK_DIR) + sys.argv[2], 'w') as f: f.write(html_src.write()) def help() -> None: - print('Usage: python balloon.py [OPTIONS] ') - print() - print('Note: balloon implicitly assumes that is in src/, and should inflate into target/') + print('Usage: python balloon.py [OPTIONS] ') print() print() print('Options:') diff --git a/uv.lock b/uv.lock new file mode 100644 index 0000000..266efc2 --- /dev/null +++ b/uv.lock @@ -0,0 +1,19 @@ +version = 1 +revision = 3 +requires-python = ">=3.13" + +[[package]] +name = "balloon" +version = "0.0.1" +source = { virtual = "." } +dependencies = [ + { name = "result" }, +] + +[package.metadata] +requires-dist = [{ name = "result", git = "https://github.com/montasaurus/result" }] + +[[package]] +name = "result" +version = "0.19.0" +source = { git = "https://github.com/montasaurus/result#59cd2fb7b57e4851a7054917fbd03c4c8f68a62f" }