A parser for HTML templates.
Project description
HTML Template Parser
Modified version of Python's HTMLParser for HTML template parsing
🤔 For What?
The is an HTML template parser. It is a modified version of Python's HTMLParse library, expanded to handle template tags.
💾 Install
pip install html-template-parser
# or
poetry add html-template-parser
✨ How to Use
A basic usage example is remarkably similar to Python's HTMLParser:
from HtmlTemplateParser import Htp
from HtmlTemplateParser import AttributeParser
class MyAttributeParser(AttributeParser):
def handle_starttag_curly_perc(self, tag, attrs, props):
print("starttag_curly_perc", tag, attrs, props)
print(self.getpos())
def handle_endtag_curly_perc(self, tag, attrs, props):
print("endtag_curly_perc", tag, attrs, props)
def handle_value(self, value):
print("value", value)
class MyHTMLParser(Htp):
def handle_starttag(self, tag, attrs):
print("Encountered a start tag:", tag)
print(self.getpos())
MyAttributeParser(attrs).parse()
def handle_endtag(self, tag):
print("Encountered an end tag :", tag)
def handle_data(self, data):
print("Encountered some data :", data)
parser = MyHTMLParser()
parser.feed('<html><head><title>Test</title></head>'
'<body {% if this %}ok{% endif %}><h1>Parse me!</h1></body></html>')
🏷 Function Naming Conventions
Comments
- comment
<!-- -->
- comment_curly_hash
{# data #}
- comment_curly_two_exlaim
{{! data }}
- starttag_comment_curly_perc
{% comment "attrs" %}
- endtag_comment_curly_perc
{% endcomment %}
- comment_at_star
@* data *@
Structure
-
startendtag
< />
-
starttag
<
-
starttag_curly_perc
{% ... %}
-
starttag_curly_two_hash
{{#...}}
-
starttag_curly_four
{{{{...}}}}
-
endtag
<.../>
-
endtag_curly_perc
{% end.. %}
-
endtag_curly_two_slash
{{/...}}
-
endtag_curly_four_slash
{{{{/...}}}}
Data and Other
- unknown_decl
- charref
- entityref
- data
- curly_two
{{ ... }}
- slash_curly_two
\{{ ... }}
- curly_three
{{{ ... }}}
- decl
- pi
Modifiers
Modifiers such as ~
, !--
, -
, +
will show up as props on the tags.
Attributes
Attributes are passed from the Htp as a complete string to be parsed with the attribute parser.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
html-template-parser-1.3.0.tar.gz
(27.1 kB
view hashes)
Built Distribution
Close
Hashes for html-template-parser-1.3.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93d2dff6953df2958cfa73d30b44d9c33c203a5f5656a4ccbf6ac82b1e017e11 |
|
MD5 | 226e958e468057d15dcacd964bf79824 |
|
BLAKE2b-256 | 65fc0cc01e2e60dfe2883739bfc4d83e4f94a3cbe548118b5262cbbc3f502679 |
Close
Hashes for html_template_parser-1.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f8f4a4e9775793058dda192e3153261b5fd290383006f5247d4bfa6cddb7ffa |
|
MD5 | abc19365c4ae6790cf3596a1ad7fa3ef |
|
BLAKE2b-256 | ecd0a2a7e3d578033912cb186fe390eafcbb93d9f465d42ad3551cd62d6b08d4 |