Coverage for liitos/figures.py: 93.75%

46 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-10-08 19:41:18 +00:00

1from collections.abc import Iterable 

2from typing import Union 

3 

4from liitos import log 

5 

6NO_RESCALE: Union[float, int] = 0 

7 

8 

9def scale(incoming: Iterable[str], lookup: Union[dict[str, str], None] = None) -> list[str]: 

10 """Later alligator.""" 

11 outgoing = [] 

12 modus = 'copy' 

13 rescale = NO_RESCALE 

14 for slot, line in enumerate(incoming): 

15 if modus == 'copy': 

16 if line.startswith(r'\scale='): 

17 log.info(f'trigger a scale mod for the next figure environment at line #{slot + 1}|{line}') 

18 modus = 'scale' 

19 scale = line # only for reporting wil not pass the filter 

20 try: 

21 sca = scale.split('=', 1)[1].strip() # \scale = 75\% --> 75\% 

22 rescale = float(sca.replace(r'\%', '')) / 100 if r'\%' in sca else float(sca) 

23 except Exception as err: 

24 log.error(f'failed to parse scale value from {line.strip()} with err: {err}') 

25 else: 

26 outgoing.append(line) 

27 

28 else: # if modus == 'scale': 

29 if line.startswith(r'\includegraphics{'): 

30 if rescale != NO_RESCALE: 

31 log.info(f'- found the scale target start at line #{slot + 1}|{line}') 

32 target = line.replace(r'\includegraphics', '') 

33 option = ( 

34 f'[width={round(rescale, 2)}\\textwidth,height={round(rescale, 2)}' 

35 '\\textheight,keepaspectratio]' 

36 ) 

37 outgoing.append(f'\\includegraphics{option}{target}') 

38 else: 

39 outgoing.append(line) 

40 modus = 'copy' 

41 rescale = NO_RESCALE 

42 elif r'\pandocbounded{\includegraphics' in line: 

43 if rescale != NO_RESCALE: 43 ↛ 58line 43 didn't jump to line 58 because the condition on line 43 was always true

44 log.info(f'- found the scale target start at line #{slot + 1}|{line}') 

45 target = line.replace(r'\pandocbounded{\includegraphics', '').replace('[keepaspectratio]', '') 

46 parts = target.split('}}') 

47 rest = '' 

48 if len(parts) >= 1: 48 ↛ 52line 48 didn't jump to line 52

49 inside = parts[0] + '}' 

50 if len(parts) == 2: 50 ↛ 52line 50 didn't jump to line 52

51 rest = parts[1].lstrip('}') 

52 option = ( 

53 f'[width={round(rescale, 2)}\\textwidth,height={round(rescale, 2)}' 

54 '\\textheight,keepaspectratio]' 

55 ) 

56 outgoing.append(f'\\pandocbounded{ \\includegraphics{option}{inside}} {rest}') 

57 else: 

58 outgoing.append(line) 

59 modus = 'copy' 

60 rescale = NO_RESCALE 

61 else: 

62 outgoing.append(line) 

63 

64 return outgoing