|
| 1 | +"""Superscript tag plugin. |
| 2 | +
|
| 3 | +Ported by Elijah Greenstein from https://github.com/markdown-it/markdown-it-sup |
| 4 | +cf. Subscript tag plugin, https://mdit-py-plugins.readthedocs.io/en/latest/#subscripts |
| 5 | +
|
| 6 | +MIT License |
| 7 | +Copyright (c) 2014-2015 Vitaly Puzrin, Alex Kocharin. |
| 8 | +
|
| 9 | +Permission is hereby granted, free of charge, to any person |
| 10 | +obtaining a copy of this software and associated documentation |
| 11 | +files (the "Software"), to deal in the Software without |
| 12 | +restriction, including without limitation the rights to use, |
| 13 | +copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | +copies of the Software, and to permit persons to whom the |
| 15 | +Software is furnished to do so, subject to the following |
| 16 | +conditions: |
| 17 | +
|
| 18 | +The above copyright notice and this permission notice shall be |
| 19 | +included in all copies or substantial portions of the Software. |
| 20 | +
|
| 21 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 22 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 23 | +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 24 | +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 25 | +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 26 | +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 27 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 28 | +OTHER DEALINGS IN THE SOFTWARE. |
| 29 | +""" |
| 30 | + |
| 31 | +from markdown_it import MarkdownIt |
| 32 | +from markdown_it.rules_inline import StateInline |
| 33 | + |
| 34 | +from mdit_py_plugins.utils import UNESCAPE_RE, WHITESPACE_RE |
| 35 | + |
| 36 | + |
| 37 | +def superscript_plugin(md: MarkdownIt) -> None: |
| 38 | + """Superscript (``<sup>``) tag plugin for Markdown-It-Py. |
| 39 | +
|
| 40 | + This plugin is ported from `markdown-it-sup |
| 41 | + <https://github.com/markdown-it/markdown-it-sup>`_. Markup is based on the |
| 42 | + `Pandoc superscript extension |
| 43 | + <https://pandoc.org/MANUAL.html#superscripts-and-subscripts>`_. |
| 44 | +
|
| 45 | + Place superscripted text within caret ``^`` characters. You must escape any |
| 46 | + spaces in the superscripted text. Note that you cannot use newline or tab |
| 47 | + characters, and that nested markup is not supported. |
| 48 | +
|
| 49 | + Example usage: |
| 50 | +
|
| 51 | + >>> from markdown_it import MarkdownIt |
| 52 | + >>> from mdit_py_plugins.superscript import superscript_plugin |
| 53 | + >>> md = MarkdownIt().use(superscript_plugin) |
| 54 | + >>> md.render("1^st^") |
| 55 | + '<p>1<sup>st</sup></p>\\n' |
| 56 | + >>> md.render("this^text\\\\ has\\\\ spaces^") |
| 57 | + '<p>this<sup>text has spaces</sup></p>\\n' |
| 58 | + """ |
| 59 | + |
| 60 | + def superscript(state: StateInline, silent: bool) -> bool: |
| 61 | + """Parse inline text for superscripted text between caret ``^`` characters.""" |
| 62 | + maximum = state.posMax |
| 63 | + start = state.pos |
| 64 | + |
| 65 | + if ord(state.src[start]) != 0x5E: # Check if char is `^` |
| 66 | + return False |
| 67 | + if silent: # Do not run any pairs in validation mode |
| 68 | + return False |
| 69 | + if start + 2 >= maximum: |
| 70 | + return False |
| 71 | + |
| 72 | + state.pos = start + 1 |
| 73 | + found = False |
| 74 | + |
| 75 | + while state.pos < maximum: |
| 76 | + if ord(state.src[state.pos]) == 0x5E: # Check if char is `^` |
| 77 | + found = True |
| 78 | + break |
| 79 | + state.md.inline.skipToken(state) |
| 80 | + |
| 81 | + if (not found) or (start + 1 == state.pos): |
| 82 | + state.pos = start |
| 83 | + return False |
| 84 | + |
| 85 | + content = state.src[start + 1 : state.pos] |
| 86 | + |
| 87 | + # Do not allow unescaped spaces/newlines inside |
| 88 | + if WHITESPACE_RE.search(content) is not None: |
| 89 | + state.pos = start |
| 90 | + return False |
| 91 | + |
| 92 | + # Found! |
| 93 | + state.posMax = state.pos |
| 94 | + state.pos = start + 1 |
| 95 | + |
| 96 | + # Earlier we checked !silent, but this implementation does not need it |
| 97 | + token_so = state.push("sup_open", "sup", 1) |
| 98 | + token_so.markup = "^" |
| 99 | + |
| 100 | + token_t = state.push("text", "", 0) |
| 101 | + token_t.content = UNESCAPE_RE.sub(r"\1", content) |
| 102 | + |
| 103 | + token_sc = state.push("sup_close", "sup", -1) |
| 104 | + token_sc.markup = "^" |
| 105 | + |
| 106 | + state.pos = state.posMax + 1 |
| 107 | + state.posMax = maximum |
| 108 | + return True |
| 109 | + |
| 110 | + md.inline.ruler.after("emphasis", "sup", superscript) |
0 commit comments