From ebcebddf8a759fdd3e5d9ac7f5bde7a38b6f7b75 Mon Sep 17 00:00:00 2001 From: Cutieguwu Date: Thu, 17 Jul 2025 12:17:27 -0400 Subject: [PATCH] Final implementation of tag. --- .zed/debug.json | 10 +++ .zed/tasks.json | 50 +++++++++++ README.md | 42 +++++++++- src/__pycache__/main.cpython-313.pyc | Bin 0 -> 4012 bytes src/main.py | 101 +++++++++++++++++++++++ tests/src/index.html | 4 +- tests/src/nav.html | 119 ++++++++++++--------------- 7 files changed, 253 insertions(+), 73 deletions(-) create mode 100644 .zed/debug.json create mode 100644 .zed/tasks.json create mode 100644 src/__pycache__/main.cpython-313.pyc create mode 100644 src/main.py diff --git a/.zed/debug.json b/.zed/debug.json new file mode 100644 index 0000000..5e6dbfa --- /dev/null +++ b/.zed/debug.json @@ -0,0 +1,10 @@ +// Project-local debug tasks +// +// For more documentation on how to configure debug tasks, +// see: https://zed.dev/docs/debugger +[{ + "label": "Python Active File", + "adapter": "Debugpy", + "program": "$ZED_FILE", + "request": "launch" + }] diff --git a/.zed/tasks.json b/.zed/tasks.json new file mode 100644 index 0000000..a79c550 --- /dev/null +++ b/.zed/tasks.json @@ -0,0 +1,50 @@ +// Project tasks configuration. See https://zed.dev/docs/tasks for documentation. +// +// Example: +[ + { + "label": "Example task", + "command": "for i in {1..5}; do echo \"Hello $i/5\"; sleep 1; done", + //"args": [], + // Env overrides for the command, will be appended to the terminal's environment from the settings. + "env": { "foo": "bar" }, + // Current working directory to spawn the command into, defaults to current project root. + //"cwd": "/path/to/working/directory", + // Whether to use a new terminal tab or reuse the existing one to spawn the process, defaults to `false`. + "use_new_terminal": false, + // Whether to allow multiple instances of the same task to be run, or rather wait for the existing ones to finish, defaults to `false`. + "allow_concurrent_runs": false, + // What to do with the terminal pane and tab, after the command was started: + // * `always` — always show the task's pane, and focus the corresponding tab in it (default) + // * `no_focus` — always show the task's pane, add the task's tab in it, but don't focus it + // * `never` — do not alter focus, but still add/reuse the task's tab in its pane + "reveal": "always", + // Where to place the task's terminal item after starting the task: + // * `dock` — in the terminal dock, "regular" terminal items' place (default) + // * `center` — in the central pane group, "main" editor area + "reveal_target": "dock", + // What to do with the terminal pane and tab, after the command had finished: + // * `never` — Do nothing when the command finishes (default) + // * `always` — always hide the terminal tab, hide the pane also if it was the last tab in it + // * `on_success` — hide the terminal tab on task success only, otherwise behaves similar to `always` + "hide": "never", + // Which shell to use when running a task inside the terminal. + // May take 3 values: + // 1. (default) Use the system's default terminal configuration in /etc/passwd + // "shell": "system" + // 2. A program: + // "shell": { + // "program": "sh" + // } + // 3. A program with arguments: + // "shell": { + // "with_arguments": { + // "program": "/bin/bash", + // "args": ["--login"] + // } + // } + "shell": "system", + // Represents the tags for inline runnable indicators, or spawning multiple tasks at once. + "tags": [] + } +] diff --git a/README.md b/README.md index 65ec61a..376ed55 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,44 @@ Reuse chunks of HTML in other HTML files via preprocessing. -Reused the chunk in `global_nav.html` via ``. +Reuse the chunk in `global_nav.html` via ``. -Balloon will inflate the chunk in place of the `` tag in the source file, dumping a released target file which can be pushed to the web server. \ No newline at end of file +Balloon will inflate the chunk in place of the `` tag in the source file, dumping a released target file which can be pushed to the web server. + +-- + +I do plan to migrate this project to rust once I'm finished tinkering around with the concept. I am considering adding the ability to add includes which wrap other includes. + +This could be useful for templates, so that the base html template could look like this: + + + + + + {Inserted Var} + + + + + {Inserted Nav Chunk} + {Inserted Page Chunk} + + + +With the actual composing file, say `index.html` looking like this: + + + + + + Home | MySite.tld + + +

Welcome

+

lorem ipsum...

