| 1 |
# Process  |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
from ..common.utils import isStrSpace, normalizeReference |
| 5 |
from ..token import Token |
| 6 |
from .state_inline import StateInline |
| 7 |
|
| 8 |
|
| 9 |
def image(state: StateInline, silent: bool) -> bool: |
| 10 |
label = None |
| 11 |
href = "" |
| 12 |
oldPos = state.pos |
| 13 |
max = state.posMax |
| 14 |
|
| 15 |
if state.src[state.pos] != "!": |
| 16 |
return False |
| 17 |
|
| 18 |
if state.pos + 1 < state.posMax and state.src[state.pos + 1] != "[": |
| 19 |
return False |
| 20 |
|
| 21 |
labelStart = state.pos + 2 |
| 22 |
labelEnd = state.md.helpers.parseLinkLabel(state, state.pos + 1, False) |
| 23 |
|
| 24 |
# parser failed to find ']', so it's not a valid link |
| 25 |
if labelEnd < 0: |
| 26 |
return False |
| 27 |
|
| 28 |
pos = labelEnd + 1 |
| 29 |
|
| 30 |
if pos < max and state.src[pos] == "(": |
| 31 |
# |
| 32 |
# Inline link |
| 33 |
# |
| 34 |
|
| 35 |
# [link]( <href> "title" ) |
| 36 |
# ^^ skipping these spaces |
| 37 |
pos += 1 |
| 38 |
while pos < max: |
| 39 |
ch = state.src[pos] |
| 40 |
if not isStrSpace(ch) and ch != "\n": |
| 41 |
break |
| 42 |
pos += 1 |
| 43 |
|
| 44 |
if pos >= max: |
| 45 |
return False |
| 46 |
|
| 47 |
# [link]( <href> "title" ) |
| 48 |
# ^^^^^^ parsing link destination |
| 49 |
start = pos |
| 50 |
res = state.md.helpers.parseLinkDestination(state.src, pos, state.posMax) |
| 51 |
if res.ok: |
| 52 |
href = state.md.normalizeLink(res.str) |
| 53 |
if state.md.validateLink(href): |
| 54 |
pos = res.pos |
| 55 |
else: |
| 56 |
href = "" |
| 57 |
|
| 58 |
# [link]( <href> "title" ) |
| 59 |
# ^^ skipping these spaces |
| 60 |
start = pos |
| 61 |
while pos < max: |
| 62 |
ch = state.src[pos] |
| 63 |
if not isStrSpace(ch) and ch != "\n": |
| 64 |
break |
| 65 |
pos += 1 |
| 66 |
|
| 67 |
# [link]( <href> "title" ) |
| 68 |
# ^^^^^^^ parsing link title |
| 69 |
res = state.md.helpers.parseLinkTitle(state.src, pos, state.posMax) |
| 70 |
if pos < max and start != pos and res.ok: |
| 71 |
title = res.str |
| 72 |
pos = res.pos |
| 73 |
|
| 74 |
# [link]( <href> "title" ) |
| 75 |
# ^^ skipping these spaces |
| 76 |
while pos < max: |
| 77 |
ch = state.src[pos] |
| 78 |
if not isStrSpace(ch) and ch != "\n": |
| 79 |
break |
| 80 |
pos += 1 |
| 81 |
else: |
| 82 |
title = "" |
| 83 |
|
| 84 |
if pos >= max or state.src[pos] != ")": |
| 85 |
state.pos = oldPos |
| 86 |
return False |
| 87 |
|
| 88 |
pos += 1 |
| 89 |
|
| 90 |
else: |
| 91 |
# |
| 92 |
# Link reference |
| 93 |
# |
| 94 |
if "references" not in state.env: |
| 95 |
return False |
| 96 |
|
| 97 |
# /* [ */ |
| 98 |
if pos < max and state.src[pos] == "[": |
| 99 |
start = pos + 1 |
| 100 |
pos = state.md.helpers.parseLinkLabel(state, pos) |
| 101 |
if pos >= 0: |
| 102 |
label = state.src[start:pos] |
| 103 |
pos += 1 |
| 104 |
else: |
| 105 |
pos = labelEnd + 1 |
| 106 |
else: |
| 107 |
pos = labelEnd + 1 |
| 108 |
|
| 109 |
# covers label == '' and label == undefined |
| 110 |
# (collapsed reference link and shortcut reference link respectively) |
| 111 |
if not label: |
| 112 |
label = state.src[labelStart:labelEnd] |
| 113 |
|
| 114 |
label = normalizeReference(label) |
| 115 |
|
| 116 |
ref = state.env["references"].get(label, None) |
| 117 |
if not ref: |
| 118 |
state.pos = oldPos |
| 119 |
return False |
| 120 |
|
| 121 |
href = ref["href"] |
| 122 |
title = ref["title"] |
| 123 |
|
| 124 |
# |
| 125 |
# We found the end of the link, and know for a fact it's a valid link |
| 126 |
# so all that's left to do is to call tokenizer. |
| 127 |
# |
| 128 |
if not silent: |
| 129 |
content = state.src[labelStart:labelEnd] |
| 130 |
|
| 131 |
tokens: list[Token] = [] |
| 132 |
state.md.inline.parse(content, state.md, state.env, tokens) |
| 133 |
|
| 134 |
token = state.push("image", "img", 0) |
| 135 |
token.attrs = {"src": href, "alt": ""} |
| 136 |
token.children = tokens or None |
| 137 |
token.content = content |
| 138 |
|
| 139 |
if title: |
| 140 |
token.attrSet("title", title) |
| 141 |
|
| 142 |
# note, this is not part of markdown-it JS, but is useful for renderers |
| 143 |
if label and state.md.options.get("store_labels", False): |
| 144 |
token.meta["label"] = label |
| 145 |
|
| 146 |
state.pos = pos |
| 147 |
state.posMax = max |
| 148 |
return True |