forked from livecomsjournal/livecomsjournal.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_navigation.py
More file actions
105 lines (90 loc) · 3.86 KB
/
generate_navigation.py
File metadata and controls
105 lines (90 loc) · 3.86 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
#!/usr/bin/env python3
import argparse
import re
import os
import string
# List of Markdown files that will
# - be referred to in the list of links on the top of the page
# - have a sidebar navigation item
markdown_files = [
{'title': 'Policies', 'file': '_policies/01_policies.md'},
{'title': 'Best Practices', 'file': '_policies/02_best_practices.md'},
{'title': 'Reviews', 'file': '_policies/03_perpetual_reviews.md'},
{'title': 'Simulation Comparisons', 'file': '_policies/04_compare_simulations.md'},
{'title': 'Tutorials', 'file': '_policies/05_tutorials.md'},
{'title': 'Lessons Learned', 'file': '_policies/06_lessons_learned.md'},
{'title': 'Paper Writing', 'file': '_policies/07_paper_code.md'},
{'title': 'Editorial Board', 'file': '_policies/08_editorial_board.md'} ]
def print_subtoc(headings, level, file_location):
for h in headings:
print(level*2*' ' + '- title: ' + h['title'])
print(level*2*' ' + ' url: ' + file_location + h['ref'])
if h['sub']:
print(level*2*' ' + ' children:')
print_subtoc(h['sub'], level + 1, file_location)
def get_folder_from_filename(filename):
return '/' + filename.replace('_policies/','').replace('.md', '')[3:] + '/'
def make_link_from_heading(heading):
translator = str.maketrans('', '', string.punctuation)
return heading.translate(translator).lower().replace(' ','-')
def create_toc(md_file):
file_location = get_folder_from_filename(md_file.name) + 'index.html'
find_heading = re.compile('^(#+) (.*)')
headings = []
# Process front matter
title = ''
line = md_file.readline()
if line == '---\n':
line = md_file.readline()
while line != '---\n':
line = [l.strip() for l in line.split(':', maxsplit=1)]
if line and line[0] == 'title' and len(line) == 2:
title = line[1]
line = md_file.readline()
else:
md_file.seek(0)
headings.append({'title': title,
'ref': '#',
'sub': []})
# Read remaining file
for line in md_file:
match = find_heading.match(line)
if match is None:
continue
heading_level = len(match.group(1))
heading = match.group(2).strip()
heading_ref = '#' + make_link_from_heading(heading)
if heading_level == 1:
headings.append({'title': heading,
'ref': heading_ref,
'sub': []})
else:
parent = headings
for n in range(heading_level - 1):
if len(parent) == 0:
parent.append({'title': '',
'ref': '#',
'sub': []})
parent = parent[-1]['sub']
parent.append({'title': heading,
'ref': heading_ref,
'sub': []})
print(os.path.basename(md_file.name)[3:] + ':')
print_subtoc(headings, 1, file_location)
# argparse might be overkill, but at least we have a little help file
parser = argparse.ArgumentParser(description=('Generate YAML table of content ' +
'from markdown files ' +
'(hardcoded at the top of this script)\n' +
'Output of this script should be redirected to ' +
'_data/navigation.yml'),
formatter_class=argparse.RawTextHelpFormatter)
args = parser.parse_args()
# print main navigation
print('main:')
for f in markdown_files:
print(' - title: ' + f['title'])
print(' url: ' + get_folder_from_filename(f['file']))
# print sidebar navigation for files
for f in markdown_files:
print()
create_toc(open(f['file']))