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

146 statements  

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

1# Block quotes 

2from __future__ import annotations 

3 

4import logging 

5 

6from ..common.utils import isSpace 

7from .state_block import StateBlock 

8 

9LOGGER = logging.getLogger(__name__) 

10 

11 

12def blockquote(state: StateBlock, startLine: int, endLine: int, silent: bool): 

13 LOGGER.debug( 

14 "entering blockquote: %s, %s, %s, %s", state, startLine, endLine, silent 

15 ) 

16 

17 oldLineMax = state.lineMax 

18 pos = state.bMarks[startLine] + state.tShift[startLine] 

19 max = state.eMarks[startLine] 

20 

21 # if it's indented more than 3 spaces, it should be a code block 

22 if (state.sCount[startLine] - state.blkIndent) >= 4: 

23 return False 

24 

25 # check the block quote marker 

26 if state.srcCharCode[pos] != 0x3E: # /* > */ 

27 return False 

28 pos += 1 

29 

30 # we know that it's going to be a valid blockquote, 

31 # so no point trying to find the end of it in silent mode 

32 if silent: 

33 return True 

34 

35 # set offset past spaces and ">" 

36 initial = offset = state.sCount[startLine] + 1 

37 

38 try: 

39 second_char_code: int | None = state.srcCharCode[pos] 

40 except IndexError: 

41 second_char_code = None 

42 

43 # skip one optional space after '>' 

44 if second_char_code == 0x20: # /* space */ 

45 # ' > test ' 

46 # ^ -- position start of line here: 

47 pos += 1 

48 initial += 1 

49 offset += 1 

50 adjustTab = False 

51 spaceAfterMarker = True 

52 elif second_char_code == 0x09: # /* tab */ 

53 spaceAfterMarker = True 

54 

55 if (state.bsCount[startLine] + offset) % 4 == 3: 

56 # ' >\t test ' 

57 # ^ -- position start of line here (tab has width==1) 

58 pos += 1 

59 initial += 1 

60 offset += 1 

61 adjustTab = False 

62 else: 

63 # ' >\t test ' 

64 # ^ -- position start of line here + shift bsCount slightly 

65 # to make extra space appear 

66 adjustTab = True 

67 

68 else: 

69 spaceAfterMarker = False 

70 

71 oldBMarks = [state.bMarks[startLine]] 

72 state.bMarks[startLine] = pos 

73 

74 while pos < max: 

75 ch = state.srcCharCode[pos] 

76 

77 if isSpace(ch): 

78 if ch == 0x09: # / tab / 

79 offset += ( 

80 4 

81 - (offset + state.bsCount[startLine] + (1 if adjustTab else 0)) % 4 

82 ) 

83 else: 

84 offset += 1 

85 

86 else: 

87 break 

88 

89 pos += 1 

90 

91 oldBSCount = [state.bsCount[startLine]] 

92 state.bsCount[startLine] = ( 

93 state.sCount[startLine] + 1 + (1 if spaceAfterMarker else 0) 

94 ) 

95 

96 lastLineEmpty = pos >= max 

97 

98 oldSCount = [state.sCount[startLine]] 

99 state.sCount[startLine] = offset - initial 

100 

101 oldTShift = [state.tShift[startLine]] 

102 state.tShift[startLine] = pos - state.bMarks[startLine] 

103 

104 terminatorRules = state.md.block.ruler.getRules("blockquote") 

105 

106 oldParentType = state.parentType 

107 state.parentType = "blockquote" 

108 

109 # Search the end of the block 

110 # 

111 # Block ends with either: 

112 # 1. an empty line outside: 

113 # ``` 

114 # > test 

115 # 

116 # ``` 

117 # 2. an empty line inside: 

118 # ``` 

119 # > 

120 # test 

121 # ``` 

122 # 3. another tag: 

123 # ``` 

124 # > test 

125 # - - - 

126 # ``` 

127 

128 # for (nextLine = startLine + 1; nextLine < endLine; nextLine++) { 

129 nextLine = startLine + 1 

130 while nextLine < endLine: 

131 # check if it's outdented, i.e. it's inside list item and indented 

132 # less than said list item: 

133 # 

134 # ``` 

135 # 1. anything 

136 # > current blockquote 

137 # 2. checking this line 

138 # ``` 

139 isOutdented = state.sCount[nextLine] < state.blkIndent 

140 

141 pos = state.bMarks[nextLine] + state.tShift[nextLine] 

142 max = state.eMarks[nextLine] 

143 

144 if pos >= max: 

145 # Case 1: line is not inside the blockquote, and this line is empty. 

146 break 

147 

148 evaluatesTrue = state.srcCharCode[pos] == 0x3E and not isOutdented # /* > */ 

149 pos += 1 

150 if evaluatesTrue: 

151 # This line is inside the blockquote. 

152 

153 # set offset past spaces and ">" 

