Skip to content

Commit

Permalink
Move comment ag
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed Mar 3, 2024
1 parent 5fa158c commit e976533
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 56 deletions.
5 changes: 3 additions & 2 deletions pyhtml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@
"""
# Disable Flake8, since it really doesn't like our docstring above
# flake8: noqa
from .__tag_base import Tag, Comment
from .__tag_base import Tag

from .__tags import input_, object_

__all__ = [
'DangerousRawHtml',
'Tag',
'DangerousRawHtml',
'Comment',
'input_',
'object_',
Expand Down Expand Up @@ -377,6 +377,7 @@

from .__tags import (
DangerousRawHtml,
Comment,
html,
base,
head,
Expand Down
51 changes: 0 additions & 51 deletions pyhtml/__tag_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,57 +116,6 @@ def __repr__(self) -> str:
return self.render()


class Comment(Tag):
"""
An HTML comment.
Renders as:
```html
<!-- [comment text] -->
```
Note that this does not render as a `<comment>` tag
"""
def __init__(self, text: str) -> None:
"""
An HTML comment.
Renders as:
```html
<!-- [comment text] -->
```
Note that this does not render as a `<comment>` tag
"""
self.comment_data = text
super().__init__()

def __call__(self):
raise TypeError('Comment tags are not callable')

def _get_tag_name(self) -> str:
# Ignore coverage since this is only implemented to satisfy inheritance
# and is never used since we override _render
return '!--' # pragma: no cover

def _render(self) -> list[str]:
"""
Override of render, to render comments
"""

return [
'<!--',
*util.increase_indent(
util.escape_string(self.comment_data).splitlines(),
# FIXME: Yucky magic number
2,
),
'-->'
]


class SelfClosingTag(Tag):
"""
Self-closing tags don't contain child elements
Expand Down
2 changes: 2 additions & 0 deletions pyhtml/__tags/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
from .renames import input_, object_
from .input import input
from .dangerous_raw_html import DangerousRawHtml
from .comment import Comment

# Copy this into pyhtml/__tags/__init__.py
__all__ = [
# This one is generated by hand
'input',
# These are extra features of PyHTML enhanced
'DangerousRawHtml',
'Comment',
# These two are renamed
'input_',
'object_',
Expand Down
58 changes: 58 additions & 0 deletions pyhtml/__tags/comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
# PyHTML Enhanced / Tags / Comment
Definition for the comment tag.
"""
from ..__tag_base import Tag
from .. import __util as util


class Comment(Tag):
"""
An HTML comment.
Renders as:
```html
<!-- [comment text] -->
```
Note that this does not render as a `<comment>` tag
"""
def __init__(self, text: str) -> None:
"""
An HTML comment.
Renders as:
```html
<!-- [comment text] -->
```
Note that this does not render as a `<comment>` tag
"""
self.comment_data = text
super().__init__()

def __call__(self):
raise TypeError('Comment tags are not callable')

def _get_tag_name(self) -> str:
# Ignore coverage since this is only implemented to satisfy inheritance
# and is never used since we override _render
return '!--' # pragma: no cover

def _render(self) -> list[str]:
"""
Override of render, to render comments
"""

return [
'<!--',
*util.increase_indent(
util.escape_string(self.comment_data).splitlines(),
# FIXME: Yucky magic number
2,
),
'-->'
]
6 changes: 3 additions & 3 deletions tests/code_gen_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""
import pytest
import pyhtml
from pyhtml import Tag, Comment
from pyhtml import Tag, Comment, DangerousRawHtml


all_tags = [
Expand All @@ -20,8 +20,8 @@
and type(getattr(pyhtml, i)) == type
# That are a kind of Tag
and issubclass(getattr(pyhtml, i), Tag)
# And aren't a comment (since comments require named args)
and not issubclass(getattr(pyhtml, i), Comment)
# And aren't a PyHTML feature (since comments require named args)
and not issubclass(getattr(pyhtml, i), (Comment, DangerousRawHtml))
)
]

Expand Down

0 comments on commit e976533

Please sign in to comment.