Coverage for liitos/figures.py: 100.00%

31 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-13 17:41:25 +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 = f'[width={round(rescale, 2)}\\textwidth,height={round(rescale, 2)}\\textheight]' 

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

35 else: 

36 outgoing.append(line) 

37 modus = 'copy' 

38 rescale = NO_RESCALE 

39 else: 

40 outgoing.append(line) 

41 

42 return outgoing