Registry / data / tree-sitter-html

tree-sitter-html

JSON →
library0.23.2pypypi✓ verified 23d ago

tree-sitter-html provides the HTML grammar for the Tree-sitter parsing system. It enables parsing HTML code into a concrete syntax tree, facilitating static analysis, syntax highlighting, and code transformation. The library is actively developed with regular updates, currently at version 0.23.2.

pip install tree-sitter tree-sitter-html
INSTALL
IMPORT
SIG · TREE-SITTER-HTML
T
tree-sitter-html
datapythonv0.23.2
Install
1.6s avg
Import
11ms
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.23.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.012s · 19.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.006s · 20MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Language, Parser
from tree_sitter import Language, Parser import tree_sitter_html
from tree_sitter_html import Language, Parser
`tree_sitter_html` provides the grammar object via `tree_sitter_html.language()`, while `Language` and `Parser` classes come from the core `tree_sitter` library.

This quickstart demonstrates how to initialize a `tree-sitter` parser with the `tree-sitter-html` grammar and parse a basic HTML string into a syntax tree, then print its S-expression representation.

import tree_sitter import tree_sitter_html # Load the HTML language grammar HTML_LANGUAGE = tree_sitter.Language(tree_sitter_html.language()) # Initialize the parser and set the language parser = tree_sitter.Parser() parser.set_language(HTML_LANGUAGE) # Sample HTML code (must be bytes for tree-sitter) html_code = b"<!DOCTYPE html>\n<html><body><h1>Hello, Tree-sitter!</h1></body></html>" # Parse the code tree = parser.parse(html_code) # Get the root node of the syntax tree root_node = tree.root_node # Print a S-expression representation of the tree print(root_node.sexp())
Debug
Known issues
gotchaThe `tree-sitter` core library is a mandatory dependency and must be installed alongside `tree-sitter-html` for the Python bindings to function.
fix
Ensure both `tree-sitter` and `tree-sitter-html` are installed: `pip install tree-sitter tree-sitter-html`.
affects: All versions
gotchaGrammar updates can lead to changes in the generated Abstract Syntax Tree (AST), potentially affecting existing queries or code analysis logic. Common issues include unexpected handling of whitespace, attribute values, or void elements.
fix
Thoroughly test parsing results with your specific HTML structures after updating the grammar. Refer to GitHub issues for known parsing quirks.
affects: All versions
gotchaWhen `tree-sitter-html` is used for parsing HTML embedded within other languages (e.g., PHP, JavaScript in `script` tags, CSS in `style` tags), ensure that the corresponding `tree-sitter` grammars (e.g., `tree-sitter-javascript`, `tree-sitter-css`) are also installed for complete and accurate highlighting/parsing of injected languages.
fix
Install all relevant language grammars for files containing multiple embedded languages: `pip install tree-sitter-html tree-sitter-javascript tree-sitter-css`.
affects: All versions
deprecatedOlder examples might show `tree_sitter.Language.build_library()` for loading grammars. This method is deprecated for pre-compiled Python wheels (like `tree-sitter-html`).
fix
Instead of `Language.build_library()`, directly pass the grammar's `language()` function to `tree_sitter.Language()`: `HTML_LANGUAGE = tree_sitter.Language(tree_sitter_html.language())`.
affects: py-tree-sitter versions ~0.21.x and later
Upgrade
Version history
0.23.2latest on PyPI · released Nov 11, 2024
Audit
Dependencies
tree-sitterrequiredThis package provides the HTML grammar; the core `tree-sitter` Python library is required for parsing functionality and Python bindings.
Agent activity
15 hits · last 30 days
node
12
OpenAI (training)
2
Resources
tree-sitter-html — pip install tree-sitter-html · libregistry