Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/markdown_it/rules_inline/image.py: 99%

88 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:07 +0000

1# Process ![image](<src> "title") 

2from __future__ import annotations 

3 

4from ..common.utils import isSpace, normalizeReference 

5from ..token import Token 

6from .state_inline import StateInline 

7 

8 

9def image(state: StateInline, silent: bool): 

10 label = None 

11 href = "" 

12 oldPos = state.pos 

13 max = state.posMax 

14 

15 # /* ! */ 

16 if state.srcCharCode[state.pos] != 0x21: 

17 return False 

18 # /* [ */ 

19 if state.pos + 1 < state.posMax and state.srcCharCode[state.pos + 1] != 0x5B: 

20 return False 

21 

22 labelStart = state.pos + 2 

23 labelEnd = state.md.helpers.parseLinkLabel(state, state.pos + 1, False) 

24 

25 # parser failed to find ']', so it's not a valid link 

26 if labelEnd < 0: 

27 return False 

28 

29 pos = labelEnd + 1 

30 # /* ( */ 

31 if pos < max and state.srcCharCode[pos] == 0x28: 

32 # 

33 # Inline link 

34 # 

35 

36 # [link]( <href> "title" ) 

37 # ^^ skipping these spaces 

38 pos += 1 

39 while pos < max: 

40 code = state.srcCharCode[pos] 

41 if not isSpace(code) and code != 0x0A: 

42 break 

43 pos += 1 

44 

45 if pos >= max: 

46 return False 

47 

48 # [link]( <href> "title" ) 

49 # ^^^^^^ parsing link destination 

50 start = pos 

51 res = state.md.helpers.parseLinkDestination(state.src, pos, state.posMax) 

52 if res.ok: 

53 href = state.md.normalizeLink(res.str) 

54 if state.md.validateLink(href): 

55 pos = res.pos 

56 else: 

57 href = "" 

58 

59 # [link]( <href> "title" ) 

60 # ^^ skipping these spaces 

61 start = pos 

62 while pos < max: 

63 code = state.srcCharCode[pos] 

64 if not isSpace(code) and code != 0x0A: 

65 break 

66 pos += 1 

67 

68 # [link]( <href> "title" ) 

69 # ^^^^^^^ parsing link title 

70 res = state.md.helpers.parseLinkTitle(state.src, pos, state.posMax) 

71 if pos < max and start != pos and res.ok: 

72 title = res.str 

73 pos = res.pos 

74 

75 # [link]( <href> "title" ) 

76 # ^^ skipping these spaces 

77 while pos < max: 

78 code = state.srcCharCode[pos] 

79 if not isSpace(code) and code != 0x0A: 

80 break 

81 pos += 1 

82 else: 

83 title = "" 

84 

85 # /* ) */ 

86 if pos >= max or state.srcCharCode[pos] != 0x29: 

87 state.pos = oldPos 

88 return False 

89 

90 pos += 1 

91 

92 else: 

93 # 

94 # Link reference 

95 # 

96 if "references" not in state.env: 

97 return False 

98 

99 # /* [ */ 

100 if pos < max and state.srcCharCode[pos] == 0x5B: 

101 start = pos + 1 

102 pos = state.md.helpers.parseLinkLabel(state, pos) 

103 if pos >= 0: 

104 label = state.src[start:pos] 

105 pos += 1 

106 else: 

107 pos = labelEnd + 1 

108 else: 

109 pos = labelEnd + 1 

110 

111 # covers label == '' and label == undefined 

112 # (collapsed reference link and shortcut reference link respectively) 

113 if not label: 

114 label = state.src[labelStart:labelEnd] 

115 

116 label = normalizeReference(label) 

117 

118 ref = state.env["references"].get(label, None) 

119 if not ref: 

120 state.pos = oldPos 

121 return False 

122 

123 href = ref["href"] 

124 title = ref["title"] 

125 

126 # 

127 # We found the end of the link, and know for a fact it's a valid link 

128 # so all that's left to do is to call tokenizer. 

129 # 

130 if not silent: 

131 content = state.src[labelStart:labelEnd] 

132 

133 tokens: list[Token] = [] 

134 state.md.inline.parse(content, state.md, state.env, tokens) 

135 

136 token = state.push("image", "img", 0) 

137 token.attrs = {"src": href, "alt": ""} 

138 token.children = tokens or None 

139 token.content = content 

140 

141 if title: 

142 token.attrSet("title", title) 

143 

144 # note, this is not part of markdown-it JS, but is useful for renderers 

145 if label and state.md.options.get("store_labels", False): 

146 token.meta["label"] = label 

147 

148 state.pos = pos 

149 state.posMax = max 

150 return True