generated from PhasesResearchLab/projects-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
yaml2md.py
220 lines (179 loc) · 8.77 KB
/
yaml2md.py
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import yaml
import re
from habanero import Crossref, cn
def parse_pressprints(header, file):
parsed_entries = f"# {header}\n---\n"
# Read the entries from the YAML file
with open('publications/' + file, 'r', encoding='utf-8') as f:
entries = yaml.safe_load(f)
if entries is not None:
No = len(entries)
for entry in entries:
entryString = f"**{No}\.** {entry['authors']}, _{entry['title']}_, {entry['metadata']}\n\n"
URLs = []
try:
url = entry['DOI']
URLs.append(f"DOI: [{re.search(r'https?://([^/]+/)?(.+)', url).group(2)}]({url})")
except Exception as e:
if 'DOI' in entry and entry['DOI'] != None:
URLs.append(f"DOI: [{url}]({url})")
try:
url = entry['arXiv']
URLs.append(f"arXiv: [{re.search(r'https?://([^/]+/)?(.+)', url).group(2)}]({url})")
except Exception as e:
if 'arXiv' in entry and entry['arXiv'] != None:
URLs.append(f"arXiv: [{url}]({url})")
try:
url = entry['URL']
URLs.append(f"URL: [{re.search(r'https?://([^/]+/)?(.+)', url).group(2)}]({url})")
except Exception as e:
if 'URL' in entry and entry['URL'] != None:
URLs.append(f"URL: [{url}]({url})")
entryString += " \| ".join(URLs) + "\n\n"
parsed_entries += entryString
No -= 1
return parsed_entries
def parse_articles(header, file):
# Read the entries from the YAML file
with open('publications/' + file, 'r', encoding='utf-8') as f:
entries = yaml.safe_load(f)
# Count how many entries contain 'bumpyear' as a key in 'entries'
bumpyears = sum(1 for item in entries if 'bumpyear' in item) + 1
# Split 'entries' into lists at the position where a 'bumpyear' key appears
split_entries = []
temp_list = []
for item in entries:
if 'bumpyear' in item:
if temp_list:
split_entries.append(temp_list)
temp_list = [item]
else:
temp_list.append(item)
if temp_list:
split_entries.append(temp_list)
# Create a dictionary with 'bumpyears' items
bumpyear_dict = {1987 + bumpyears - i: split_entries[i] for i in range(bumpyears)}
# Remove entries containing 'bumpyear' as a key from the sublists in 'bumpyear_dict'
for year, sublist in bumpyear_dict.items():
bumpyear_dict[year] = [item for item in sublist if 'bumpyear' not in item]
parsed_entries = f"# {header}\n---\n## {max(bumpyear_dict)//10*10}'s\n\n"
# Create a string to store the formatted entries
id = len(entries) - bumpyears + 1
for key, value in bumpyear_dict.items():
if key%10 == 9 and key != max(bumpyear_dict):
parsed_entries += f"## {key//10*10}'s\n\n"
parsed_entries += f"### {key} ({id} - {id-len(value)+1})\n\n"
for i, entry in enumerate(value):
if "ID_deprecated" in entry and entry["ID_deprecated"] != None:
id = int(entry["ID_deprecated"])
entryString = f"<div id='{id}'></div> **{id}.** {entry['authors']}, _{entry['title']}_, {entry['metadata']}\n\n"
URLs = []
try:
url = entry['DOI']
URLs.append(f"DOI: [{re.search(r'https?://([^/]+/)?(.+)', url).group(2)}]({url})")
except Exception as e:
if 'DOI' in entry and entry['DOI'] != None:
URLs.append(f"DOI: [{url}]({url})")
try:
url = entry['arXiv']
URLs.append(f"arXiv: [{re.search(r'https?://([^/]+/)?(.+)', url).group(2)}]({url})")
except Exception as e:
if 'arXiv' in entry and entry['arXiv'] != None:
URLs.append(f"arXiv: [{url}]({url})")
try:
url = entry['URL']
URLs.append(f"URL: [{re.search(r'https?://([^/]+/)?(.+)', url).group(2)}]({url})")
except Exception as e:
if 'URL' in entry and entry['URL'] != None:
URLs.append(f"URL: [{url}]({url})")
try:
bibentry = cn.content_negotiation(ids = entry['DOI'], format = "bibentry")
entryString += f"<button onclick='copyToClipboard(\"bib{id}\")'><i class='fas fa-copy'></i></button>\n" + " \| ".join(URLs) + f"\n<p id='bib{id}' style='display: none;'>{bibentry}</p>\n\n"
except Exception as e:
entryString += " \| ".join(URLs) + "\n\n"
print(f"An error occurred while processing the BIBENTRY DOI {entry['DOI']}: {e}")
try:
result = cr.works(ids = entry['DOI'])
if 'abstract' in result['message']:
abstract = result['message']['abstract']
# Remove opening <jats:p *> tags
abstract = re.sub(r'<jats:p[^>]*>', '', abstract)
# Remove closing </jats:p> tags
abstract = re.sub(r'</jats:p>', '', abstract)
# Remove <jats:title>Abstract</jats:title> tags
abstract = re.sub(r'<jats:title>Abstract</jats:title>', '', abstract)
entryString += f"\n<details style='margin-bottom: 20px;'>\n <summary>Abstract:</summary>\n \n {abstract}\n</details>"
except Exception as e:
print(f"An error occurred while processing the ABSTRACT of DOI {entry['DOI']}: {e}")
parsed_entries += entryString + "\n\n"
id -= 1
return parsed_entries
def parse_theses(header, file):
parsed_entries = f"# {header}\n---\n"
# Read the entries from the YAML file
with open('publications/' + file, 'r', encoding='utf-8') as f:
entries = yaml.safe_load(f)
for type in ['PhD Thesis','MS Thesis','BS Thesis']:
parsed_entries += f"## {type.upper()}\n\n"
No = len([entry for entry in entries if entry['type'] == type])
for entry in entries:
if entry['type'] == type:
entryString = f"**{No}\.** {entry['metadata']}\n\n"
URLs = []
try:
url = entry['URL']
URLs.append(f"URL: [{re.search(r'https?://([^/]+/)?(.+)', url).group(2)}]({url})")
except Exception as e:
if 'URL' in entry and entry['URL'] != None:
URLs.append(f"URL: [{url}]({url})")
try:
url = entry['arXiv']
URLs.append(f"arXiv: [{re.search(r'https?://([^/]+/)?(.+)', url).group(2)}]({url})")
except Exception as e:
if 'arXiv' in entry and entry['arXiv'] != None:
URLs.append(f"arXiv: [{url}]({url})")
try:
url = entry['Recording']
URLs.append(f"Recording: [{re.search(r'https?://([^/]+/)?(.+)', url).group(2)}]({url})")
except Exception as e:
if 'Recording' in entry and entry['Recording'] != None:
URLs.append(f"Recording: [{url}]({url})")
entryString += " \| ".join(URLs) + "\n\n"
parsed_entries += entryString
No -= 1
return parsed_entries
def parse_others(header, file):
parsed_entries = f"# {header}\n---\n"
# Read the entries from the YAML file
with open('publications/' + file, 'r', encoding='utf-8') as f:
entries = yaml.safe_load(f)
if entries is not None:
No = len(entries)
for entry in entries:
if entry['URL']:
parsed_entries += f"**{No}\.** [{entry['metadata']}]({entry['URL']})\n\n"
else:
parsed_entries += f"**{No}\.** {entry['metadata']}\n\n"
No -= 1
return parsed_entries
cr = Crossref()
formatted_entries = '''---
layout: single
toc: true
toc_label: 'Publications List'
sidebar:
nav: 'sidebar'
---
'''
formatted_entries += parse_pressprints('BOOKS', 'books.yaml')
formatted_entries += parse_pressprints('PRE-PRINTS', 'preprints.yaml')
formatted_entries += parse_pressprints('IN-PRESS', 'inpress.yaml')
formatted_entries += parse_articles('ARTICLES', 'articles.yaml')
formatted_entries += parse_others('CONFERENCE PROCEEDINGS AND REPORTS','proceedingsandreports.yaml')
formatted_entries += parse_others('PATENTS', 'patents.yaml')
formatted_entries += parse_others('BOOK CHAPTERS', 'bookchapters.yaml')
formatted_entries += parse_others('WEBCASTS', 'webcasts.yaml')
formatted_entries += parse_theses('THESES', 'theses.yaml')
# Write the formatted entries to a .md file
with open('index.md', 'w', encoding='utf-8') as f:
f.write(formatted_entries)