-
-
Notifications
You must be signed in to change notification settings - Fork 254
/
gen_readme.py
executable file
·73 lines (55 loc) · 1.94 KB
/
gen_readme.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
#!/usr/bin/env python3
# usage:
# ./gen_readme.py > README.md
#
# requires pip install pyyaml
import yaml
def load_papers():
def markdown(paper, bold=False):
entry = f'{paper["title"]}. [{paper["author"]} ({paper["year"]})]({paper["link"]}).'
if bold:
entry = f'**{entry}**'
entry += '\n'
# if the title ends with ? don't append a period
return entry.replace('?.', '?')
with open('papers.yml') as file_:
papers = yaml.safe_load(file_)
for paper in papers:
paper['markdown'] = markdown(paper, bold=True)
for related in paper.setdefault('related', []):
related['markdown'] = markdown(related, bold=False)
paper['related'].sort(key=lambda p: p['year'])
return papers
def render_readme(papers):
with open('README.md.template') as template:
readme = template.read()
# Full nested list
papers_full = ''
for paper in papers:
papers_full += '1. ' + paper['markdown']
papers_full += ' \\\n'
topics = '; '.join(paper['topics'])
papers_full += f" <sub>{topics}</sub>\n"
for related in paper['related']:
papers_full += ' * ' + related['markdown']
papers_full += '\n'
readme = readme.replace('{{ PAPERS_FULL }}', papers_full)
# top-level only
papers_top = ''
for paper in papers:
papers_top += '1. ' + paper['markdown']
readme = readme.replace('{{ PAPERS_TOC }}', papers_top)
# chronological flat list
all_papers = []
for paper in papers:
all_papers.append(paper)
all_papers += paper['related']
papers_sorted = ''
for paper in sorted(all_papers, key=lambda p: p['year']):
papers_sorted += '1. ' + paper['markdown']
readme = readme.replace('{{ PAPERS_SORTED }}', papers_sorted)
return readme
if __name__ == '__main__':
papers = load_papers()
output = render_readme(papers)
print(output)