40
40
from .config import PostConfig
41
41
from .markdown import ExcerptTreeprocessor
42
42
43
+
43
44
# -----------------------------------------------------------------------------
44
45
# Classes
45
46
# -----------------------------------------------------------------------------
@@ -52,40 +53,45 @@ class Post(Page):
52
53
def __init__ (self , file : File , config : MkDocsConfig ):
53
54
super ().__init__ (None , file , config )
54
55
55
- # Resolve path relative to docs directory
56
- docs = os .path .relpath (config .docs_dir )
57
- path = os .path .relpath (file .abs_src_path , docs )
56
+ if file .generated_by is None :
57
+ # Resolve path relative to docs directory
58
+ docs = os .path .relpath (config .docs_dir )
59
+ path = os .path .relpath (file .abs_src_path , docs )
60
+ file_origin = f" in '{ docs } '"
61
+ else :
62
+ # Path for generated posts
63
+ path = file .src_path
64
+ file_origin = f" generated by '{ file .generated_by } '"
58
65
59
66
# Read contents and metadata immediately
60
- with open (file .abs_src_path , encoding = "utf-8-sig" ) as f :
61
- self .markdown = f .read ()
62
-
63
- # Sadly, MkDocs swallows any exceptions that occur during parsing.
64
- # Since we want to provide the best possible user experience, we
65
- # need to catch errors early and display them nicely. We decided to
66
- # drop support for MkDocs' MultiMarkdown syntax, because it is not
67
- # correctly implemented anyway. When using MultiMarkdown syntax, all
68
- # date formats are returned as strings and list are not properly
69
- # supported. Thus, we just use the relevants parts of `get_data`.
70
- match : Match = YAML_RE .match (self .markdown )
71
- if not match :
72
- raise PluginError (
73
- f"Error reading metadata of post '{ path } ' in '{ docs } ':\n "
74
- f"Expected metadata to be defined but found nothing"
75
- )
76
-
77
- # Extract metadata and parse as YAML
78
- try :
79
- self .meta = yaml .load (match .group (1 ), SafeLoader ) or {}
80
- self .markdown = self .markdown [match .end ():].lstrip ("\n " )
81
-
82
- # The post's metadata could not be parsed because of a syntax error,
83
- # which we display to the author with a nice error message
84
- except Exception as e :
85
- raise PluginError (
86
- f"Error reading metadata of post '{ path } ' in '{ docs } ':\n "
87
- f"{ e } "
88
- )
67
+ self .markdown = file .content_string
68
+
69
+ # Sadly, MkDocs swallows any exceptions that occur during parsing.
70
+ # Since we want to provide the best possible user experience, we
71
+ # need to catch errors early and display them nicely. We decided to
72
+ # drop support for MkDocs' MultiMarkdown syntax, because it is not
73
+ # correctly implemented anyway. When using MultiMarkdown syntax, all
74
+ # date formats are returned as strings and list are not properly
75
+ # supported. Thus, we just use the relevants parts of `get_data`.
76
+ match : Match = YAML_RE .match (self .markdown )
77
+ if not match :
78
+ raise PluginError (
79
+ f"Error reading metadata of post '{ path } ' { file_origin } :\n "
80
+ f"Expected metadata to be defined but found nothing"
81
+ )
82
+
83
+ # Extract metadata and parse as YAML
84
+ try :
85
+ self .meta = yaml .load (match .group (1 ), SafeLoader ) or {}
86
+ self .markdown = self .markdown [match .end ():].lstrip ("\n " )
87
+
88
+ # The post's metadata could not be parsed because of a syntax error,
89
+ # which we display to the author with a nice error message
90
+ except Exception as e :
91
+ raise PluginError (
92
+ f"Error reading metadata of post '{ path } ' { file_origin } :\n "
93
+ f"{ e } "
94
+ )
89
95
90
96
# Initialize post configuration, but remove all keys that this plugin
91
97
# doesn't care about, or they will be reported as invalid configuration
@@ -103,7 +109,7 @@ def __init__(self, file: File, config: MkDocsConfig):
103
109
log .warning (w )
104
110
for k , e in errors :
105
111
raise PluginError (
106
- f"Error reading metadata '{ k } ' of post '{ path } ' in ' { docs } ' :\n "
112
+ f"Error reading metadata '{ k } ' of post '{ path } ' { file_origin } :\n "
107
113
f"{ e } "
108
114
)
109
115
0 commit comments