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