References
kTemplate.tag
Tag
HTML / SVG element generation class
Examples:
- void element, child=None (default)
>>> Tag('br')
<br />
- void Tag w/ attr
>>> Tag("img", src="http://img.url")
<img src="http://img.url" />
- empty string child -> Tag with end tag but no content
>>> Tag("script", child="", src="url")
<script src="url"></script>
- boolean attribute(attribute w/o value)
>>> Tag("option", "a", "selected")
<option selected>a</option>
- mix of boolean attribute and normal attribute
>>> Tag('a', 'foo', 'm-2', 'rounded', 'text-teal-400', href='bar')
<a m-2 rounded text-teal-400 href="bar">foo</a>
- nested Tag
>>> Tag("div", Tag("i", "x"))
<div><i>x</i></div>
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tag_name |
str
|
tag name |
required |
child |
str | Tag | None
|
inner text or other Tag, default None |
None
|
args |
list[str]
|
names of value-less attributes
- eg. |
()
|
kwargs |
dict
|
tag attributes, in form of |
{}
|
Source code in kTemplate/tag.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
__enter__()
enter context manager, append self to current context stack
Examples:
Source code in kTemplate/tag.py
__exit__(exc_type, exc_value, traceback)
exit context manager, pop self from current context stack
Source code in kTemplate/tag.py
__init__(tag_name: str | None, child: str | Self | None = None, *args, **kwargs)
initialize Tag, append to current context stack if exists
Source code in kTemplate/tag.py
__repr__()
string representation of the Tag
Source code in kTemplate/tag.py
add(child: str | Self, *args, **kwargs) -> Self
add child and attributes to current Tag
Examples:
>>> opt = Tag('option')
>>> opt.add('hello', 'selected', value='world')
<option selected value="world">hello</option>
Source code in kTemplate/tag.py
comment(comment: str) -> str
staticmethod
comment element
Examples:
doctype(kind: str = 'html') -> str
staticmethod
prepare_child(child: str | Self | None) -> list[Self | str] | None
kTemplate.gen_elements
Pre-generate HTML and SVG element to kTemplate/elements.py
We take pre-generating approach because Pylance
cannot infer the type of partial functions dynamically,
failing attempts included:
- update local variable with
locals().update()
- use
exec()
to create partial functions.
html_tags = 'a abbr acronym address applet area article aside audio b base basefont bdi bdo big blockquote body br button canvas caption center cite code col colgroup data datalist dd del details dfn dialog dir div dl dt em embed fieldset figcaption figure font footer form frame frameset h1 h2 h3 h4 h5 h6 head header hgroup hr html i iframe img input ins kbd label legend li link main map mark menu meta meter nav noframes noscript object ol optgroup option output p param picture pre progress q rp rt ruby s samp script search section select small source span strike strong style sub summary sup svg table tbody td template textarea tfoot th thead time title tr track tt u ul var video wbr'
module-attribute
source: https://www.w3schools.com/tags/
svg_tags = 'a animate animateMotion animateTransform circle clipPath defs desc ellipse feBlend feColorMatrix feComponentTransfer feComposite feConvolveMatrix feDiffuseLighting feDisplacementMap feDistantLight feDropShadow feFlood feFuncA feFuncB feFuncG feFuncR feGaussianBlur feImage feMerge feMergeNode feMorphology feOffset fePointLight feSpecularLighting feSpotLight feTile feTurbulence filter foreignObject g image line linearGradient marker mask metadata mpath path pattern polygon polyline radialGradient rect script set stop style svg switch symbol text textPath title tspan use view'
module-attribute
source: https://developer.mozilla.org/en-US/docs/Web/SVG/Element
generate_elements()
generate and write HTML and SVG elements to elements.py