Skip to content

Commit

Permalink
Merge pull request #1 from akumarujon/main
Browse files Browse the repository at this point in the history
(feat): add `write` function && specify `Head` and `Body` manually
  • Loading branch information
ChogirmaliYigit authored Mar 2, 2024
2 parents ead6439 + 6aaed23 commit 5f17a9c
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions markupify/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,30 @@ class HTMLPage:
add_body: Add tags to the body section.
"""

def __init__(self, lang: str = "en"):
def __init__(self,head: Optional[Head], body: Optional[Body], lang: str = "en"):
"""
Initialize the HTMLPage instance.
Args:
### Args:
lang (str, optional): The language of the HTML page (Defaults to "en").
head (str, optional): The head section of the HTML page (Defaults to "<head></head>").
body (str, optional): The body section of the HTML page (Defaults to "<body></body>").
### Attributes:
lang (str): The language of the HTML page.
head (Head): The head section of the HTML page.
body (Body): The body section of the HTML page.
template (str): The template of the HTML page.
### Methods:
self.set_head(head)
self.set_body(body)
"""
self.lang: str = lang
self.template: str = "{doc_type}{html}"
self._head: Head = self.set_head()
self._body: Body = self.set_body()
self.head: Head = self.set_head(head) if not bool(head) else Head()
self.body: Body = self.set_body(body) if not bool(body) else Body()

def __str__(self) -> str:
"""
Expand Down Expand Up @@ -87,7 +100,7 @@ def html(self) -> Html:
Html: The Html element of the HTML page.
"""
html = Html(
tag_content=f"{self._head}{self._body}",
tag_content=f"{self.head}{self.body}",
lang=self.lang,
)
return html
Expand All @@ -106,8 +119,8 @@ def set_head(self, *args, **props) -> Head:
content = ""
for tag in args:
content += str(tag)
self._head = Head(tag_content=content, **props)
return self._head
self.head = Head(tag_content=content, **props)
return self.head

def set_body(self, *args, **props) -> Body:
"""
Expand All @@ -123,8 +136,8 @@ def set_body(self, *args, **props) -> Body:
content = ""
for tag in args:
content += str(tag)
self._body = Body(tag_content=content, **props)
return self._body
self.body = Body(tag_content=content, **props)
return self.body

def pretty(self, html_content: Optional[Union[str, Element]] = "", encoding=None, formatter="minimal") -> str:
"""
Expand Down Expand Up @@ -152,8 +165,8 @@ def add_tag_to_head(self, *args) -> Head:
*args (str, Element): The list of contents to be added to the head section.
"""
for tag in args:
self._head.tag_content += str(tag)
return self._head
self.head.tag_content += str(tag)
return self.head

def add_tag_to_body(self, *args) -> Body:
"""
Expand All @@ -162,6 +175,19 @@ def add_tag_to_body(self, *args) -> Body:
Args:
*args (str, Element): The list of contents to be added to the body section.
"""

for tag in args:
self._body.tag_content += str(tag)
return self._body
self.body.tag_content += str(tag)
return self.body


def write(self, filename: str):
"""
Write the HTML content to a file.
Args:
filename (str): The name of the file to write to.
"""
with open(filename, "w") as f:
f.write(self.pretty())

0 comments on commit 5f17a9c

Please sign in to comment.