1717from psc .here import HERE
1818from psc .here import PYODIDE
1919
20+
2021EXCLUSIONS = ("pyscript.css" , "pyscript.js" , "favicon.png" )
2122
2223
2324def tag_filter (
24- tag : Tag ,
25- exclusions : tuple [str , ...] = EXCLUSIONS ,
25+ tag : Tag ,
26+ exclusions : tuple [str , ...] = EXCLUSIONS ,
2627) -> bool :
2728 """Filter nodes from example that should not get included."""
2829 attr = "href" if tag .name == "link" else "src"
@@ -74,7 +75,7 @@ def get_body_content(s: BeautifulSoup, test_path: Path = PYODIDE) -> str:
7475class Resource :
7576 """Base dataclass used for all resources."""
7677
77- path : PurePath
78+ name : str
7879 title : str = ""
7980 body : str = ""
8081 extra_head : str = ""
@@ -91,21 +92,24 @@ class Example(Resource):
9192 """
9293
9394 subtitle : str = ""
95+ description : str = ""
96+ author : str | None = None
9497
9598 def __post_init__ (self ) -> None :
9699 """Extract most of the data from the HTML file."""
97100 # 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"
99102 md_fm = frontmatter .load (index_md_file )
100103 self .title = md_fm .get ("title" , "" )
104+ self .author = md_fm .get ("author" , "" )
101105 self .subtitle = md_fm .get ("subtitle" , "" )
102106 md = MarkdownIt ()
103- self .body = str (md .render (md_fm .content ))
107+ self .description = str (md .render (md_fm .content ))
104108
105109 # 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"
107111 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 } " )
109113 soup = BeautifulSoup (index_html_file .read_text (), "html5lib" )
110114 self .extra_head = get_head_nodes (soup )
111115 self .body = get_body_content (soup )
@@ -117,7 +121,7 @@ class Author(Resource):
117121
118122 def __post_init__ (self ) -> None :
119123 """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"
121125 md_fm = frontmatter .load (md_file )
122126 self .title = md_fm .get ("title" , "" )
123127 md = MarkdownIt ()
@@ -129,14 +133,13 @@ class Page(Resource):
129133 """A Markdown+frontmatter driven content page."""
130134
131135 subtitle : str = ""
132- body : str = ""
133136
134137 def __post_init__ (self ) -> None :
135138 """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"
138141
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
140143 if md_file .exists ():
141144 md_fm = frontmatter .load (md_file )
142145 self .title = md_fm .get ("title" , "" )
@@ -157,16 +160,16 @@ def __post_init__(self) -> None:
157160 if body_node and isinstance (body_node , Tag ):
158161 self .body = body_node .prettify ()
159162 else : # pragma: no cover
160- raise ValueError (f"No page at { self .path } " )
163+ raise ValueError (f"No page at { self .name } " )
161164
162165
163166@dataclass
164167class Resources :
165168 """Container for all resources in site."""
166169
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 )
170173
171174
172175def get_sorted_paths (target_dir : Path , only_dirs : bool = True ) -> list [PurePath ]:
@@ -183,26 +186,23 @@ def get_resources() -> Resources:
183186 """Factory to construct all the resources in the site."""
184187 resources = Resources ()
185188
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-
193189 # Load the authors
194190 authors = HERE / "gallery/authors"
195191 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
199200
200201 # Load the Pages
201202 pages_dir = HERE / "pages"
202203 pages = [e for e in pages_dir .iterdir ()]
203204 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
207207
208208 return resources
0 commit comments