codekingpro/portable-devtools
114k
1# Process 2from __future__ import annotations3 4from ..common.utils import isStrSpace, normalizeReference5from ..token import Token6from .state_inline import StateInline7 8 9def image(state: StateInline, silent: bool) -> bool:10 label = None11 href = ""12 oldPos = state.pos13 max = state.posMax14 15 if state.src[state.pos] != "!":16 return False17 18 if state.pos + 1 < state.posMax and state.src[state.pos + 1] != "[":19 return False20 21 labelStart = state.pos + 222 labelEnd = state.md.helpers.parseLinkLabel(state, state.pos + 1, False)23 24 # parser failed to find ']', so it's not a valid link25 if labelEnd < 0:26 return False27 28 pos = labelEnd + 129 30 if pos < max and state.src[pos] == "(":31 #32 # Inline link33 #34 35 # [link]( <href> "title" )36 # ^^ skipping these spaces37 pos += 138 while pos < max:39 ch = state.src[pos]40 if not isStrSpace(ch) and ch != "\n":41 break42 pos += 143 44 if pos >= max:45 return False46 47 # [link]( <href> "title" )48 # ^^^^^^ parsing link destination49 start = pos50 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.pos55 else:56 href = ""57 58 # [link]( <href> "title" )59 # ^^ skipping these spaces60 start = pos61 while pos < max:62 ch = state.src[pos]63 if not isStrSpace(ch) and ch != "\n":64 break65 pos += 166 67 # [link]( <href> "title" )68 # ^^^^^^^ parsing link title69 res = state.md.helpers.parseLinkTitle(state.src, pos, state.posMax)70 if pos < max and start != pos and res.ok:71 title = res.str72 pos = res.pos73 74 # [link]( <href> "title" )75 # ^^ skipping these spaces76 while pos < max:77 ch = state.src[pos]78 if not isStrSpace(ch) and ch != "\n":79 break80 pos += 181 else:82 title = ""83 84 if pos >= max or state.src[pos] != ")":85 state.pos = oldPos86 return False87 88 pos += 189 90 else:91 #92 # Link reference93 #94 if "references" not in state.env:95 return False96 97 # /* [ */98 if pos < max and state.src[pos] == "[":99 start = pos + 1100 pos = state.md.helpers.parseLinkLabel(state, pos)101 if pos >= 0:102 label = state.src[start:pos]103 pos += 1104 else:105 pos = labelEnd + 1106 else:107 pos = labelEnd + 1108 109 # covers label == '' and label == undefined110 # (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 = oldPos119 return False120 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 link126 # 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 None137 token.content = content138 139 if title:140 token.attrSet("title", title)141 142 # note, this is not part of markdown-it JS, but is useful for renderers143 if label and state.md.options.get("store_labels", False):144 token.meta["label"] = label145 146 state.pos = pos147 state.posMax = max148 return True149 