Ali2206 commited on
Commit
e78764c
·
verified ·
1 Parent(s): 2dde43f

Update utils/helpers.py

Browse files
Files changed (1) hide show
  1. utils/helpers.py +43 -1
utils/helpers.py CHANGED
@@ -24,4 +24,46 @@ def standardize_language(language):
24
  "spanish": "es",
25
  "german": "de"
26
  }
27
- return language_map.get(language, re.sub(r'[^a-z]', '', language)[:2])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  "spanish": "es",
25
  "german": "de"
26
  }
27
+ return language_map.get(language, re.sub(r'[^a-z]', '', language)[:2])
28
+
29
+ def escape_latex_special_chars(text):
30
+ """Escape special characters for LaTeX compatibility."""
31
+ if not isinstance(text, str) or not text:
32
+ return ""
33
+ latex_special_chars = {
34
+ '&': r'\&',
35
+ '%': r'\%',
36
+ '$': r'\$',
37
+ '#': r'\#',
38
+ '_': r'\_',
39
+ '{': r'\{',
40
+ '}': r'\}',
41
+ '~': r'\textasciitilde{}',
42
+ '^': r'\textasciicircum{}',
43
+ '\\': r'\textbackslash{}'
44
+ }
45
+ return ''.join(latex_special_chars.get(c, c) for c in text)
46
+
47
+ def hyphenate_long_strings(text, max_length=30):
48
+ """Insert hyphens into long strings to prevent LaTeX table overflow."""
49
+ if not isinstance(text, str) or not text:
50
+ return ""
51
+ if len(text) <= max_length:
52
+ return text
53
+ # Insert a zero-width space (allowing LaTeX to break) every max_length characters
54
+ result = []
55
+ for i in range(0, len(text), max_length):
56
+ chunk = text[i:i + max_length]
57
+ result.append(chunk)
58
+ return r"\-".join(result)
59
+
60
+ def format_timestamp(timestamp_str):
61
+ """Format a timestamp string for display in LaTeX."""
62
+ if not timestamp_str:
63
+ return "N/A"
64
+ try:
65
+ # Handle ISO format timestamps
66
+ dt = datetime.fromisoformat(timestamp_str.replace('Z', '+00:00'))
67
+ return dt.strftime("%Y-%m-%d %H:%M")
68
+ except (ValueError, TypeError):
69
+ return str(timestamp_str)