_fenced.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import re
  2. from typing import TYPE_CHECKING, List, Match, Optional
  3. from ._base import BaseDirective, DirectiveParser, DirectivePlugin
  4. if TYPE_CHECKING:
  5. from ..block_parser import BlockParser
  6. from ..core import BlockState
  7. from ..markdown import Markdown
  8. __all__ = ["FencedDirective"]
  9. _type_re = re.compile(r"^ *\{[a-zA-Z0-9_-]+\}")
  10. _directive_re = re.compile(
  11. r"\{(?P<type>[a-zA-Z0-9_-]+)\} *(?P<title>[^\n]*)(?:\n|$)"
  12. r"(?P<options>(?:\:[a-zA-Z0-9_-]+\: *[^\n]*\n+)*)"
  13. r"\n*(?P<text>(?:[^\n]*\n+)*)"
  14. )
  15. class FencedParser(DirectiveParser):
  16. name = "fenced_directive"
  17. @staticmethod
  18. def parse_type(m: Match[str]) -> str:
  19. return m.group("type")
  20. @staticmethod
  21. def parse_title(m: Match[str]) -> str:
  22. return m.group("title")
  23. @staticmethod
  24. def parse_content(m: Match[str]) -> str:
  25. return m.group("text")
  26. class FencedDirective(BaseDirective):
  27. """A **fenced** style of directive looks like a fenced code block, it is
  28. inspired by markdown-it-docutils. The syntax looks like:
  29. .. code-block:: text
  30. ```{directive-type} title
  31. :option-key: option value
  32. :option-key: option value
  33. content text here
  34. ```
  35. To use ``FencedDirective``, developers can add it into plugin list in
  36. the :class:`Markdown` instance:
  37. .. code-block:: python
  38. import mistune
  39. from mistune.directives import FencedDirective, Admonition
  40. md = mistune.create_markdown(plugins=[
  41. # ...
  42. FencedDirective([Admonition()]),
  43. ])
  44. FencedDirective is using >= 3 backticks or curly-brackets for the fenced
  45. syntax. Developers can change it to other characters, e.g. colon:
  46. .. code-block:: python
  47. directive = FencedDirective([Admonition()], ':')
  48. And then the directive syntax would look like:
  49. .. code-block:: text
  50. ::::{note} Nesting directives
  51. You can nest directives by ensuring the start and end fence matching
  52. the length. For instance, in this example, the admonition is started
  53. with 4 colons, then it should end with 4 colons.
  54. You can nest another admonition with other length of colons except 4.
  55. :::{tip} Longer outermost fence
  56. It would be better that you put longer markers for the outer fence,
  57. and shorter markers for the inner fence. In this example, we put 4
  58. colons outsie, and 3 colons inside.
  59. :::
  60. ::::
  61. :param plugins: list of directive plugins
  62. :param markers: characters to determine the fence, default is backtick
  63. and curly-bracket
  64. """
  65. parser = FencedParser
  66. def __init__(self, plugins: List[DirectivePlugin], markers: str = "`~") -> None:
  67. super(FencedDirective, self).__init__(plugins)
  68. self.markers = markers
  69. _marker_pattern = "|".join(re.escape(c) for c in markers)
  70. self.directive_pattern = (
  71. r"^(?P<fenced_directive_mark>(?:" + _marker_pattern + r"){3,})"
  72. r"\{[a-zA-Z0-9_-]+\}"
  73. )
  74. def _process_directive(self, block: "BlockParser", marker: str, start: int, state: "BlockState") -> Optional[int]:
  75. mlen = len(marker)
  76. cursor_start = start + len(marker)
  77. _end_pattern = (
  78. r"^ {0,3}" + marker[0] + "{" + str(mlen) + r",}"
  79. r"[ \t]*(?:\n|$)"
  80. )
  81. _end_re = re.compile(_end_pattern, re.M)
  82. _end_m = _end_re.search(state.src, cursor_start)
  83. if _end_m:
  84. text = state.src[cursor_start : _end_m.start()]
  85. end_pos = _end_m.end()
  86. else:
  87. text = state.src[cursor_start:]
  88. end_pos = state.cursor_max
  89. m = _directive_re.match(text)
  90. if not m:
  91. return None
  92. self.parse_method(block, m, state)
  93. return end_pos
  94. def parse_directive(self, block: "BlockParser", m: Match[str], state: "BlockState") -> Optional[int]:
  95. marker = m.group("fenced_directive_mark")
  96. return self._process_directive(block, marker, m.start(), state)
  97. def parse_fenced_code(self, block: "BlockParser", m: Match[str], state: "BlockState") -> Optional[int]:
  98. info = m.group("fenced_3")
  99. if not info or not _type_re.match(info):
  100. return block.parse_fenced_code(m, state)
  101. if state.depth() >= block.max_nested_level:
  102. return block.parse_fenced_code(m, state)
  103. marker = m.group("fenced_2")
  104. return self._process_directive(block, marker, m.start(), state)
  105. def __call__(self, md: "Markdown") -> None:
  106. super(FencedDirective, self).__call__(md)
  107. if self.markers == "`~":
  108. md.block.register("fenced_code", None, self.parse_fenced_code)
  109. else:
  110. self.register_block_parser(md, "fenced_code")