44# - Run with: julia --project=. scripts/export_pluto.jl
55
66using Pkg
7- Pkg. activate (" ." ) # prefer repo environment if present
8-
9- import Dates
10- using Printf
117using Logging
128
13- notebook_dir = joinpath (pwd (), " notebooks" )
14- output_dir = joinpath (pwd (), " public" )
15- mkpath (output_dir)
16-
17- if ! isdir (notebook_dir)
18- @info " No notebooks/ directory found; nothing to export."
19- exit (0 )
20- end
9+ const EXPORTER_PKG = " PlutoStaticHTML"
2110
22- notebooks = filter (f -> endswith (f, " .jl" ), readdir (notebook_dir; join= true ))
23- if isempty (notebooks)
24- @info " No .jl notebooks found in notebooks/. Nothing to do."
25- exit (0 )
26- end
27-
28- const exporter_pkg = " PlutoStaticHTML"
29-
30- # Ensure exporter is available (try to load, otherwise add it)
3111function ensure_exporter (pkg:: String )
3212 try
3313 @eval using PlutoStaticHTML
3414 return true
35- catch
36- @info " $pkg not present in environment, trying to add it for this run..."
15+ catch err
16+ @info ( " $pkg not present in environment, trying to add it for this run..." )
3717 try
3818 Pkg. add (pkg)
3919 @eval using PlutoStaticHTML
4020 return true
41- catch err
42- @error " Failed to install or load $pkg : $err "
21+ catch err2
22+ @error ( " Failed to install or load $pkg : $err2 " )
4323 return false
4424 end
4525 end
4626end
4727
48- ok = ensure_exporter (exporter_pkg)
49- if ! ok
50- @error " Exporter $exporter_pkg not available. Add it to Project.toml or install it manually."
51- exit (1 )
52- end
53-
54- # Try several common export function names in PlutoStaticHTML
5528function try_export_with_known_apis (mod, nbpath:: String , outpath:: String )
5629 # 1) export(nb, outpath)
5730 if isdefined (mod, :export )
5831 try
59- @info " Calling PlutoStaticHTML.export($( nbpath) , $( outpath) ) "
32+ @info ( " Calling PlutoStaticHTML.export($nbpath , $outpath )" )
6033 getfield (mod, :export )(nbpath, outpath)
6134 return true
6235 catch err
63- @warn " PlutoStaticHTML.export failed: $err "
36+ @warn ( " PlutoStaticHTML.export failed: $err " )
6437 end
6538 end
6639
6740 # 2) save_html(nb, outpath)
6841 if isdefined (mod, :save_html )
6942 try
70- @info " Calling PlutoStaticHTML.save_html($( nbpath) , $( outpath) ) "
43+ @info ( " Calling PlutoStaticHTML.save_html($nbpath , $outpath )" )
7144 getfield (mod, :save_html )(nbpath, outpath)
7245 return true
7346 catch err
74- @warn " PlutoStaticHTML.save_html failed: $err "
47+ @warn ( " PlutoStaticHTML.save_html failed: $err " )
7548 end
7649 end
7750
7851 # 3) notebook_to_html(nb) -> returns HTML string
7952 if isdefined (mod, :notebook_to_html )
8053 try
81- @info " Calling PlutoStaticHTML.notebook_to_html($( nbpath) ) -> writing to $( outpath) "
54+ @info ( " Calling PlutoStaticHTML.notebook_to_html($nbpath ) -> writing to $outpath " )
8255 html = getfield (mod, :notebook_to_html )(nbpath)
8356 open (outpath, " w" ) do io
8457 write (io, html)
8558 end
8659 return true
8760 catch err
88- @warn " PlutoStaticHTML.notebook_to_html failed: $err "
61+ @warn ( " PlutoStaticHTML.notebook_to_html failed: $err " )
8962 end
9063 end
9164
92- # 4) try to autodiscover any function name containing "html" or "export"
65+ # 4) autodiscover candidate functions with "html", "export" or "render" in the name
9366 fnames = filter (n -> occursin (" html" , String (n)) || occursin (" export" , String (n)) || occursin (" render" , String (n)), names (mod, all= true ))
9467 for fname in fnames
9568 try
9669 f = getfield (mod, fname)
97- @info " Attempting autodiscovered function: $(fname) "
70+ @info ( " Attempting autodiscovered function: $(fname) " )
9871 # Try calling with (nb, outpath) then with (nb) and write result if string-like
9972 try
10073 f (nbpath, outpath)
@@ -107,39 +80,62 @@ function try_export_with_known_apis(mod, nbpath::String, outpath::String)
10780 end
10881 end
10982 catch err
110- @warn " Autodiscovered attempt $(fname) failed: $err "
83+ @warn ( " Autodiscovered attempt $(fname) failed: $err " )
11184 end
11285 end
11386
11487 return false
11588end
11689
117- # Perform exports
118- success_count = 0
119- for nb in notebooks
120- name = splitext (basename (nb))[1 ]
121- outpath = joinpath (output_dir, " $(name) .html" )
122- @info " Exporting $(nb) -> $(outpath) "
90+ function main ()
91+ notebook_dir = joinpath (pwd (), " notebooks" )
92+ output_dir = joinpath (pwd (), " public" )
93+ mkpath (output_dir)
12394
124- try
125- ok = try_export_with_known_apis (PlutoStaticHTML, nb, outpath)
126- if ok
127- @info " Export succeeded for $(nb) "
128- success_count += 1
129- continue
130- else
131- @warn " No known export API succeeded for $(nb) ."
132- @warn " Inspect PlutoStaticHTML.jl API or update this script to call the correct function."
95+ if ! isdir (notebook_dir)
96+ @info " No notebooks/ directory found; nothing to export."
97+ return
98+ end
99+
100+ notebooks = filter (f -> endswith (f, " .jl" ), readdir (notebook_dir; join= true ))
101+ if isempty (notebooks)
102+ @info " No .jl notebooks found in notebooks/. Nothing to do."
103+ return
104+ end
105+
106+ ok = ensure_exporter (EXPORTER_PKG)
107+ if ! ok
108+ @error " Exporter $EXPORTER_PKG not available. Add it to Project.toml or install it manually."
109+ exit (1 )
110+ end
111+
112+ # Export loop with local counter
113+ success_count = 0
114+ for nb in notebooks
115+ name = splitext (basename (nb))[1 ]
116+ outpath = joinpath (output_dir, " $(name) .html" )
117+ @info " Exporting $nb -> $outpath "
118+
119+ try
120+ exported = try_export_with_known_apis (PlutoStaticHTML, nb, outpath)
121+ if exported
122+ @info " Export succeeded for $nb "
123+ success_count += 1
124+ else
125+ @warn " No known export API succeeded for $nb . See logs above."
126+ end
127+ catch err
128+ @error " Unexpected error exporting $nb : $err "
133129 end
134- catch err
135- @error " Unexpected error exporting $(nb) : $err "
136130 end
137- end
138131
139- if success_count == 0
140- @error " No notebooks were exported. See messages above to diagnose."
141- exit (1 )
142- else
143- @info " Export complete: $(success_count) notebook(s) exported to public/."
144- exit (0 )
132+ if success_count == 0
133+ @error " No notebooks were exported. See messages above to diagnose."
134+ exit (1 )
135+ else
136+ @info " Export complete: $success_count notebook(s) exported to public/."
137+ exit (0 )
138+ end
145139end
140+
141+ main ()
0 commit comments