Skip to content

Commit 80e2f65

Browse files
committed
improve bibtex output
1 parent 716dde3 commit 80e2f65

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

install_html_meta_data.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import argparse
22
import json
33
import os
4+
import re
45

56
import mysql.connector
67
from lxml.html import parse, document_fromstring
@@ -73,6 +74,52 @@ def replace_string_between(s, start_str, end_str, with_str):
7374
return s
7475

7576

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+
76123
def bibtex_entry_to_html(entry):
77124
"""Inspired by bibtex2html.py get_entry_output()"""
78125
# rip out whitespace
@@ -88,6 +135,8 @@ def field(f):
88135
raise RuntimeError(
89136
f"bibtex tag {entry['bibtag']} is missing" f" required field {f}"
90137
)
138+
if isinstance(entry[f], str):
139+
return bibtex_field_to_html(entry[f])
91140
return entry[f]
92141

93142
out = ["\n<li id=%s>\n" % field("bibtag")]

0 commit comments

Comments
 (0)