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

33 statements  

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

1# Process html entity - {, ¯, ", ... 

2import re 

3 

4from ..common.entities import entities 

5from ..common.utils import fromCodePoint, isValidEntityCode 

6from .state_inline import StateInline 

7 

8DIGITAL_RE = re.compile(r"^&#((?:x[a-f0-9]{1,6}|[0-9]{1,7}));", re.IGNORECASE) 

9NAMED_RE = re.compile(r"^&([a-z][a-z0-9]{1,31});", re.IGNORECASE) 

10 

11 

12def entity(state: StateInline, silent: bool): 

13 pos = state.pos 

14 maximum = state.posMax 

15 

16 if state.srcCharCode[pos] != 0x26: # /* & */ 

17 return False 

18 

19 if (pos + 1) < maximum: 

20 ch = state.srcCharCode[pos + 1] 

21 

22 if ch == 0x23: # /* # */ 

23 match = DIGITAL_RE.search(state.src[pos:]) 

24 if match: 

25 if not silent: 

26 match1 = match.group(1) 

27 code = ( 

28 int(match1[1:], 16) 

29 if match1[0].lower() == "x" 

30 else int(match1, 10) 

31 ) 

32 state.pending += ( 

33 fromCodePoint(code) 

34 if isValidEntityCode(code) 

35 else fromCodePoint(0xFFFD) 

36 ) 

37 

38 state.pos += len(match.group(0)) 

39 return True 

40 

41 else: 

42 match = NAMED_RE.search(state.src[pos:]) 

43 if match: 

44 if match.group(1) in entities: 

45 if not silent: 

46 state.pending += entities[match.group(1)] 

47 state.pos += len(match.group(0)) 

48 return True 

49 

50 if not silent: 

51 state.pending += "&" 

52 state.pos += 1 

53 return True