Skip to content

Commit 1902867

Browse files
committed
TL: best served by oneself
1 parent 23c65a6 commit 1902867

1 file changed

Lines changed: 17 additions & 23 deletions

File tree

bin/issue_to_bibtex.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import argparse
22
import fileinput
3-
from html import unescape
43
import re
54
import urllib
65
import requests
6+
import traceback
77

88
from arxivcheck.arxiv import get_arxiv_info
99

@@ -12,28 +12,21 @@
1212
from bibtexparser.bwriter import BibTexWriter
1313

1414

15-
def normalize_month_fields(bib):
16-
def replace_month(match):
17-
prefix, value = match.groups()
18-
value = unescape(value).strip()
19-
if value.startswith("{") and value.endswith("}"):
20-
return match.group(0)
21-
if value.startswith('"') and value.endswith('"'):
22-
value = value[1:-1].strip()
23-
value = value.strip("'\"{}")
24-
value = re.sub(r"[^A-Za-z]", "", value)
25-
return f"{prefix}{{{value}}},"
26-
27-
return re.sub(
28-
r"(^\s*month\s*=\s*)([^,\n]+)\s*,",
29-
replace_month,
30-
bib,
31-
flags=re.MULTILINE | re.IGNORECASE,
32-
)
33-
15+
def fixBadBibFormat(bibString):
16+
bibString = bibString.strip()
17+
assert bibString.startswith("@") and bibString.endswith("}"), \
18+
"bib entry should start with '@' and finish with '}', got :\n{bibString}"
19+
content = bib[1:-1]
20+
fields = content.split(",")
21+
for i, field in enumerate(fields[1:]):
22+
item = field.split("=")
23+
if len(item) == 2 and "{" not in item[1]:
24+
item[1] = "{"+item[1]+"}"
25+
fields[i] = "=".join(item)
26+
return "@"+",".join(fields)+"}"
3427

3528
try:
36-
29+
3730
parser = argparse.ArgumentParser()
3831
parser.add_argument("-b", "--body", help="input issue body here", type=str, default="")
3932
args = parser.parse_args()
@@ -170,7 +163,7 @@ def replace_month(match):
170163
bib = re.sub(r'(@[a-z]*{)(.*?),', r'\1' + id + ',', bib)
171164
url_bad = re.search(r'url\s*=\s*{(.*)}', bib).groups()[0]
172165
bib = re.sub(r'(url\s*=\s*{)(.*)}', r'\1' + urllib.parse.unquote(url_bad) + '}', bib)
173-
bib = normalize_month_fields(bib)
166+
bib = fixBadBibFormat(bib)
174167
bib_db = bibtexparser.loads(bib)
175168
print(bib)
176169
else:
@@ -198,4 +191,5 @@ def replace_month(match):
198191
print(line)
199192

200193
except Exception as e:
201-
print(f"ERROR : {e}\n")
194+
print(f"ERROR : {e}\n")
195+
print(traceback.format_exc(limit=1))

0 commit comments

Comments
 (0)