17
17
from psc .here import HERE
18
18
from psc .here import PYODIDE
19
19
20
+
20
21
EXCLUSIONS = ("pyscript.css" , "pyscript.js" , "favicon.png" )
21
22
22
23
23
24
def tag_filter (
24
- tag : Tag ,
25
- exclusions : tuple [str , ...] = EXCLUSIONS ,
25
+ tag : Tag ,
26
+ exclusions : tuple [str , ...] = EXCLUSIONS ,
26
27
) -> bool :
27
28
"""Filter nodes from example that should not get included."""
28
29
attr = "href" if tag .name == "link" else "src"
@@ -74,7 +75,7 @@ def get_body_content(s: BeautifulSoup, test_path: Path = PYODIDE) -> str:
74
75
class Resource :
75
76
"""Base dataclass used for all resources."""
76
77
77
- path : PurePath
78
+ name : str
78
79
title : str = ""
79
80
body : str = ""
80
81
extra_head : str = ""
@@ -91,21 +92,24 @@ class Example(Resource):
91
92
"""
92
93
93
94
subtitle : str = ""
95
+ description : str = ""
96
+ author : str | None = None
94
97
95
98
def __post_init__ (self ) -> None :
96
99
"""Extract most of the data from the HTML file."""
97
100
# Title, subtitle, body come from the example's MD file.
98
- index_md_file = HERE / "gallery/examples" / self .path / "index.md"
101
+ index_md_file = HERE / "gallery/examples" / self .name / "index.md"
99
102
md_fm = frontmatter .load (index_md_file )
100
103
self .title = md_fm .get ("title" , "" )
104
+ self .author = md_fm .get ("author" , "" )
101
105
self .subtitle = md_fm .get ("subtitle" , "" )
102
106
md = MarkdownIt ()
103
- self .body = str (md .render (md_fm .content ))
107
+ self .description = str (md .render (md_fm .content ))
104
108
105
109
# Main, extra head example's HTML file.
106
- index_html_file = HERE / "gallery/examples" / self .path / "index.html"
110
+ index_html_file = HERE / "gallery/examples" / self .name / "index.html"
107
111
if not index_html_file .exists (): # pragma: nocover
108
- raise ValueError (f"No example at { self .path } " )
112
+ raise ValueError (f"No example at { self .name } " )
109
113
soup = BeautifulSoup (index_html_file .read_text (), "html5lib" )
110
114
self .extra_head = get_head_nodes (soup )
111
115
self .body = get_body_content (soup )
@@ -117,7 +121,7 @@ class Author(Resource):
117
121
118
122
def __post_init__ (self ) -> None :
119
123
"""Initialize the rest of the fields from the Markdown."""
120
- md_file = HERE / "gallery/authors" / f"{ self .path } .md"
124
+ md_file = HERE / "gallery/authors" / f"{ self .name } .md"
121
125
md_fm = frontmatter .load (md_file )
122
126
self .title = md_fm .get ("title" , "" )
123
127
md = MarkdownIt ()
@@ -129,14 +133,13 @@ class Page(Resource):
129
133
"""A Markdown+frontmatter driven content page."""
130
134
131
135
subtitle : str = ""
132
- body : str = ""
133
136
134
137
def __post_init__ (self ) -> None :
135
138
"""Extract content from either Markdown or HTML file."""
136
- md_file = HERE / "pages" / f"{ self .path } .md"
137
- html_file = HERE / "pages" / f"{ self .path } .html"
139
+ md_file = HERE / "pages" / f"{ self .name } .md"
140
+ html_file = HERE / "pages" / f"{ self .name } .html"
138
141
139
- # If this self.path resolves to a Markdown file, use it first
142
+ # If this self.name resolves to a Markdown file, use it first
140
143
if md_file .exists ():
141
144
md_fm = frontmatter .load (md_file )
142
145
self .title = md_fm .get ("title" , "" )
@@ -157,16 +160,16 @@ def __post_init__(self) -> None:
157
160
if body_node and isinstance (body_node , Tag ):
158
161
self .body = body_node .prettify ()
159
162
else : # pragma: no cover
160
- raise ValueError (f"No page at { self .path } " )
163
+ raise ValueError (f"No page at { self .name } " )
161
164
162
165
163
166
@dataclass
164
167
class Resources :
165
168
"""Container for all resources in site."""
166
169
167
- authors : dict [PurePath , Author ] = field (default_factory = dict )
168
- examples : dict [PurePath , Example ] = field (default_factory = dict )
169
- pages : dict [PurePath , Page ] = field (default_factory = dict )
170
+ authors : dict [str , Author ] = field (default_factory = dict )
171
+ examples : dict [str , Example ] = field (default_factory = dict )
172
+ pages : dict [str , Page ] = field (default_factory = dict )
170
173
171
174
172
175
def get_sorted_paths (target_dir : Path , only_dirs : bool = True ) -> list [PurePath ]:
@@ -183,26 +186,23 @@ def get_resources() -> Resources:
183
186
"""Factory to construct all the resources in the site."""
184
187
resources = Resources ()
185
188
186
- # Load the examples
187
- examples = HERE / "gallery/examples"
188
- for example in get_sorted_paths (examples ):
189
- this_path = PurePath (example .name )
190
- this_example = Example (path = this_path )
191
- resources .examples [this_path ] = this_example
192
-
193
189
# Load the authors
194
190
authors = HERE / "gallery/authors"
195
191
for author in get_sorted_paths (authors , only_dirs = False ):
196
- this_path = PurePath (author .stem )
197
- this_author = Author (path = this_path )
198
- resources .authors [this_path ] = this_author
192
+ this_author = Author (name = author .stem )
193
+ resources .authors [author .stem ] = this_author
194
+
195
+ # Load the examples
196
+ examples = HERE / "gallery/examples"
197
+ for example in get_sorted_paths (examples ):
198
+ this_example = Example (example .stem )
199
+ resources .examples [example .stem ] = this_example
199
200
200
201
# Load the Pages
201
202
pages_dir = HERE / "pages"
202
203
pages = [e for e in pages_dir .iterdir ()]
203
204
for page in pages :
204
- this_path = PurePath (page .stem )
205
- this_page = Page (path = this_path )
206
- resources .pages [this_path ] = this_page
205
+ this_page = Page (name = page .stem )
206
+ resources .pages [page .stem ] = this_page
207
207
208
208
return resources
0 commit comments