Coverage for liitos/description_lists.py: 89.36%

35 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_OPTION: str = '' 

7 

8 

9def parse_options_command(slot: int, text_line: str) -> tuple[bool, str, str]: 

10 """Parse the \\option[style=multiline,leftmargin=6em].""" 

11 if text_line.startswith(r'\option['): 

12 log.info(f'trigger an option mod for the next description environment at line #{slot + 1}|{text_line}') 

13 try: 

14 # \option[style=multiline,leftmargin=6em] --> [style=multiline,leftmargin=6em] 

15 opt = text_line.split(r'\option', 1)[1].strip() 

16 log.info(f' -> parsed option as ({opt})') 

17 return True, f'%CONSIDERED_{text_line}', opt 

18 except Exception as err: 

19 log.error(f'failed to parse option value from {text_line.strip()} with err: {err}') 

20 return False, text_line, '' 

21 else: 

22 return False, text_line, '' 

23 

24 

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

26 """Later alligator. \\option[style=multiline,leftmargin=6em]""" 

27 outgoing = [] 

28 modus = 'copy' 

29 opt = NO_OPTION 

30 for slot, line in enumerate(incoming): 

31 if modus == 'copy': 

32 has_opt, text_line, opt = parse_options_command(slot, line) 

33 if has_opt: 

34 modus = 'option' 

35 else: 

36 outgoing.append(text_line) 

37 continue 

38 

39 # if modus == 'option': 

40 if line.startswith(r'\begin{description}'): 

41 if opt != NO_OPTION: 41 ↛ 45line 41 didn't jump to line 45, because the condition on line 41 was never false

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

43 outgoing.append(f'\\begin{ description} {opt}') 

44 else: 

45 outgoing.append(line) 

46 modus = 'copy' 

47 opt = NO_OPTION 

48 else: 

49 outgoing.append(line) 

50 

51 return outgoing