+
+
+ +Perhaps I could do this with a custom `` tag with maybe an `id` parameter. How you would declare a variable, I'm not sure. At the very least, I'll likely have to make a proper parser to make this happen. The script is kinda hacky as it is. + +The idea being that `` in effect pulls in stuff from another file, and `` places in within something. Perhaps think of it as sewing; you stitch both sides together by pushing the pin through, and pulling it back. diff --git a/src/__pycache__/main.cpython-313.pyc b/src/__pycache__/main.cpython-313.pyc new file mode 100644 index 0000000000000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<)4=$hb!%! z4?_<^k8l z&dI=2Yc=Bh#SYlW9He5kw&96R>huXP!Z_||Zu-?JsDV1a*9sk~U;J}Vh1wBpa+*wK z!R<$Dz=j&Q+&P52$L-eZS~gN6;ra@9tZwyA8?xj8IYwNtj)O%L0L=X)4u8W}-=h1; z1oz!Dvc#E@5Qk?^_e~b2wEtSJ!qsaUcF_u@kv7viX-?%C7OAuS-(#`LpuXv=ue7+E z!2M@oxlQ=&E!6B(c?UGhQ`B!Vbs8!VZo-oACA{c3y*z6D_2L>B!B4#3#aE!&;RI7a4puh%suox@O&YC_I63y7KwkD+wu^9cqM${ zPx52m<*%<+B;SH}&buhx4=;tw(fE^S&uX-%98Elyj%-unPjV~1%iCUD`B&f2x7)Bf z3EJgv4dkUy`_BjHpIds*@1tJ@{O9+2zuMM8J$B^f3ZgRLw%4M_+FxQmhR=htQ zx1y&U2)~R^OFHWmYHu?rDhOt^p@G<-9LGH+-Tx#9o|2BIr1S5jdzExQC2i}Rf15wR zb#4$4o0RZ@4f}O~19h7}%(ZV25Y-XTw%+AL?&wCC2*J{9neP2Y@c#PZ1|@=1Y5p5R Cu=VEv literal 0 HcmV?d00001 diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..6af5ece --- /dev/null +++ b/src/main.py @@ -0,0 +1,101 @@ +from __future__ import annotations + +from dataclasses import dataclass +from os import setregid +from types import NoneType +from typing import Optional +from result import Result, Ok, Err +from icecream import ic + +@dataclass +class Tag: + value: str + trail: Optional[str] + + def __post_init__(self) -> None: + self.trail = self.trail if (self.trail is not None) and (self.trail.strip() != '') else None + + # Returns the type of tag. + def type(self) -> str: + type = str() + + for c in self.value: + if c.isspace(): + break + + type += c + + + return type + + def get_param(self, param: str) -> Optional[str]: + pos = self.value.find(param) + param.__len__() + len('="') + + if pos == -1: + return None + + param_value = str() + + for idx in range(pos, (self.value.__len__() - param.__len__())): + param_value += self.value[idx] + + return param_value + + def write(self) -> str: + 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]: + tag = str() + trail: Optional[str] = str() + tags = list() + record = False + + for c in self.value: + if c == '<' and tag != '': + tags.append(Tag(tag, trail)) + tag = str() + trail = str() + + if c == '<' or c == '>': + record = not record # why can't I have ! operator... + elif record == True: + tag += c + else: + trail += c + + tags.append(Tag(tag, trail)) + return tags + + def inflate(self) -> Result[str, str]: + file = str() + + for tag in self.tags(): + if tag.type() == 'include': + chunk = tag.get_param('src') + if isinstance(chunk, NoneType): + return Err('FileNotFoundError') + + html = HTML(open(chunk, 'rt').read()) + file += html.inflate().expect('FileNotFoundError') + else: + file += tag.write() + + return Ok(file) + + # Convert the HTML obj into a str to write to file. + def write(self) -> str: + return self.value + +def run() -> None: + with open('/home/cutieguwu/Workspace/balloon/tests/src/index.html', 'rt') as f: + html_src = HTML(f.read()) + with open('/home/cutieguwu/Workspace/balloon/target/index.html', 'w') as f: + f.write(html_src.write()) + +if __name__ == '__main__': + run() diff --git a/tests/src/index.html b/tests/src/index.html index 7a52837..d623e3a 100644 --- a/tests/src/index.html +++ b/tests/src/index.html @@ -2,12 +2,9 @@ - Home | Cutieguwu - - +

lorem ipsum etc idk what else there is in this latin phrase

diff --git a/tests/src/nav.html b/tests/src/nav.html index 59a50d1..1d282ef 100644 --- a/tests/src/nav.html +++ b/tests/src/nav.html @@ -1,69 +1,52 @@ - +