components.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # $Id: components.py 9037 2022-03-05 23:31:10Z milde $
  2. # Author: David Goodger <goodger@python.org>
  3. # Copyright: This module has been placed in the public domain.
  4. """
  5. Docutils component-related transforms.
  6. """
  7. from docutils.transforms import Transform
  8. __docformat__ = 'reStructuredText'
  9. class Filter(Transform):
  10. """
  11. Include or exclude elements which depend on a specific Docutils component.
  12. For use with `nodes.pending` elements. A "pending" element's dictionary
  13. attribute ``details`` must contain the keys "component" and "format". The
  14. value of ``details['component']`` must match the type name of the
  15. component the elements depend on (e.g. "writer"). The value of
  16. ``details['format']`` is the name of a specific format or context of that
  17. component (e.g. "html"). If the matching Docutils component supports that
  18. format or context, the "pending" element is replaced by the contents of
  19. ``details['nodes']`` (a list of nodes); otherwise, the "pending" element
  20. is removed.
  21. For example, up to version 0.17, the reStructuredText "meta"
  22. directive created a "pending" element containing a "meta" element
  23. (in ``pending.details['nodes']``).
  24. Only writers (``pending.details['component'] == 'writer'``)
  25. supporting the "html", "latex", or "odf" formats
  26. (``pending.details['format'] == 'html,latex,odf'``) included the
  27. "meta" element; it was deleted from the output of all other writers.
  28. This transform is no longer used by Docutils, it may be removed in future.
  29. """
  30. # TODO: clean up or keep this for 3rd party (or possible future) use?
  31. # (GM 2021-05-18)
  32. default_priority = 780
  33. def apply(self):
  34. pending = self.startnode
  35. component_type = pending.details['component'] # 'reader' or 'writer'
  36. formats = (pending.details['format']).split(',')
  37. component = self.document.transformer.components[component_type]
  38. for format in formats:
  39. if component.supports(format):
  40. pending.replace_self(pending.details['nodes'])
  41. break
  42. else:
  43. pending.parent.remove(pending)