__init__.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """
  2. mistune
  3. ~~~~~~~
  4. A fast yet powerful Python Markdown parser with renderers and
  5. plugins, compatible with sane CommonMark rules.
  6. Documentation: https://mistune.lepture.com/
  7. """
  8. from typing import Any, Dict, Iterable, List, Optional, Tuple, Union, Literal
  9. from .block_parser import BlockParser
  10. from .core import BaseRenderer, BlockState, InlineState
  11. from .inline_parser import InlineParser
  12. from .markdown import Markdown
  13. from .plugins import Plugin, PluginRef, import_plugin
  14. from .renderers.html import HTMLRenderer
  15. from .util import escape, escape_url, safe_entity, unikey
  16. RendererRef = Union[Literal["html", "ast"], BaseRenderer]
  17. def create_markdown(
  18. escape: bool = True,
  19. hard_wrap: bool = False,
  20. renderer: Optional[RendererRef] = "html",
  21. plugins: Optional[Iterable[PluginRef]] = None,
  22. ) -> Markdown:
  23. """Create a Markdown instance based on the given condition.
  24. :param escape: Boolean. If using html renderer, escape html.
  25. :param hard_wrap: Boolean. Break every new line into ``<br>``.
  26. :param renderer: renderer instance, default is HTMLRenderer.
  27. :param plugins: List of plugins.
  28. This method is used when you want to re-use a Markdown instance::
  29. markdown = create_markdown(
  30. escape=False,
  31. hard_wrap=True,
  32. )
  33. # re-use markdown function
  34. markdown('.... your text ...')
  35. """
  36. if renderer == "ast":
  37. # explicit and more similar to 2.x's API
  38. renderer = None
  39. elif renderer == "html":
  40. renderer = HTMLRenderer(escape=escape)
  41. inline = InlineParser(hard_wrap=hard_wrap)
  42. real_plugins: Optional[Iterable[Plugin]] = None
  43. if plugins is not None:
  44. real_plugins = [import_plugin(n) for n in plugins]
  45. return Markdown(renderer=renderer, inline=inline, plugins=real_plugins)
  46. html: Markdown = create_markdown(escape=False, plugins=["strikethrough", "footnotes", "table", "speedup"])
  47. __cached_parsers: Dict[Tuple[bool, Optional[RendererRef], Optional[Iterable[Any]]], Markdown] = {}
  48. def markdown(
  49. text: str,
  50. escape: bool = True,
  51. renderer: Optional[RendererRef] = "html",
  52. plugins: Optional[Iterable[Any]] = None,
  53. ) -> Union[str, List[Dict[str, Any]]]:
  54. if renderer == "ast":
  55. # explicit and more similar to 2.x's API
  56. renderer = None
  57. key = (escape, renderer, plugins)
  58. if key in __cached_parsers:
  59. return __cached_parsers[key](text)
  60. md = create_markdown(escape=escape, renderer=renderer, plugins=plugins)
  61. # improve the speed for markdown parser creation
  62. __cached_parsers[key] = md
  63. return md(text)
  64. __all__ = [
  65. "Markdown",
  66. "HTMLRenderer",
  67. "BlockParser",
  68. "BlockState",
  69. "BaseRenderer",
  70. "InlineParser",
  71. "InlineState",
  72. "escape",
  73. "escape_url",
  74. "safe_entity",
  75. "unikey",
  76. "html",
  77. "create_markdown",
  78. "markdown",
  79. ]
  80. __version__ = "3.2.0"
  81. __homepage__ = "https://mistune.lepture.com/"