Skip to content

First pass at docs for html5lib.treeadapters #380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions html5lib/treeadapters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
"""Tree adapters let you convert from one tree structure to another

Example:

.. code-block:: python

import html5lib
from html5lib.treeadapters import genshi

doc = '<html><body>Hi!</body></html>'
treebuilder = html5lib.getTreeBuilder('etree')
parser = html5lib.HTMLParser(tree=treebuilder)
tree = parser.parse(doc)
TreeWalker = html5lib.getTreeWalker('etree')

genshi_tree = genshi.to_genshi(TreeWalker(tree))

"""
from __future__ import absolute_import, division, unicode_literals

from . import sax
Expand Down
7 changes: 7 additions & 0 deletions html5lib/treeadapters/genshi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@


def to_genshi(walker):
"""Convert a tree to a genshi tree

:arg walker: the treewalker to use to walk the tree to convert it

:returns: generator of genshi nodes

"""
text = []
for token in walker:
type = token["type"]
Expand Down
8 changes: 7 additions & 1 deletion html5lib/treeadapters/sax.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@


def to_sax(walker, handler):
"""Call SAX-like content handler based on treewalker walker"""
"""Call SAX-like content handler based on treewalker walker

:arg walker: the treewalker to use to walk the tree to convert it

:arg handler: SAX handler to use

"""
handler.startDocument()
for prefix, namespace in prefix_mapping.items():
handler.startPrefixMapping(prefix, namespace)
Expand Down