forked from wagov/wasocshared
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
48 lines (43 loc) · 1.57 KB
/
main.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
import os
from pathlib import Path
from itertools import groupby
from dateutil.parser import parse
def define_env(env):
"""
This is the hook for defining variables, macros and filters
- variables: the dictionary that contains the environment variables
- macro: a decorator function, to declare a macro.
"""
def get_title(path):
line = ""
with open(path) as f:
while not line.startswith("#"):
line = f.readline()
return line[2:].strip()
@env.macro
def date_index(glob, prefix="", expand=3, include=None):
"""
Insert an index to a glob pattern relative to top dir of documentation project.
"""
files = Path(env.project_dir).glob(glob)
mdtext = []
# Reverse order, sorted by first 6 characters (year + month)
months = groupby(reversed(sorted(files)), key=lambda f: f.name[0:6])
month_count = 0
for month, paths in months:
month_text = parse(month + "01").strftime("%Y %B")
if month_count < expand:
indent = ""
mdtext.append(f"#### {month_text}")
else:
indent = " "
mdtext.append(f'??? note "{month_text}"')
mdtext.append("")
for path in paths:
title = get_title(path)
mdtext.append(f"{indent}- [{title}]({prefix}{path.name})")
month_count += 1
if include is not None:
if month_count > include:
break
return "\n".join(mdtext)