-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddcommitlink.py
More file actions
62 lines (36 loc) · 1.22 KB
/
Copy pathaddcommitlink.py
File metadata and controls
62 lines (36 loc) · 1.22 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
"""Automatically add link to specific commit after footer of pages.
That is, if current folder is a git project.
"""
from subprocess import check_output
from pathlib import Path
from shlex import split
GIT_COMMAND = 'git rev-parse HEAD'
GIT_COMMAND_ELEMENTS = split(GIT_COMMAND)
try:
_full_hash = (
check_output(GIT_COMMAND_ELEMENTS)
.decode('ascii')
.strip()
)
except Exception as err:
print("Didn't manage to retrieve hash of HEAD")
print()
print(err)
else:
### text to be replaced
search_text = '<!-- hash placeholder -->'
### text to replace
_hash_head = _full_hash[:7]
commit_html = (
' (commit: <a href="https://github.com/IndieSmiths/nodezator.com/commit/'
f'{_full_hash}">{_hash_head}</a>)'
)
### pages wherein to search and replace
_output_dir = Path(__file__).parent / '_output'
html_pages = _output_dir.glob('**/*.html')
### iterating over such pages performing the replacement
for page in html_pages:
text = page.read_text(encoding='utf-8')
if search_text in text:
new_text = text.replace(search_text, commit_html)
page.write_text(new_text, encoding='utf-8')