This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathseo_from_example.py
76 lines (59 loc) · 2.24 KB
/
seo_from_example.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
from os import path, walk
import re
NODES_DIR = path.join("docs", "nodes")
__ignore_dirs = ["appendix", "a1-[autogen]"]
def line_prepender(filename, content):
with open(filename, "r+") as f:
lines = f.readlines()
f.seek(0)
f.write(content)
f.write("\n")
for line in lines: # write old content after new
f.write(line)
def up_dir_name(file_path):
return path.dirname(path.dirname(path.dirname(file_path)))
def search_reg(file_path):
with open(file_path) as myfile:
content = myfile.read()
return re.search(r"---\n.*?---", content, re.DOTALL)
def del_placeholder(file_path):
with open(file_path, "r+") as f:
d = f.readlines()
f.seek(0)
for i in d:
if i != "<!--Add SEO here-->\n":
f.write(i)
f.truncate()
def copy_seo():
paths = []
for root, dirnames, files in walk(NODES_DIR):
# only care about directories for actual nodes
if any((d not in __ignore_dirs) for d in dirnames):
continue
if any((dir in root) for dir in __ignore_dirs):
continue
file_proccessed = []
for file in files:
if file.endswith("example.md"):
if file in file_proccessed:
raise FileExistsError(
f"Error: multiple md file found in {root} for {file}, there should only be one!"
)
file_proccessed.append(file)
path_index = root.index("nodes")
path_from_second_dir = root[path_index:]
file_path = path.join(path_from_second_dir, file)
file_path = path.join("docs", file_path)
paths.append(file_path)
for i in paths:
md_path = up_dir_name(i)
md_name = f"{path.join(md_path, path.basename(md_path))}.md"
if path.exists(md_name) and path.exists(i):
md_exists = search_reg(md_name)
ex_exists = search_reg(i)
if (ex_exists is not None) and (md_exists is None):
print(path.basename(md_path))
del_placeholder(md_name)
line_prepender(md_name, ex_exists.group())
if __name__ == "__main__":
copy_seo()