Update main.py
This commit is contained in:
107
src/balloon_old.py
Normal file
107
src/balloon_old.py
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from types import NoneType
|
||||||
|
from typing import Optional
|
||||||
|
from result import Result, Ok, Err
|
||||||
|
from icecream import ic
|
||||||
|
import os
|
||||||
|
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from _typeshed import StrPath
|
||||||
|
|
||||||
|
WORK_DIR: StrPath = os.getcwd()
|
||||||
|
|
||||||
|
@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(str(WORK_DIR) + '/src/' + 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.inflate().unwrap()
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
with open(str(WORK_DIR) + '/src/index.html', 'rt') as f:
|
||||||
|
html_src = HTML(f.read())
|
||||||
|
with open(str(WORK_DIR) + '/target/index.html', 'w') as f:
|
||||||
|
f.write(html_src.write())
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
110
src/main.py
110
src/main.py
@@ -5,6 +5,16 @@ from types import NoneType
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
from result import Result, Ok, Err
|
from result import Result, Ok, Err
|
||||||
from icecream import ic
|
from icecream import ic
|
||||||
|
import os
|
||||||
|
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from _typeshed import StrPath
|
||||||
|
|
||||||
|
WORK_DIR: StrPath = os.getcwd()
|
||||||
|
|
||||||
|
consts = dict()
|
||||||
|
inserts = dict()
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Tag:
|
class Tag:
|
||||||
@@ -24,7 +34,6 @@ class Tag:
|
|||||||
|
|
||||||
type += c
|
type += c
|
||||||
|
|
||||||
|
|
||||||
return type
|
return type
|
||||||
|
|
||||||
def get_param(self, param: str) -> Optional[str]:
|
def get_param(self, param: str) -> Optional[str]:
|
||||||
@@ -40,8 +49,50 @@ class Tag:
|
|||||||
|
|
||||||
return param_value
|
return param_value
|
||||||
|
|
||||||
|
def inflate(self) -> Optional[str]:
|
||||||
|
match self.type():
|
||||||
|
case 'include':
|
||||||
|
chunk = str(self.get_param('src'))
|
||||||
|
|
||||||
|
html = HTML(open(
|
||||||
|
str(WORK_DIR)
|
||||||
|
+ '/src/'
|
||||||
|
+ chunk,
|
||||||
|
'rt')
|
||||||
|
.read())
|
||||||
|
|
||||||
|
return html.inflate()
|
||||||
|
|
||||||
|
case 'const':
|
||||||
|
global consts
|
||||||
|
id = str(self.get_param('id'))
|
||||||
|
|
||||||
|
if self.self_closes():
|
||||||
|
# Pull value
|
||||||
|
return consts[id]
|
||||||
|
else:
|
||||||
|
# Set value
|
||||||
|
consts[id] = self.trail
|
||||||
|
|
||||||
|
case 'insert':
|
||||||
|
global inserts
|
||||||
|
id = str(self.get_param('id'))
|
||||||
|
|
||||||
|
if self.self_closes():
|
||||||
|
# Pull value
|
||||||
|
return inserts[id]
|
||||||
|
else:
|
||||||
|
# Set value
|
||||||
|
inserts[id] = self.trail
|
||||||
|
|
||||||
|
case _:
|
||||||
|
return self.write()
|
||||||
|
|
||||||
|
def self_closes(self):
|
||||||
|
return False if self.value.find('/>') == -1 else True
|
||||||
|
|
||||||
def write(self) -> str:
|
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
|
@dataclass
|
||||||
class HTML:
|
class HTML:
|
||||||
@@ -70,31 +121,56 @@ class HTML:
|
|||||||
tags.append(Tag(tag, trail))
|
tags.append(Tag(tag, trail))
|
||||||
return tags
|
return tags
|
||||||
|
|
||||||
def inflate(self) -> Result[str, str]:
|
# This modifies the internal structure of the HTML instance.
|
||||||
|
def inflate(self) -> str:
|
||||||
|
tag_const = 0
|
||||||
|
tag_include = 0
|
||||||
|
tag_insert = 0
|
||||||
|
|
||||||
|
while (tag_const + tag_include + tag_insert) != 0:
|
||||||
file = str()
|
file = str()
|
||||||
|
|
||||||
|
tag_const = 0
|
||||||
|
tag_include = 0
|
||||||
|
tag_insert = 0
|
||||||
|
|
||||||
|
# Must expand <const> then <include> then <insert>
|
||||||
for tag in self.tags():
|
for tag in self.tags():
|
||||||
if tag.type() == 'include':
|
not_finished = True
|
||||||
chunk = tag.get_param('src')
|
|
||||||
if isinstance(chunk, NoneType):
|
|
||||||
return Err('FileNotFoundError')
|
|
||||||
|
|
||||||
html = HTML(open(chunk, 'rt').read())
|
match tag.type():
|
||||||
file += html.inflate().expect('FileNotFoundError')
|
case 'const':
|
||||||
else:
|
tag_const += 1
|
||||||
file += tag.write()
|
case 'include':
|
||||||
|
tag_include += 1
|
||||||
|
case 'insert':
|
||||||
|
tag_insert += 1
|
||||||
|
case _:
|
||||||
|
pass
|
||||||
|
|
||||||
return Ok(file)
|
inflated = tag.inflate()
|
||||||
|
if inflated is not None:
|
||||||
|
file += str(inflated)
|
||||||
|
|
||||||
|
for tag in self.tags():
|
||||||
|
if tag_const + tag_include + tag_insert
|
||||||
|
if (tag_const != 0) and (tag.type() == 'const'):
|
||||||
|
tag.inflate()
|
||||||
|
|
||||||
|
self.value = file
|
||||||
|
|
||||||
|
return self.value
|
||||||
|
|
||||||
# Convert the HTML obj into a str to write to file.
|
# Convert the HTML obj into a str to write to file.
|
||||||
def write(self) -> str:
|
def write(self) -> str:
|
||||||
return self.value
|
return self.inflate()
|
||||||
|
|
||||||
def run() -> None:
|
|
||||||
with open('/home/cutieguwu/Workspace/balloon/tests/src/index.html', 'rt') as f:
|
def main() -> None:
|
||||||
|
with open(str(WORK_DIR) + '/src/template.html', 'rt') as f:
|
||||||
html_src = HTML(f.read())
|
html_src = HTML(f.read())
|
||||||
with open('/home/cutieguwu/Workspace/balloon/target/index.html', 'w') as f:
|
with open(str(WORK_DIR) + '/target/index.html', 'w') as f:
|
||||||
f.write(html_src.write())
|
f.write(html_src.write())
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
run()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user