brokenco.de/generate-tags

37 lines
967 B
Python
Executable File

#!/usr/bin/env python3
import glob
import os
import re
post_dir = '_posts/'
tag_dir = 'tag/'
total_tags = []
for filename in glob.glob(post_dir + '*.md'):
matcher = r'^tags:$'
with open(filename, 'r') as fd:
tagged_line = False
for line in fd.readlines():
if tagged_line:
if line.startswith('---'):
tagged_line = False
else:
total_tags.append(line[1:].strip())
if re.match(matcher, line):
tagged_line = True
total_tags = set(total_tags)
for tag in glob.glob(tag_dir + '*.md'):
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'
with open(tag_filename, 'w+') as fd:
fd.write('---\nlayout: tag_page\ntitle: \"Tag: ' + tag + '\"\ntag: ' + tag + '\nrobots: noindex\n---\n')
print("Tags generated, count", total_tags.__len__())