-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
weight_map.py
executable file
·149 lines (119 loc) · 3.77 KB
/
weight_map.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
import sys
import re
import os
import operator
import argparse
WEIGHT = re.compile(r'^weight\s*:\s*([0-9]+)\r?\n')
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
CONTENT_PATH = os.path.join(ROOT_PATH, 'content')
class Page:
def __init__(self, name):
self.name = name
self.weight = 0
def set_weight(self, weight):
self.weight = weight
class Section(Page):
def __init__(self, name, parent):
super().__init__(name)
self.parent = parent
self.pages = []
def ensure_section(self, name):
for page in self.pages:
if page.name == name:
assert isinstance(page, Section)
return page
section = Section(name, self)
self.pages.append(section)
return section
def add_page(self, name):
page = Page(name)
self.pages.append(page)
return page
def echo(self, indent):
self.pages.sort(key=operator.attrgetter('weight'))
for page in self.pages:
print(f'{" " * indent}{page.name}')
if isinstance(page, Section):
page.echo(indent+1)
def get_weight_map():
site = Section('', None)
weight = 1
for root, dirs, files in os.walk(CONTENT_PATH):
uri = root[len(CONTENT_PATH):]
uri = uri.strip('/').split('/')
section = site
for s in uri:
if s:
section = section.ensure_section(s)
files.sort()
for file in files:
if file.endswith(".md"):
if file == '_index.md':
page = section
else:
page = section.add_page(os.path.splitext(file)[0])
fn = os.path.join(root, file)
with open(fn, 'r') as f:
lines = f.readlines()
for i, line in enumerate(lines):
m = WEIGHT.match(line)
if m:
page.set_weight(int(m.group(1)))
break
site.echo(0)
def set_weights(fn):
with open(fn, 'r') as f:
lines = f.readlines()
indent = [0]
uri = ['']
ident = 0
weight = 1
for line in lines:
leading_spaces = len(line) - len(line.strip(' '))
if leading_spaces > indent[-1]:
indent.append(leading_spaces)
else:
while True:
if leading_spaces == indent[-1]:
break
indent.pop()
uri = uri[:len(indent)]
uri.append(line.strip())
fn = os.path.join(CONTENT_PATH, *uri)
chap_fn = os.path.join(fn, '_index.md')
page_fn = fn + '.md'
if os.path.isfile(chap_fn):
fn = chap_fn
elif os.path.isfile(page_fn):
fn = page_fn
else:
raise FileNotFoundError(
f'neither `{chap_fn}` or `{page_fn}` is found')
with open(fn, 'r') as f:
doc_lines = f.readlines()
for i, doc_line in enumerate(doc_lines):
m = WEIGHT.match(doc_line)
if m:
doc_lines[i] = f'weight: {weight}\n'
weight += 1
break
with open(fn, 'w') as f:
f.writelines(doc_lines)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--export',
action='store_true',
help='export current site structure by weights')
parser.add_argument(
'--apply',
help='set weights according a site structure file')
args = parser.parse_args()
if not args.export and not args.apply:
parser.print_help()
sys.exit(0)
if args.export:
get_weight_map()
if args.apply:
set_weights(args.apply)