1
1
#! /usr/bin/python
2
2
# -*- coding: utf-8 -*-
3
3
import os , sys , glob , re , validators , argparse
4
- from frontmatter import Frontmatter
4
+ import frontmatter
5
5
6
6
# Parse arguments
7
7
parser = argparse .ArgumentParser ()
24
24
link_err_str = "\n {err}\n File: {file}\n Line: {line}\n Link: {link}\n "
25
25
id_err_str = "\n Error: {err}\n id: {id}\n Files:\n {f1}\n {f2}\n "
26
26
27
+ def render_fmatter (fm ):
28
+ s = ''
29
+ for k in fm .keys ():
30
+ s += '\n ' + f"{ k } : { fm [k ]} "
31
+ return s + '\n '
32
+
27
33
def check_frontmatter (fmatter ):
28
34
num_errs = 0
29
35
has_required_attrs = True
30
- fm = fmatter [ 'frontmatter' ] .replace ('\n ' ,'\n ' )
36
+ fm = render_fmatter ( fmatter ) .replace ('\n ' ,'\n ' )
31
37
for x in required_attrs :
32
- if x not in fmatter [ 'attributes' ] : # Check for required attributes
38
+ if x not in fmatter . keys () : # Check for required attributes
33
39
print (attr_err_str .format (file = fname , attr = x , err = "missing" , fm = fm ))
34
40
has_required_attrs = False
35
41
num_errs += 1
36
42
37
43
# Skip the rest of checks if missing required attributes
38
44
if has_required_attrs :
39
- if not re .match ('^[-\w]+$' , fmatter ['attributes' ][ "id" ]): # Check for invalid id string
45
+ if not re .match ('^[-\w]+$' , fmatter ["id" ]): # Check for invalid id string
40
46
print (attr_err_str .format (file = fname , attr = "id" , err = "invalid\n id must contain only alphanumeric, hyphen(-), or underscore(_)" , fm = fm ))
41
47
num_errs += 1
42
48
43
- if fmatter ['attributes' ][ "title" ] is None : # Check for empty attribute
49
+ if fmatter ["title" ] is None : # Check for empty attribute
44
50
print (attr_err_str .format (file = fname , attr = "title" , err = "invalid" , fm = fm ))
45
51
num_errs += 1
46
52
47
- if not isinstance (fmatter ['attributes' ][ "is_entry_point" ] , bool ) : # Check for non-boolean attribute
53
+ if not isinstance (fmatter ["is_entry_point" ] , bool ) : # Check for non-boolean attribute
48
54
print (attr_err_str .format (file = fname , attr = "is_entry_point" , err = "invalid" , fm = fm ))
49
55
num_errs += 1
50
56
@@ -61,11 +67,11 @@ def check_frontmatter(fmatter):
61
67
continue
62
68
63
69
# Parse file's front matter
64
- fmatter = Frontmatter . read_file (fname )
70
+ fmatter = frontmatter . load (fname )
65
71
err = check_frontmatter (fmatter )
66
72
errs += err
67
73
if err == 0 :
68
- id = fmatter ['attributes' ][ "id" ]
74
+ id = fmatter ['id' ]
69
75
if id in dict : # Check if id already exists
70
76
print (id_err_str .format (err = "id already exists" , id = id , f1 = dict [id ]["filename" ], f2 = fname ))
71
77
errs += 1
0 commit comments