Coverage for liitos/labels.py: 83.55%
114 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-28 20:14:46 +00:00
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-28 20:14:46 +00:00
1from collections.abc import Iterable
2from typing import Union
4from liitos import log
6NO_LABEL = 'no-label-found-ERROR'
9def is_include_graphics(text: str) -> bool:
10 """Only DRY."""
11 ntoken = r'\pandocbounded' # nosec B105
12 pos_after = len(ntoken)
13 if len(text) > len(ntoken) and text.startswith(ntoken) and text[pos_after] in ('[', '{'):
14 return True
15 token = r'\includegraphics' # nosec B105
16 pos_after = len(token)
17 return len(text) > len(token) and text.startswith(token) and text[pos_after] in ('[', '{')
20def extract_image_path(include_graphics_line: str) -> str:
21 """We had bugs, so we isolate in a function to simplify following upstream fashions."""
22 igl = include_graphics_line
23 if igl and 'pandocbounded{' in igl and ',alt={' in igl:
24 return igl.split('}]{', 1)[1].rstrip().rstrip('}')
25 if igl and 'pandocbounded{' in igl: 25 ↛ 26line 25 didn't jump to line 26 because the condition on line 25 was never true
26 return igl.split('{', 2)[2].rstrip().rstrip('}')
27 if igl and '{' in igl:
28 return igl.split('{', 1)[1].rstrip().rstrip('}')
29 else:
30 return 'IMAGE_PATH_NOT_FOUND'
33def inject(incoming: Iterable[str], lookup: Union[dict[str, str], None] = None) -> list[str]:
34 """Later alligator."""
35 outgoing = []
36 modus = 'copy'
37 label = NO_LABEL
38 figure: list[str] = []
39 caption: list[str] = []
40 precondition = r'\begin{figure}'
41 precondition_met = False
42 for slot, line in enumerate(incoming):
43 if line.startswith(precondition) and not precondition_met:
44 log.info(f'start of a figure environment at line #{slot + 1}')
45 precondition_met = True
46 outgoing.append(line)
47 continue
49 if modus == 'copy':
50 if is_include_graphics(line) and not precondition_met:
51 log.warning(f'graphics include outside of a figure environment at line #{slot + 1}')
52 log.debug(f'line#{slot + 1}|{line.rstrip()}')
53 log.info('trying to fix temporarily ... watch for marker MISSING-CAPTION-IN-MARKDOWN')
54 adhoc_label = 'FIX-AT-SOURCE'
55 try:
56 lab = line.split('{', 1)[1]
57 lab = lab.rsplit('.', 1)[0]
58 lab = lab.rsplit('/', 1)[1]
59 adhoc_label = r'\label{fig:' + lab + '}'
60 log.info(adhoc_label)
61 except Exception as err:
62 log.error(f'failed to extract generic label from {line.strip()} with err: {err}')
63 captain = 'MISSING-CAPTION-IN-MARKDOWN'
64 try:
65 token = extract_image_path(line)
66 log.debug(f'- looking up key ({token}) for captions ...')
67 if lookup is not None:
68 cand = lookup.get(token, None)
69 if cand is not None:
70 captain = cand
71 log.debug(f' + found ({captain})')
72 else:
73 log.debug(f' ? no match for key ({token})')
74 else:
75 log.debug(' + no lut?')
76 except Exception as err:
77 log.error(
78 f'failed to extract file path token for caption lookup from {line.strip()} with err: {err}'
79 )
80 outgoing.append(r'\begin{figure}' + '\n')
81 outgoing.append(r'\centering' + '\n')
82 outgoing.append(line)
83 outgoing.append(r'\caption{' + captain + ' ' + adhoc_label + '}' + '\n')
84 outgoing.append(r'\end{figure}' + '\n')
85 elif is_include_graphics(line) and precondition_met:
86 log.info(f'within a figure environment at line #{slot + 1}')
87 log.info(line.rstrip())
88 modus = 'figure'
89 figure = [line]
90 try:
91 lab = line.split('{', 1)[1]
92 lab = lab.rsplit('.', 1)[0]
93 lab = lab.rsplit('/', 1)[1]
94 label = r'\label{fig:' + lab + '}'
95 log.info(label)
96 except Exception as err:
97 log.error(f'failed to extract generic label from {line.strip()} with err: {err}')
98 caption = []
99 else:
100 log.debug(f'- copying {slot + 1 :3d}|{line.rstrip()}')
101 outgoing.append(line)
102 continue
104 if modus == 'figure': 104 ↛ 129line 104 didn't jump to line 129 because the condition on line 104 was always true
105 if line.startswith(r'\caption{'):
106 log.info(f'- found the caption start at line #{slot + 1}')
107 caption.append(line)
108 if not line.strip().endswith('}'): 108 ↛ 109line 108 didn't jump to line 109 because the condition on line 108 was never true
109 log.info(f'- multi line caption at line #{slot + 1}')
110 modus = 'caption'
111 elif line.startswith(r'\end{figure}'): 111 ↛ 125line 111 didn't jump to line 125 because the condition on line 111 was always true
112 log.info(f'end of figure env detected at line #{slot + 1}')
113 outgoing.extend(figure)
114 caption_text = '\n'.join(caption)
115 if r'\label{' not in caption_text: 115 ↛ 117line 115 didn't jump to line 117 because the condition on line 115 was always true
116 caption_text = f"{caption_text.rstrip().rstrip('}')} {label}" + '}'
117 if caption_text == caption_text.rstrip(): 117 ↛ 119line 117 didn't jump to line 119 because the condition on line 117 was always true
118 caption_text += '\n'
119 outgoing.append(caption_text)
120 outgoing.append(line)
121 modus = 'copy'
122 label = NO_LABEL
123 precondition_met = False
124 else:
125 log.debug(f'- figure continues at line #{slot + 1}')
126 figure.append(line)
127 continue
129 if modus == 'caption':
130 caption.append(line)
131 if line.strip().endswith(r'}'):
132 log.info(f'- caption read at line #{slot + 1}')
133 modus = 'figure'
134 continue
136 return outgoing