154 initial = offset = state.sCount[nextLine] + 1 

155 

156 try: 

157 next_char: int | None = state.srcCharCode[pos] 

158 except IndexError: 

159 next_char = None 

160 

161 # skip one optional space after '>' 

162 if next_char == 0x20: # /* space */ 

163 # ' > test ' 

164 # ^ -- position start of line here: 

165 pos += 1 

166 initial += 1 

167 offset += 1 

168 adjustTab = False 

169 spaceAfterMarker = True 

170 elif next_char == 0x09: # /* tab */ 

171 spaceAfterMarker = True 

172 

173 if (state.bsCount[nextLine] + offset) % 4 == 3: 

174 # ' >\t test ' 

175 # ^ -- position start of line here (tab has width==1) 

176 pos += 1 

177 initial += 1 

178 offset += 1 

179 adjustTab = False 

180 else: 

181 # ' >\t test ' 

182 # ^ -- position start of line here + shift bsCount slightly 

183 # to make extra space appear 

184 adjustTab = True 

185 

186 else: 

187 spaceAfterMarker = False 

188 

189 oldBMarks.append(state.bMarks[nextLine]) 

190 state.bMarks[nextLine] = pos 

191 

192 while pos < max: 

193 ch = state.srcCharCode[pos] 

194 

195 if isSpace(ch): 

196 if ch == 0x09: 

197 offset += ( 

198 4 

199 - ( 

200 offset 

201 + state.bsCount[nextLine] 

202 + (1 if adjustTab else 0) 

203 ) 

204 % 4 

205 ) 

206 else: 

207 offset += 1 

208 else: 

209 break 

210 

211 pos += 1 

212 

213 lastLineEmpty = pos >= max 

214 

215 oldBSCount.append(state.bsCount[nextLine]) 

216 state.bsCount[nextLine] = ( 

217 state.sCount[nextLine] + 1 + (1 if spaceAfterMarker else 0) 

218 ) 

219 

220 oldSCount.append(state.sCount[nextLine]) 

221 state.sCount[nextLine] = offset - initial 

222 

223 oldTShift.append(state.tShift[nextLine]) 

224 state.tShift[nextLine] = pos - state.bMarks[nextLine] 

225 

226 nextLine += 1 

227 continue 

228 

229 # Case 2: line is not inside the blockquote, and the last line was empty. 

230 if lastLineEmpty: 

231 break 

232 

233 # Case 3: another tag found. 

234 terminate = False 

235 

236 for terminatorRule in terminatorRules: 

237 if terminatorRule(state, nextLine, endLine, True): 

238 terminate = True 

239 break 

240 

241 if terminate: 

242 # Quirk to enforce "hard termination mode" for paragraphs; 

243 # normally if you call `tokenize(state, startLine, nextLine)`, 

244 # paragraphs will look below nextLine for paragraph continuation, 

245 # but if blockquote is terminated by another tag, they shouldn't 

246 state.lineMax = nextLine 

247 

248 if state.blkIndent != 0: 

249 # state.blkIndent was non-zero, we now set it to zero, 

250 # so we need to re-calculate all offsets to appear as 

251 # if indent wasn't changed 

252 oldBMarks.append(state.bMarks[nextLine]) 

253 oldBSCount.append(state.bsCount[nextLine]) 

254 oldTShift.append(state.tShift[nextLine]) 

255 oldSCount.append(state.sCount[nextLine]) 

256 state.sCount[nextLine] -= state.blkIndent 

257 

258 break 

259 

260 oldBMarks.append(state.bMarks[nextLine]) 

261 oldBSCount.append(state.bsCount[nextLine]) 

262 oldTShift.append(state.tShift[nextLine]) 

263 oldSCount.append(state.sCount[nextLine]) 

264 

265 # A negative indentation means that this is a paragraph continuation 

266 # 

267 state.sCount[nextLine] = -1 

268 

269 nextLine += 1 

270 

271 oldIndent = state.blkIndent 

272 state.blkIndent = 0 

273 

274 token = state.push("blockquote_open", "blockquote", 1) 

275 token.markup = ">" 

276 token.map = lines = [startLine, 0] 

277 

278 state.md.block.tokenize(state, startLine, nextLine) 

279 

280 token = state.push("blockquote_close", "blockquote", -1) 

281 token.markup = ">" 

282 

283 state.lineMax = oldLineMax 

284 state.parentType = oldParentType 

285 lines[1] = state.line 

286 

287 # Restore original tShift; this might not be necessary since the parser 

288 # has already been here, but just to make sure we can do that. 

289 for i, item in enumerate(oldTShift): 

290 state.bMarks[i + startLine] = oldBMarks[i] 

291 state.tShift[i + startLine] = item 

292 state.sCount[i + startLine] = oldSCount[i] 

293 state.bsCount[i + startLine] = oldBSCount[i] 

294 

295 state.blkIndent = oldIndent 

296 

297 return True