forked from lawrennd/old_talks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_yaml_refs.py
executable file
·35 lines (29 loc) · 1.32 KB
/
clean_yaml_refs.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
#!/usr/bin/env python
# Format taken from: http://blog.martinfenner.org/2013/07/30/citeproc-yaml-for-bibliographies/
import frontmatter
import glob
replacements = {'key' : 'id', 'editors' : 'editor', 'authors' : 'author', 'number' : 'issue', 'booktitle' : 'container-title', 'doi' : 'DOI', 'url': 'URL'}
name_replacements = {'firstname' : 'given', 'lastname' : 'family'}
name_fields = ['author', 'editor']
section = False
section_name = 'neil'
for filename in glob.glob('./_posts/*.md'):
post = frontmatter.load(filename)
if section:
if 'section' not in post.keys():
post.metadata['section'] = section_name
for key,val in replacements.items():
if val not in post.keys() and key in post.keys():
post.metadata[val] = post.metadata[key]
del post.metadata[key]
for name_field in name_fields:
if name_field in post.keys():
for author in post.metadata[name_field]:
for key,val in name_replacements.items():
if val not in post.keys() and key in post.keys():
post.metadata[val] = post.metadata[key]
del post.metadata[key]
print(frontmatter.dumps(post))
stream = file(filename, 'w')
frontmatter.dump(post, stream, encoding='utf-8')
f.close()