1
0

writer_aux.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # $Id: writer_aux.py 9037 2022-03-05 23:31:10Z milde $
  2. # Author: Lea Wiemann <LeWiemann@gmail.com>
  3. # Copyright: This module has been placed in the public domain.
  4. """
  5. Auxiliary transforms mainly to be used by Writer components.
  6. This module is called "writer_aux" because otherwise there would be
  7. conflicting imports like this one::
  8. from docutils import writers
  9. from docutils.transforms import writers
  10. """
  11. __docformat__ = 'reStructuredText'
  12. import warnings
  13. from docutils import nodes, languages
  14. from docutils.transforms import Transform
  15. class Compound(Transform):
  16. """
  17. .. warning:: This transform is not used by Docutils since Dec 2010
  18. and will be removed in Docutils 0.21 or later.
  19. Flatten all compound paragraphs. For example, transform ::
  20. <compound>
  21. <paragraph>
  22. <literal_block>
  23. <paragraph>
  24. into ::
  25. <paragraph>
  26. <literal_block classes="continued">
  27. <paragraph classes="continued">
  28. """
  29. default_priority = 910
  30. def __init__(self, document, startnode=None):
  31. warnings.warn('docutils.transforms.writer_aux.Compound is deprecated'
  32. ' and will be removed in Docutils 0.21 or later.',
  33. DeprecationWarning, stacklevel=2)
  34. super().__init__(document, startnode)
  35. def apply(self):
  36. for compound in self.document.findall(nodes.compound):
  37. first_child = True
  38. for child in compound:
  39. if first_child:
  40. if not isinstance(child, nodes.Invisible):
  41. first_child = False
  42. else:
  43. child['classes'].append('continued')
  44. # Substitute children for compound.
  45. compound.replace_self(compound[:])
  46. class Admonitions(Transform):
  47. """
  48. Transform specific admonitions, like this:
  49. <note>
  50. <paragraph>
  51. Note contents ...
  52. into generic admonitions, like this::
  53. <admonition classes="note">
  54. <title>
  55. Note
  56. <paragraph>
  57. Note contents ...
  58. The admonition title is localized.
  59. """
  60. default_priority = 920
  61. def apply(self):
  62. language = languages.get_language(self.document.settings.language_code,
  63. self.document.reporter)
  64. for node in self.document.findall(nodes.Admonition):
  65. node_name = node.__class__.__name__
  66. # Set class, so that we know what node this admonition came from.
  67. node['classes'].append(node_name)
  68. if not isinstance(node, nodes.admonition):
  69. # Specific admonition. Transform into a generic admonition.
  70. admonition = nodes.admonition(node.rawsource, *node.children,
  71. **node.attributes)
  72. title = nodes.title('', language.labels[node_name])
  73. admonition.insert(0, title)
  74. node.replace_self(admonition)