Coverage for liitos/figures.py: 93.85%
47 statements
« prev ^ index » next coverage.py v7.6.8, created at 2024-11-25 15:36:16 +00:00
« prev ^ index » next coverage.py v7.6.8, created at 2024-11-25 15:36:16 +00:00
1from collections.abc import Iterable
2from typing import Union
4from liitos import log
6NO_RESCALE: Union[float, int] = 0
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 line = line.rstrip('\n')
16 if modus == 'copy':
17 if line.startswith(r'\scale='):
18 log.info(f'trigger a scale mod for the next figure environment at line #{slot + 1}|{line}')
19 modus = 'scale'
20 scale = line # only for reporting wil not pass the filter
21 try:
22 sca = scale.split('=', 1)[1].strip() # \scale = 75\% --> 75\%
23 rescale = float(sca.replace(r'\%', '')) / 100 if r'\%' in sca else float(sca)
24 except Exception as err:
25 log.error(f'failed to parse scale value from {line.strip()} with err: {err}')
26 else:
27 outgoing.append(line)
29 else: # if modus == 'scale':
30 if line.startswith(r'\includegraphics{'):
31 if rescale != NO_RESCALE:
32 log.info(f'- found the scale target start at line #{slot + 1}|{line}')
33 target = line.replace(r'\includegraphics', '')
34 option = (
35 f'[width={round(rescale, 2)}\\textwidth,height={round(rescale, 2)}'
36 '\\textheight,keepaspectratio]'
37 )
38 outgoing.append(f'\\includegraphics{option}{target}')
39 else:
40 outgoing.append(line)
41 modus = 'copy'
42 rescale = NO_RESCALE
43 elif r'\pandocbounded{\includegraphics' in line:
44 if rescale != NO_RESCALE: 44 ↛ 59line 44 didn't jump to line 59 because the condition on line 44 was always true
45 log.info(f'- found the scale target start at line #{slot + 1}|{line}')
46 target = line.replace(r'\pandocbounded{\includegraphics', '').replace('[keepaspectratio]', '')
47 parts = target.split('}}')
48 rest = ''
49 if len(parts) >= 1: 49 ↛ 53line 49 didn't jump to line 53
50 inside = parts[0] + '}'
51 if len(parts) == 2: 51 ↛ 53line 51 didn't jump to line 53
52 rest = parts[1].lstrip('}')
53 option = (
54 f'[width={round(rescale, 2)}\\textwidth,height={round(rescale, 2)}'
55 '\\textheight,keepaspectratio]'
56 )
57 outgoing.append(f'\\pandocbounded{ \\includegraphics{option}{inside}} {rest}')
58 else:
59 outgoing.append(line)
60 modus = 'copy'
61 rescale = NO_RESCALE
62 else:
63 outgoing.append(line)
65 return outgoing