@@ -25,7 +25,8 @@ def setup(app: Sphinx) -> None:
2525 """Setup the extension."""
2626 from sphinx_timeline import __version__
2727
28- app .connect ("builder-inited" , add_css )
28+ app .connect ("builder-inited" , add_html_assets )
29+ app .connect ("html-page-context" , load_html_assets )
2930 app .add_directive ("timeline" , TimelineDirective )
3031 app .add_node (
3132 TimelineDiv ,
@@ -43,23 +44,47 @@ def setup(app: Sphinx) -> None:
4344 }
4445
4546
46- def add_css (app : Sphinx ):
47- """Copy the CSS to the build directory."""
47+ def add_html_assets (app : Sphinx ) -> None :
48+ """Add the HTML assets to the build directory."""
49+ if (not app .builder ) or app .builder .format != "html" :
50+ return
4851 # setup up new static path in output dir
4952 static_path = (Path (app .outdir ) / "_sphinx_timeline_static" ).absolute ()
5053 static_path .mkdir (exist_ok = True )
54+ for path in static_path .glob ("**/*" ):
55+ path .unlink ()
5156 app .config .html_static_path .append (str (static_path ))
52- # Read the css content and hash it
53- content = resources .read_text (static_module , "default.css" )
54- hash = hashlib .md5 (content .encode ("utf8" )).hexdigest ()
55- # Write the css file
56- css_path = static_path / f"tl_default.{ hash } .css"
57- app .add_css_file (css_path .name )
58- if css_path .exists ():
57+ for resource in resources .contents (static_module ):
58+ if not resource .endswith (".css" ) and not resource .endswith (".js" ):
59+ continue
60+ # Read the content and hash it
61+ content = resources .read_text (static_module , resource )
62+ hash = hashlib .md5 (content .encode ("utf8" )).hexdigest ()
63+ # Write the file
64+ name , ext = resource .split ("." , maxsplit = 1 )
65+ write_path = static_path / f"{ name } .{ hash } .{ ext } "
66+ write_path .write_text (content , encoding = "utf8" )
67+
68+
69+ def load_html_assets (app : Sphinx , pagename : str , * args , ** kwargs ) -> None :
70+ """Ensure the HTML assets are loaded in the page, if necessary."""
71+ if (not app .builder ) or app .builder .format != "html" :
5972 return
60- for path in static_path .glob ("*.css" ):
61- path .unlink ()
62- css_path .write_text (content , encoding = "utf8" )
73+ if (not app .env ) or not app .env .metadata .get (pagename , {}).get ("timeline" , False ):
74+ return
75+ for resource in resources .contents (static_module ):
76+ if not resource .endswith (".css" ) and not resource .endswith (".js" ):
77+ continue
78+ # Read the content and hash it
79+ content = resources .read_text (static_module , resource )
80+ hash = hashlib .md5 (content .encode ("utf8" )).hexdigest ()
81+ # add the file to the context
82+ name , ext = resource .split ("." , maxsplit = 1 )
83+ write_name = f"{ name } .{ hash } .{ ext } "
84+ if ext == "css" :
85+ app .add_css_file (write_name )
86+ if ext == "js" :
87+ app .add_js_file (write_name )
6388
6489
6590class TimelineDiv (nodes .General , nodes .Element ):
@@ -251,6 +276,8 @@ def run(self) -> list[nodes.Element]:
251276 )
252277 item_node .append (item_container )
253278
279+ self .env .metadata [self .env .docname ]["timeline" ] = True
280+
254281 return [container ]
255282
256283
0 commit comments