You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """A collection of modules for building different kinds of trees from HTML
  2. documents.
  3. To create a treebuilder for a new type of tree, you need to do
  4. implement several things:
  5. 1. A set of classes for various types of elements: Document, Doctype, Comment,
  6. Element. These must implement the interface of ``base.treebuilders.Node``
  7. (although comment nodes have a different signature for their constructor,
  8. see ``treebuilders.etree.Comment``) Textual content may also be implemented
  9. as another node type, or not, as your tree implementation requires.
  10. 2. A treebuilder object (called ``TreeBuilder`` by convention) that inherits
  11. from ``treebuilders.base.TreeBuilder``. This has 4 required attributes:
  12. * ``documentClass`` - the class to use for the bottommost node of a document
  13. * ``elementClass`` - the class to use for HTML Elements
  14. * ``commentClass`` - the class to use for comments
  15. * ``doctypeClass`` - the class to use for doctypes
  16. It also has one required method:
  17. * ``getDocument`` - Returns the root node of the complete document tree
  18. 3. If you wish to run the unit tests, you must also create a ``testSerializer``
  19. method on your treebuilder which accepts a node and returns a string
  20. containing Node and its children serialized according to the format used in
  21. the unittests
  22. """
  23. from __future__ import absolute_import, division, unicode_literals
  24. from .._utils import default_etree
  25. treeBuilderCache = {}
  26. def getTreeBuilder(treeType, implementation=None, **kwargs):
  27. """Get a TreeBuilder class for various types of trees with built-in support
  28. :arg treeType: the name of the tree type required (case-insensitive). Supported
  29. values are:
  30. * "dom" - A generic builder for DOM implementations, defaulting to a
  31. xml.dom.minidom based implementation.
  32. * "etree" - A generic builder for tree implementations exposing an
  33. ElementTree-like interface, defaulting to xml.etree.cElementTree if
  34. available and xml.etree.ElementTree if not.
  35. * "lxml" - A etree-based builder for lxml.etree, handling limitations
  36. of lxml's implementation.
  37. :arg implementation: (Currently applies to the "etree" and "dom" tree
  38. types). A module implementing the tree type e.g. xml.etree.ElementTree
  39. or xml.etree.cElementTree.
  40. :arg kwargs: Any additional options to pass to the TreeBuilder when
  41. creating it.
  42. Example:
  43. >>> from html5lib.treebuilders import getTreeBuilder
  44. >>> builder = getTreeBuilder('etree')
  45. """
  46. treeType = treeType.lower()
  47. if treeType not in treeBuilderCache:
  48. if treeType == "dom":
  49. from . import dom
  50. # Come up with a sane default (pref. from the stdlib)
  51. if implementation is None:
  52. from xml.dom import minidom
  53. implementation = minidom
  54. # NEVER cache here, caching is done in the dom submodule
  55. return dom.getDomModule(implementation, **kwargs).TreeBuilder
  56. elif treeType == "lxml":
  57. from . import etree_lxml
  58. treeBuilderCache[treeType] = etree_lxml.TreeBuilder
  59. elif treeType == "etree":
  60. from . import etree
  61. if implementation is None:
  62. implementation = default_etree
  63. # NEVER cache here, caching is done in the etree submodule
  64. return etree.getETreeModule(implementation, **kwargs).TreeBuilder
  65. else:
  66. raise ValueError("""Unrecognised treebuilder "%s" """ % treeType)
  67. return treeBuilderCache.get(treeType)