Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion buildfile.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,26 @@ function docsTask(~)
nb = fullfile(path, name+".ipynb");

fprintf("Rendering %s as %s\n", mlx, nb)
export(mlx, nb, Run=true);
export(mlx, nb, Run=true, FigureFormat="png");

% MATLAB currently (R2025b) exports embedded images in output cells
% with the text/html mimetype. The images are embedded in <img> tags
% with a data URL. This jq (https://jqlang.org/) filter extracts the
% data from this URL and replaces the "text/html" object in the output
% data with an "image/png" one. This works, but depends on how MATLAB
% chooses to export Jupyter notebooks. Bad things will also happen if
% any other output cells have the text/html mimetype.
jqfilter = ['''.cells[].outputs[]?.data |= if has("text/html") then ' ...
'{"image/png" : [."text/html"[] | ' ...
'capture("data:image/png;base64,(?<x>[A-Za-z0-9/=+]*)\"").x]} ' ...
'else . end'' '];
% jq will by default print colored output, which gets stored as ANSI
% escape codes. The -M option turns this off.
[status, cout] = system(['jq -M ' jqfilter char(nb)]);
if status ~= 0
error("jq unable to recode images: [%s] %s", status, cout);
end
writelines(cout, nb);
end

end