-
Notifications
You must be signed in to change notification settings - Fork 9
/
metadata-check.py
50 lines (39 loc) · 1.09 KB
/
metadata-check.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
# metadata-check.py
# read .md file and make sure there is correct metadata
import pathlib
import markdown
import csv
import os
# check_metadata(file: str) -> None
def check_metadata(file: str) -> None:
metadata_fields = {
"blog_title",
"thumbnail",
"date",
"author",
"tags",
"category",
"language",
}
# read the markdown file
try:
data = pathlib.Path(file).read_text(encoding="utf-8")
md = markdown.Markdown(extensions=["meta"])
md.convert(data)
except:
return 1
# error flag, 0 = no error, 1 = error
# you want it to print out all the errors, so you shouldnt exit on the first one
missing = []
error = 0
for field in metadata_fields:
if field not in md.Meta:
missing.append(field)
error = 1
missing_text = " ".join(missing)
print(f"{file} is missing a metadata field: {missing_text} with error {error}, please take a look at guide-to-blogs-metadata.md")
exit(error)
def main():
file = input()
check_metadata(file)
main()