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
|
#!/usr/bin/env python3
from os import path
from subprocess import run
from sys import argv, exit
def parse_arguments(args):
if len(args) != 3:
exit(f'Usage: {argv[0]} TOP-DIR OUTPUT-DIR')
return argv[1], argv[2]
def find_sources(top_dir):
p = run(
('find', top_dir, '-name', '*.md', '-o', '-name', '*.org'),
capture_output=True, check=True, text=True
)
return p.stdout.splitlines()
def html_path(source_path, top_dir, out_dir):
fname = path.basename(source_path)
dname = path.dirname(source_path)
name, _ = path.splitext(fname)
if name == 'README':
name = 'index'
return path.join(
out_dir, path.relpath(dname, top_dir), f'{name}.html'
)
def write_dependencies(output, sources, top_dir, out_dir):
pages = []
for src in sources:
html = html_path(src, top_dir, out_dir)
html_dir = path.dirname(html)
print(f'{html}: {src} | {html_dir}', file=output)
pages.append(html)
print(file=output)
print(f'pages = {" ".join(pages)}', file=output)
def main(argv):
top_dir, out_dir = parse_arguments(argv)
source_files = find_sources(top_dir)
with open('deps.mk', 'w') as deps:
write_dependencies(deps, source_files, top_dir, out_dir)
if __name__ == '__main__':
main(argv)
|