1515
1616from psc .here import HERE
1717
18-
1918EXCLUSIONS = ("pyscript.css" , "pyscript.js" , "favicon.png" )
2019
2120
@@ -35,16 +34,6 @@ def tag_filter(
3534 return True
3635
3736
38- def get_description (index_html_file : Path ) -> str :
39- """Read an index.md if present and convert to HTML."""
40- md_file = index_html_file .parent / "index.md"
41- if not md_file .exists ():
42- return ""
43- md_content = md_file .read_text ()
44- md = MarkdownIt ()
45- return str (md .render (md_content ))
46-
47-
4837def get_head_nodes (s : BeautifulSoup ) -> str :
4938 """Make post init simpler by putting head node helper here."""
5039 head_nodes = [
@@ -57,24 +46,10 @@ def get_head_nodes(s: BeautifulSoup) -> str:
5746 return ""
5847
5948
60- def get_main_node_content (s : BeautifulSoup ) -> str :
61- """Get the main node but raise an exception if not present."""
62- # Moving to a helper to allow testing ValueError without needing
63- # to ship a broken (non-main) example.
64- main_element = s .select_one ("main" )
65- if main_element is None : # pragma: no cover
66- raise ValueError ("Example file has no <main> element" )
67- return f"{ main_element .decode_contents ()} "
68-
69-
70- def get_pyscript_nodes (s : BeautifulSoup ) -> str :
71- """Find any pyscript nodes that are NOT ``py-config``."""
72- pyscript_nodes = [
73- pyscript .prettify ()
74- for pyscript in s .select ("body > *" )
75- if pyscript and pyscript .name .startswith ("py-" ) and pyscript .name != "py-config"
76- ]
77- return "\n " .join (pyscript_nodes )
49+ def get_body_content (s : BeautifulSoup ) -> str :
50+ """Get the body node but raise an exception if not present."""
51+ body_element = s .select_one ("body" )
52+ return f"{ body_element .decode_contents ()} "
7853
7954
8055@dataclass
@@ -83,7 +58,7 @@ class Resource:
8358
8459 path : PurePath
8560 title : str = ""
86- main : str = ""
61+ body : str = ""
8762 extra_head : str = ""
8863
8964
@@ -98,37 +73,25 @@ class Example(Resource):
9873 """
9974
10075 description : str = ""
101- extra_pyscript : str = ""
10276 subtitle : str = ""
10377
10478 def __post_init__ (self ) -> None :
10579 """Extract most of the data from the HTML file."""
80+ # Title, subtitle, description come from the example's MD file.
81+ index_md_file = HERE / "gallery/examples" / self .path / "index.md"
82+ md_fm = frontmatter .load (index_md_file )
83+ self .title = md_fm .get ("title" , "" )
84+ self .subtitle = md_fm .get ("subtitle" , "" )
85+ md = MarkdownIt ()
86+ self .description = str (md .render (md_fm .content ))
87+
88+ # Main, extra head example's HTML file.
10689 index_html_file = HERE / "gallery/examples" / self .path / "index.html"
10790 if not index_html_file .exists ():
10891 raise ValueError (f"No example at { self .path } " )
10992 soup = BeautifulSoup (index_html_file .read_text (), "html5lib" )
110-
111- # Title
112- title_node = soup .select_one ("title" )
113- self .title = title_node .text if title_node else ""
114-
115- # Subtitle
116- subtitle_node = soup .select_one ('meta[name="subtitle"]' )
117- assert subtitle_node # noqa
118- subtitle = cast (str , subtitle_node .get ("content" , "" ))
119- self .subtitle = subtitle
120-
121- # Description
122- self .description = get_description (index_html_file )
123-
124- # Head
12593 self .extra_head = get_head_nodes (soup )
126-
127- # Main
128- self .main = get_main_node_content (soup )
129-
130- # Extra PyScript
131- self .extra_pyscript = get_pyscript_nodes (soup )
94+ self .body = get_body_content (soup )
13295
13396
13497@dataclass
0 commit comments