-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_tags.py
198 lines (154 loc) · 5.42 KB
/
update_tags.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python
# python3 update_tags.py
# https://arturomoncadatorres.com/automatically-generating-tag-posts-for-github-pages-using-jekyll/
# https://github.com/arturomoncadatorres/arturomoncadatorres.github.io/blob/main/_layouts/post.html
"""
update_tags.py
Script to automatically create tags posts for a Jekyll blog hosted
in Github pages. First, it fishes all tags. Then, it creates the
tags posts. Lastly, it adds the generated files, commits them, and pushes
them to the corresponding repository. Preferably run this script
when having a clean working space (nothing to commit/push).
Based on Long Qian's post
https://longqian.me/2017/02/09/github-jekyll-tag/
@author: Arturo Moncada-Torres
"""
#%% Preliminaries.
import glob
import os
import git
#%% Define paths.
post_dir = '_posts/'
tag_dir = 'tag/'
#%%
def get_tags(post_dir=post_dir, verbose=True):
'''
Adapted from tag_generator.py
Copyright 2017 Long Qian
Contact: [email protected]
This script created tags for a Jekyll blog hosted by Github page.
No plugins required.
See https://longqian.me/2017/02/09/github-jekyll-tag/
Updated 2019-12-05
Arturo Moncada-Torres
Adapted script to process .md files with tags in format
tags:
- tag1
- tag2
...
Notice that for this to work properly, tags must be the last element of the
Markdown header.
Parameters
----------
post_dir: string
Path to directory _posts/
verbose: boolean
Indicate if status messages are printed (True) or not (False)
Returns
-------
total_tags: set
Set with all the tags used in the different posts.
'''
# Get Markdown posts files.
filenames = glob.glob(post_dir + '*md')
# Loop through all files.
total_tags = []
for filename in filenames:
f = open(filename, 'r', encoding='utf8')
crawl = False
tag_lines_coming = False
for line in f:
current_line = line.strip()
if crawl:
if current_line == 'tags:':
tag_lines_coming = True
continue
# If --- delimiter is found, start crawling.
if current_line == '---':
if not crawl:
crawl = True
else:
crawl = False
break
# If we are in the actual tag lines (that is, tag_lines_coming is
# True and we aren't in the tags: line), extract them.
if tag_lines_coming and (current_line != 'tags:'):
total_tags.append(current_line.strip('- '))
f.close()
# Make tags unique in a set.
total_tags = set(total_tags)
if verbose:
print("Found " + str(total_tags.__len__()) + " tags")
return total_tags
#%%
def create_tags_posts(tag_dir=tag_dir, total_tags=set(), verbose=True):
'''
Adapted from tag_generator.py
Copyright 2017 Long Qian
Contact: [email protected]
This script created tag posts for a Jekyll blog hosted by Github page.
No plugins required.
See https://longqian.me/2017/02/09/github-jekyll-tag/
Updated 2019-12-11
Arturo Moncada-Torres
Modularized for ease of use in update_tags.py.
Parameters
----------
post_dir: string
Path to directory directory where tag posts will be created.
total_tags: set
verbose: boolean
Indicate if status messages are printed (True) or not (False)
Returns
-------
None
'''
if total_tags.__len__() == 0:
print("No tags. Thus, no tag posts were created")
return None
else:
old_tags = glob.glob(tag_dir + '*.md')
for tag in old_tags:
os.remove(tag)
if not os.path.exists(tag_dir):
os.makedirs(tag_dir)
for tag in total_tags:
tag_filename = tag_dir + tag + '.md'
f = open(tag_filename, 'a')
write_str = '---\nlayout: tag_page\ntitle: \"Tag: ' + tag + '\"\ntag: ' + tag + '\nrobots: noindex\n---\n'
f.write(write_str)
f.close()
# for tag in total_tags:
# tag_filename = tag_dir + tag.lower() + '.md'
# f = open(tag_filename, 'a')
# write_str = '---\nlayout: tag_page\ntitle: \"Tag: ' + tag + '\"\ntag: ' + tag + '\nrobots: noindex\n---\n'
# f.write(write_str)
# f.close()
if verbose:
print("Created " + str(total_tags.__len__()) + " tag posts")
return None
#%%
if __name__ == '__main__':
tags = get_tags(post_dir)
create_tags_posts(tag_dir, tags)
# For Git.
# repo = git.Repo(os.getcwd())
# # Add files for commit.
# try:
# repo.git.add(tag_dir)
# except:
# print("Error ocurred while adding files to Git.")
# # Commit changes.
# try:
# repo.git.commit('-m', 'Updated tags and created corresponding posts', author='[email protected]')
# except:
# print("Error occurred while commiting.")
# # Push commit.
# try:
# origin = repo.remote(name='origin')
# origin.push()
# except:
# print("Error occurred while pushing.")