-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalizer.py
More file actions
191 lines (152 loc) · 5.42 KB
/
normalizer.py
File metadata and controls
191 lines (152 loc) · 5.42 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
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
import os
import sys
import re
import random
import yaml
import ruamel.yaml
from collections import OrderedDict
from glob import glob
from datetime import datetime, timedelta
front_matter_key_removals = [
"excerpt",
"custom_scripts",
"published",
]
front_matter_data_defaults = {
"title": "",
"author": "xransum",
"date": "",
"categories": [],
"tags": [],
"image": {
"path": None,
"lqip": None,
"alt": None,
},
}
key_order = [
"title",
"author",
"date",
"categories",
"tags",
"pin",
"math",
"mermaid",
"image",
]
root = os.path.abspath(".")
images_dir = os.path.join(root, "commons")
posts_dir = os.path.join(root, "_posts")
images = glob(os.path.join(images_dir, "*"))
posts = glob(os.path.join(posts_dir, "*.md"))
def read_file(filepath):
contents = None
with open(filepath, "r", encoding="utf-8") as f:
contents = f.read()
return contents
def parse_post_basename(basename):
year, month, day, post_name = basename.split("-", 3)
date_str = "-".join([year, month, day])
date = datetime.strptime(date_str, "%Y-%m-%d")
return date, post_name
def generate_timestamp_for_day(day):
rng_time = day + timedelta(
hours=random.randint(0, 23),
minutes=random.randint(0, 59),
seconds=random.randint(0, 59),
)
return rng_time.strftime("%Y-%m-%d %H:%M:%S -0500")
def get_random_image(excludes=[]):
img = random.choice([img for img in images if img not in excludes])
return img
class FixedDumper(yaml.Dumper):
def increase_indent(self, flow=False, indentless=False):
return super(FixedDumper, self).increase_indent(flow, False)
def update_front_matter(day, post_content):
front_matter_match = re.match(r"---\n(.*?\n)---", post_content, re.DOTALL)
front_matter_content = front_matter_match.group(1)
# Load front matter as yaml
front_matter_data = yaml.safe_load(front_matter_content)
# Remove unwanted keys
for key_removal in front_matter_key_removals:
front_matter_data.pop(key_removal, None)
# Add missing keys with default values
for key, value in front_matter_data_defaults.items():
if key not in front_matter_data or not front_matter_data[key]:
if isinstance(value, dict):
front_matter_data[key] = value.copy()
else:
front_matter_data[key] = value
if r"```mermaid" in post_content:
front_matter_data["mermaid"] = True
if r"$$" in post_content:
front_matter_data["math"] = True
# If 'date' is missing, generate a random timestamp for the day
# if 'date' not in front_matter_data:
# front_matter_data['date'] = generate_timestamp_for_day(day)
front_matter_data["date"] = generate_timestamp_for_day(day)
# Replace tags to be lowercased and '-' delimited.
if "tags" in front_matter_data and isinstance(
front_matter_data["tags"], list
):
front_matter_data["tags"] = [
"-".join(re.findall(r"\w+", tag.lower()))
for tag in front_matter_data["tags"]
]
# If 'image' is missing, get a random image
if "image" not in front_matter_data:
front_matter_data["image"] = front_matter_data_defaults["image"].copy()
if "path" not in front_matter_data["image"] or any(
[front_matter_data["image"]["path"] == value for value in [None, ""]]
):
image = random.choice(images)
front_matter_data["image"]["path"] = os.path.join(
"/", os.path.relpath(image, root)
)
# Create an ordered dictionary with the desired key order
ordered_front_matter = {
key: front_matter_data.get(key, "")
for key in key_order
if key in front_matter_data
}
for key in ["categories", "tags"]:
# Customize the style for 'categories' and 'tags' keys
# updated_value_str = yaml.dump(
# front_matter_data[key],
# default_flow_style=None, # Use default style for lists
# ).strip()
# updated_value_str = '[' + ', '.join(front_matter_data[key]) + ']'
# Remove square brackets
# updated_value_str = updated_value_str[1:-1]
# Remove single-quotes wrapping the new list format
# updated_value_str = updated_value_str.replace("'", "")
# ordered_front_matter[key] = updated_value_str
pass
# Print the ordered YAML
yaml_parser = ruamel.yaml.YAML()
yaml_parser.indent(sequence=4, offset=2)
updated_front_matter_str = yaml.dump(
ordered_front_matter,
default_flow_style=False,
sort_keys=False,
Dumper=FixedDumper,
)
# updated_front_matter_str = yaml_parser.dump(ordered_front_matter, sys.stdout)
return front_matter_content, updated_front_matter_str
for file_path in posts:
basename = os.path.basename(file_path)
day, post_name = parse_post_basename(basename)
post_content = read_file(file_path)
front_matter_data, updated_front_matter = update_front_matter(
day, post_content
)
updated_content = post_content.replace(
front_matter_data, updated_front_matter
)
if r"```mermaid" in updated_content:
updated_content = re.sub(r"```mermaid\!", r"```mermaid", updated_content)
with open(file_path, "w", encoding="utf-8") as file:
stdout = file.write(updated_content)
if stdout:
print(f"{file_path} sucessfully overwritten!")