1
1
import argparse
2
2
import json
3
3
import os
4
+ import re
4
5
5
6
import mysql .connector
6
7
from lxml .html import parse , document_fromstring
@@ -73,6 +74,52 @@ def replace_string_between(s, start_str, end_str, with_str):
73
74
return s
74
75
75
76
77
+ def bibtex_field_to_html (text ):
78
+ # Function to process nested braces
79
+ def process_braces (match ):
80
+ content = match .group (1 )
81
+ while '{' in content :
82
+ content = re .sub (r'\{([^{}]*)\}' , lambda m : m .group (1 ), content )
83
+ return content
84
+
85
+ # Remove outermost braces and process nested ones
86
+ text = re .sub (r'\{([^{}]*)\}' , process_braces , text )
87
+
88
+ # Replace \& with &
89
+ text = text .replace (r'\&' , '&' )
90
+
91
+ # Replace accented characters
92
+ accent_map = {
93
+ r"\'e" : "é" ,
94
+ r"\'a" : "á" ,
95
+ r'\"o' : "ö" ,
96
+ r'\"a' : "ä" ,
97
+ r"\'i" : "í" ,
98
+ r"\'o" : "ó" ,
99
+ r"\'u" : "ú" ,
100
+ r'\"u' : "ü" ,
101
+ r"\`e" : "è" ,
102
+ r"\`a" : "à" ,
103
+ r"\^e" : "ê" ,
104
+ r"\^a" : "â" ,
105
+ r"\~n" : "ñ" ,
106
+ r"\c{c}" : "ç"
107
+ }
108
+ for latex , html in accent_map .items ():
109
+ text = text .replace (latex , html )
110
+
111
+ # Replace other LaTeX special characters (extend as needed)
112
+ latex_to_html = {
113
+ r'\textbf{' : '<strong>' ,
114
+ r'\textit{' : '<em>' ,
115
+ r'}' : '</strong></em>' # Closing tag for both bold and italic
116
+ }
117
+ for latex , html in latex_to_html .items ():
118
+ text = text .replace (latex , html )
119
+
120
+ return text
121
+
122
+
76
123
def bibtex_entry_to_html (entry ):
77
124
"""Inspired by bibtex2html.py get_entry_output()"""
78
125
# rip out whitespace
@@ -88,6 +135,8 @@ def field(f):
88
135
raise RuntimeError (
89
136
f"bibtex tag { entry ['bibtag' ]} is missing" f" required field { f } "
90
137
)
138
+ if isinstance (entry [f ], str ):
139
+ return bibtex_field_to_html (entry [f ])
91
140
return entry [f ]
92
141
93
142
out = ["\n <li id=%s>\n " % field ("bibtag" )]
0 commit comments