|
1 | 1 | """ |
2 | 2 | # PyHTML Enhanced |
3 | 3 |
|
4 | | -v2.0.3 |
5 | | -
|
6 | 4 | A library for building HTML documents with a simple and learnable syntax, |
7 | 5 | inspired by (and similar to) |
8 | 6 | [Cenk Altı's PyHTML library](https://github.com/cenkalti/pyhtml), but |
|
35 | 33 | ... ), |
36 | 34 | ... ) |
37 | 35 | >>> print(str(my_website)) |
| 36 | +<!DOCTYPE html> |
38 | 37 | <html> |
39 | 38 | <head> |
40 | 39 | <title> |
|
149 | 148 |
|
150 | 149 | ``` |
151 | 150 |
|
| 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 | +
|
152 | 188 | Refer to the documentation for `Tag` for more information. |
153 | 189 |
|
154 | 190 | ## Differences to PyHTML |
|
162 | 198 | >>> p.br |
163 | 199 | <class 'pyhtml.__tags.generated.br'> |
164 | 200 | >>> print(str(p.html(p.body(p.br)))) |
| 201 | +<!DOCTYPE html> |
165 | 202 | <html> |
166 | 203 | <body> |
167 | 204 | <br/> |
|
254 | 291 | Documentation is copied from MDN Web Docs, and is license under |
255 | 292 | [CC-BY-SA-2.5](https://creativecommons.org/licenses/by-sa/2.5/). A copy of the |
256 | 293 | license text is available in [LICENSE_DOCS.md](https://github.com/COMP1010UNSW/pyhtml-enhanced/blob/main/LICENSE_DOCS.md) |
257 | | -
|
258 | 294 | """ |
259 | 295 | # Disable Flake8, since it really doesn't like our docstring above |
260 | 296 | # flake8: noqa |
|
0 commit comments