html_inline.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Process html tags
  2. from ..common.html_re import HTML_TAG_RE
  3. from ..common.utils import isLinkClose, isLinkOpen
  4. from .state_inline import StateInline
  5. def isLetter(ch: int) -> bool:
  6. lc = ch | 0x20 # to lower case
  7. # /* a */ and /* z */
  8. return (lc >= 0x61) and (lc <= 0x7A)
  9. def html_inline(state: StateInline, silent: bool) -> bool:
  10. pos = state.pos
  11. if not state.md.options.get("html", None):
  12. return False
  13. # Check start
  14. maximum = state.posMax
  15. if state.src[pos] != "<" or pos + 2 >= maximum:
  16. return False
  17. # Quick fail on second char
  18. ch = state.src[pos + 1]
  19. if ch not in ("!", "?", "/") and not isLetter(ord(ch)): # /* / */
  20. return False
  21. match = HTML_TAG_RE.search(state.src[pos:])
  22. if not match:
  23. return False
  24. if not silent:
  25. token = state.push("html_inline", "", 0)
  26. token.content = state.src[pos : pos + len(match.group(0))]
  27. if isLinkOpen(token.content):
  28. state.linkLevel += 1
  29. if isLinkClose(token.content):
  30. state.linkLevel -= 1
  31. state.pos += len(match.group(0))
  32. return True