-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
54 lines (42 loc) · 1.5 KB
/
helpers.py
File metadata and controls
54 lines (42 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
helper.py - A Python module for a Helper class handling information retrieval.
Author: Wes Modes
Date: 2023
"""
import logging
import re
# Create a logger instance for the 'lookup' module
logger = logging.getLogger('helper_logger')
def super_strip(string):
"""
Trim leading/trailing empty lines, remove leading spaces/tabs, and spaces/tabs before triple backticks.
"""
# strip leading and trailing empty lines and spaces
string = string.strip()
# split string into lines
lines = string.splitlines()
in_code_block = False
compiled_lines = []
# Remove leading and trailing empty lines
# lines = [line for line in lines if line.strip()]
for line in lines:
# strip line to find code blocks
stripped_line = line.strip()
# flag when we are in a code block
if stripped_line.startswith('```') and not in_code_block:
in_code_block = True
compiled_lines.append(stripped_line)
elif stripped_line.endswith('```') and in_code_block:
in_code_block = False
compiled_lines.append(line)
elif in_code_block:
compiled_lines.append(line)
else:
# Remove leading spaces and tabs
trimmed_line = line.lstrip(' \t')
# if trimmed_line.count('"') % 2 == 1:
# in_code_block = True
compiled_lines.append(trimmed_line)
# Join trimmed lines
trimmed_string = '\n'.join(compiled_lines)
return trimmed_string