-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbibtex_clip.py
More file actions
150 lines (130 loc) · 4.88 KB
/
bibtex_clip.py
File metadata and controls
150 lines (130 loc) · 4.88 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"Convert the BibTex entry in the clipboard to a reference YAML file."
from pathlib import Path
import string
import sys
import unicodedata
import bibtexparser
import pyperclip
import yaml
import latex_utf8
LIBRARY_SOURCE_DIR = Path("/home/pekrau/Dropbox/pekrau.github.io/source/others")
LIBRARY_TARGET_DIR = Path("/home/pekrau/Dropbox/pekrau.github.io/docs/library/references")
MONTHS = {
"jan": 1,
"january": 1,
"feb": 2,
"february": 2,
"mar": 3,
"march": 3,
"apr": 4,
"april": 4,
"may": 5,
"jun": 6,
"june": 6,
"jul": 7,
"july": 7,
"aug": 8,
"august": 8,
"sep": 9,
"september": 9,
"oct": 10,
"october": 10,
"nov": 11,
"november": 11,
"dec": 12,
"december": 12,
}
def cleanup_latex(value):
"Convert LaTeX characters to UTF-8, remove newlines and normalize blanks."
return latex_utf8.from_latex_to_utf8(" ".join(value.split()))
def cleanup_whitespaces(value):
"Replace all whitespaces with blanks."
return " ".join(value.split())
def normalize(reference):
"""Normalize the reference for part of a URL.
- Normalize non-ASCII characters.
- Convert the string to ASCII.
- Lowercase.
- Replace blank with dash.
"""
return unicodedata.normalize("NFKD", reference).\
replace("æ", "a").\
replace("Æ", "A").\
encode("ASCII", "ignore").\
decode("utf-8").\
lower().\
replace(" ", "-")
for entry in bibtexparser.loads(pyperclip.paste()).entries:
data = dict(authors=cleanup_latex(entry.get("author", "")).split(" and "),
type=entry.get("ENTRYTYPE") or constants.ARTICLE,
year=int(entry["year"]))
if editors := cleanup_latex(entry.get("editor", "")):
data["editors"] = editors.split(" and ")
for key, value in entry.items():
if key in ("author", "editor", "ID", "ENTRYTYPE", "year"):
continue
data[key] = cleanup_latex(value).strip()
# Do some post-processing.
# Change month into date; sometimes has day number.
month = cleanup_latex(data.pop("month", ""))
parts = month.split("~")
if len(parts) == 2 and parts[1]:
month = MONTHS[parts[1].strip().casefold()]
day = int("".join([c for c in parts[0] if c in string.digits]))
data["published"] = f'{entry["year"]}-{month:02d}-{day:02d}'
elif len(parts) == 1 and parts[0]:
month = MONTHS[parts[0].strip().casefold()]
data["published"] = f'{entry["year"]}-{month:02d}-00'
# Split up keywords
if keywords := data.pop("keywords", None):
data["keywords"] = [cleanup_latex(k).strip() for k in keywords.split(";")]
# Issue instead of number.
try:
data["issue"] = data.pop("number")
except KeyError:
pass
# Change page numbers double dash to single dash.
if pages := data.pop("pages", None):
data["pages"] = pages.replace("--", "-")
if abstract:= data.pop("abstract", None):
data["abstract"] = cleanup_latex(cleanup_whitespaces(abstract))
reference = f'{data["authors"][0].split(",")[0]} {data["year"]}'
stem = normalize(reference)
# Is there a set of references for the same year?
if (filepath := LIBRARY_SOURCE_DIR / f"{stem}a.yaml").exists():
for suffix in string.ascii_lowercase:
if not (filepath := LIBRARY_SOURCE_DIR / f"{stem}{suffix}.yaml").exists():
reference = reference + suffix
break
# Is there a previous reference? If so, rename by adding suffix 'a' to it.
elif (filepath := LIBRARY_SOURCE_DIR / f"{stem}.yaml").exists():
print(" ", filepath, "already exists")
if input("overwrite it? > ") in "yYjJ":
pass
elif input("rename it? > ") in "yYjJ":
changed_reference = reference + "a"
changed_stem = stem + "a"
for root in [LIBRARY_SOURCE_DIR, LIBRARY_TARGET_DIR]:
source = root / f"{stem}.yaml"
try:
with open(source) as infile:
changed_data = yaml.safe_load(infile)
changed_data["reference"] = changed_reference
target = root / f"{changed_stem}.yaml"
target.write_text(yaml.dump(changed_data, allow_unicode=True))
source.unlink()
print(f"renamed {source} to {target}")
except FileNotFoundError:
pass
reference = reference + "b"
else:
print("no action; reference not saved")
sys.exit()
data["reference"] = reference
stem = normalize(reference)
filepath = LIBRARY_SOURCE_DIR / f"{stem}.yaml"
filepath.write_text(yaml.dump(data, allow_unicode=True))
print("wrote", filepath)
filepath = LIBRARY_TARGET_DIR / f"{stem}.yaml"
filepath.write_text(yaml.dump(data, allow_unicode=True))
print("wrote", filepath)