Skip to content

Commit f3c9364

Browse files
Document rendering control methods
1 parent 368fd68 commit f3c9364

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,43 @@ create a class deriving from `Tag`.
147147

148148
```
149149

150+
#### Tag base classes
151+
152+
You can derive from various other classes to get more control over how your tag
153+
is rendered:
154+
155+
* `Tag`: default rendering.
156+
157+
* `SelfClosingTag`: tag is self-closing, meaning that no child elements are
158+
accepted.
159+
160+
* `WhitespaceSensitiveTag`: tag is whitespace-sensitive, meaning that its
161+
child elements are not indented.
162+
163+
#### Class properties
164+
165+
* `children`: child elements
166+
* `attributes`: element attributes
167+
168+
#### Rendering control functions
169+
170+
You can also override various functions to control the existing rendering.
171+
172+
* `_get_tag_name`: return the name to use for the tag. For example returning
173+
`"foo"` would produce `<foo>`.
174+
175+
* `_get_default_attributes`: return the default values for attributes.
176+
177+
* `_get_tag_pre_content`: return the pre-content for the tag. For example, the
178+
`<html>` tag uses this to add the `<!DOCTYPE html>` before the opening tag.
179+
180+
* `_escape_children`: return whether the string child elements should be
181+
escaped to prevent HTML injection.
182+
183+
* `_render`: render the element and its children, returning the list of lines
184+
to use for the output. Overriding this should be a last resort, as it is easy
185+
to subtly break the rendering process if you aren't careful.
186+
150187
Refer to the documentation for `Tag` for more information.
151188

152189
## Differences to PyHTML

pyhtml/__init__.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"""
22
# PyHTML Enhanced
33
4-
v2.0.3
5-
64
A library for building HTML documents with a simple and learnable syntax,
75
inspired by (and similar to)
86
[Cenk Altı's PyHTML library](https://github.com/cenkalti/pyhtml), but
@@ -35,6 +33,7 @@
3533
... ),
3634
... )
3735
>>> print(str(my_website))
36+
<!DOCTYPE html>
3837
<html>
3938
<head>
4039
<title>
@@ -149,6 +148,43 @@
149148
150149
```
151150
151+
#### Tag base classes
152+
153+
You can derive from various other classes to get more control over how your tag
154+
is rendered:
155+
156+
* `Tag`: default rendering.
157+
158+
* `SelfClosingTag`: tag is self-closing, meaning that no child elements are
159+
accepted.
160+
161+
* `WhitespaceSensitiveTag`: tag is whitespace-sensitive, meaning that its
162+
child elements are not indented.
163+
164+
#### Class properties
165+
166+
* `children`: child elements
167+
* `attributes`: element attributes
168+
169+
#### Rendering control functions
170+
171+
You can also override various functions to control the existing rendering.
172+
173+
* `_get_tag_name`: return the name to use for the tag. For example returning
174+
`"foo"` would produce `<foo>`.
175+
176+
* `_get_default_attributes`: return the default values for attributes.
177+
178+
* `_get_tag_pre_content`: return the pre-content for the tag. For example, the
179+
`<html>` tag uses this to add the `<!DOCTYPE html>` before the opening tag.
180+
181+
* `_escape_children`: return whether the string child elements should be
182+
escaped to prevent HTML injection.
183+
184+
* `_render`: render the element and its children, returning the list of lines
185+
to use for the output. Overriding this should be a last resort, as it is easy
186+
to subtly break the rendering process if you aren't careful.
187+
152188
Refer to the documentation for `Tag` for more information.
153189
154190
## Differences to PyHTML
@@ -162,6 +198,7 @@
162198
>>> p.br
163199
<class 'pyhtml.__tags.generated.br'>
164200
>>> print(str(p.html(p.body(p.br))))
201+
<!DOCTYPE html>
165202
<html>
166203
<body>
167204
<br/>
@@ -254,7 +291,6 @@
254291
Documentation is copied from MDN Web Docs, and is license under
255292
[CC-BY-SA-2.5](https://creativecommons.org/licenses/by-sa/2.5/). A copy of the
256293
license text is available in [LICENSE_DOCS.md](https://github.com/COMP1010UNSW/pyhtml-enhanced/blob/main/LICENSE_DOCS.md)
257-
258294
"""
259295
# Disable Flake8, since it really doesn't like our docstring above
260296
# flake8: noqa

0 commit comments

Comments
 (0)