_rst.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import re
  2. from typing import TYPE_CHECKING, Match, Optional
  3. from ._base import BaseDirective, DirectiveParser
  4. if TYPE_CHECKING:
  5. from ..block_parser import BlockParser
  6. from ..core import BlockState
  7. from ..markdown import Markdown
  8. __all__ = ["RSTDirective"]
  9. _directive_re = re.compile(
  10. r"\.\.( +)(?P<type>[a-zA-Z0-9_-]+)\:\: *(?P<title>[^\n]*)(?:\n|$)"
  11. r"(?P<options>(?: \1 {0,3}\:[a-zA-Z0-9_-]+\: *[^\n]*\n+)*)"
  12. r"\n*(?P<text>(?: \1 {0,3}[^\n]*\n+)*)"
  13. )
  14. class RSTParser(DirectiveParser):
  15. name = "rst_directive"
  16. @staticmethod
  17. def parse_type(m: Match[str]) -> str:
  18. return m.group("type")
  19. @staticmethod
  20. def parse_title(m: Match[str]) -> str:
  21. return m.group("title")
  22. @staticmethod
  23. def parse_content(m: Match[str]) -> str:
  24. full_content = m.group(0)
  25. text = m.group("text")
  26. pretext = full_content[: -len(text)]
  27. leading = len(m.group(1)) + 2
  28. return "\n".join(line[leading:] for line in text.splitlines()) + "\n"
  29. class RSTDirective(BaseDirective):
  30. """A RST style of directive syntax is inspired by reStructuredText.
  31. The syntax is very powerful that you can define a lot of custom
  32. features on your own. The syntax looks like:
  33. .. code-block:: text
  34. .. directive-type:: directive value
  35. :option-key: option value
  36. :option-key: option value
  37. content text here
  38. To use ``RSTDirective``, developers can add it into plugin list in
  39. the :class:`Markdown` instance:
  40. .. code-block:: python
  41. import mistune
  42. from mistune.directives import RSTDirective, Admonition
  43. md = mistune.create_markdown(plugins=[
  44. # ...
  45. RSTDirective([Admonition()]),
  46. ])
  47. """
  48. parser = RSTParser
  49. directive_pattern = r"^\.\. +[a-zA-Z0-9_-]+\:\:"
  50. def parse_directive(self, block: "BlockParser", m: Match[str], state: "BlockState") -> Optional[int]:
  51. m2 = _directive_re.match(state.src, state.cursor)
  52. if not m2:
  53. return None
  54. self.parse_method(block, m2, state)
  55. return m2.end()
  56. def __call__(self, markdown: "Markdown") -> None:
  57. super(RSTDirective, self).__call__(markdown)
  58. self.register_block_parser(markdown)