From f4fa59a413aba031dffd7d75380b28e225b9df9c Mon Sep 17 00:00:00 2001 From: Matteo Zanetti <97408052+matteozanettii@users.noreply.github.com> Date: Mon, 15 Jun 2026 15:33:18 +0200 Subject: [PATCH] Refactor publishFS for native execution of examples Refactor publishFS to use native execution for examples, capturing output and images directly. Remove legacy HTML generation code and streamline the process. --- toolbox/utilities_help/publishFS.m | 142 ++++++++++++----------------- 1 file changed, 57 insertions(+), 85 deletions(-) diff --git a/toolbox/utilities_help/publishFS.m b/toolbox/utilities_help/publishFS.m index 56f781245..0e5bb4037 100644 --- a/toolbox/utilities_help/publishFS.m +++ b/toolbox/utilities_help/publishFS.m @@ -3464,103 +3464,75 @@ fprintf(filetmp,'%s',ExToExec); fclose(filetmp); - options=struct; - options = supplyDefaultOptions(options); - options.codeToEvaluate=[name 'tmp']; - options.createThumbnail=0; - prefix=name; - - if verLessThanFS([26,1]) - [dom,cellBoundaries] = m2mxdom(ExToExec); - [dom,laste] = evalmxdom(fullPathToScript,dom,cellBoundaries,prefix,imagesDir,outputDir,options); - - drawnow; - - ext='html'; - AbsoluteFilename = fullfile(outputDir,[prefix '.' ext]); - [xResultURI]=xslt(dom,options.stylesheet,AbsoluteFilename); - - drawnow; - - % load html output in a string and extract the parts which are required - if ismac || isunix - fileHTML = fopen(xResultURI(6:end), 'r+'); - elseif ispc - fileHTML = fopen(xResultURI(7:end), 'r+'); + % --- START OF NEW NATIVE BLOCK (No Java) --- + laste = ''; + totex = numexToExec + numextraexToExec; + texttoadd = cell(totex, 1); + + % Split the large string of examples into single blocks + % using the '%% Ex' tag as a separator. + esempiRaw = strsplit(ExToExec, '%% Ex'); + + % Remove any empty blocks created by the split + esempiRaw = esempiRaw(~cellfun('isempty', strtrim(esempiRaw))); + + for k = 1:length(esempiRaw) + blocco = esempiRaw{k}; + + % The block starts with the label (e.g., "1" or "Extra1"). + % Find the first newline to isolate the actual code. + primoACapo = regexp(blocco, '\n', 'once'); + if ~isempty(primoACapo) + codiceDaEseguire = blocco(primoACapo+1:end); else - fileHTML = fopen(xResultURI(6:end), 'r+'); - disp('Cannot recognize platform: I use unix as default') + codiceDaEseguire = blocco; end - % Insert the file into fstring - fstringHTML=fscanf(fileHTML,'%c'); + txtOutput = ''; + imgHTML = ''; - else - [dom,cellBoundaries] = m2mxdomNEW(ExToExec); - [dom,laste] = evalmxdomNEW(fullPathToScript,dom,cellBoundaries,prefix,imagesDir,outputDir,options); - drawnow; - AbsoluteFilename = fullfile(outputDir,'out_pretty.xml'); - import matlab.io.xml.transform.* - tr = Transformer(); - writer = matlab.io.xml.dom.DOMWriter; - % writer.Configuration.FormatPrettyPrint = true; % indent + newlines - writeToFile(writer, dom, AbsoluteFilename); - filexsl=[path2privateFS filesep 'mxdom2simplehtml.xsl']; - fstringHTML=char(transformToString(tr, AbsoluteFilename, filexsl)); - end - - % Now remove the temporary .m file with the examples which had been created - delete(fullPathToScript) + try + % Close any figures opened by previous examples + close all; + % 1. TEXT CAPTURE: Execute the code natively and capture the output + txtOutput = evalc(codiceDaEseguire); - totex=numexToExec+numextraexToExec; - texttoadd=cell(totex,1); - - fHTML=regexp(fstringHTML,'
','once'); - end - % If fHTML is still empty it means that the ouptut only generates images - if isempty(fHTML) - fHTML=regexp(fstringHTML,'','once'); - % if fHTML is still empty produce an error - if isempty(fHTML) - errmsg='Parser could not run an example'; - error('FSDA:publishFS:WrngOutFolder',errmsg) - end - end + % 2. IMAGE CAPTURE: Check if the example generated any plots (figures) + figHandles = findobj('Type', 'figure'); + if ~isempty(figHandles) + for f = 1:length(figHandles) + % Create a unique name for the saved image + imgName = sprintf('%s_ex%d_fig%d.png', name, k, f); + imgPath = fullfile(imagesDir, imgName); + saveas(figHandles(f), imgPath); - for j=1:totex - if j
1 && length(fHTML)>1 - try - fcand=fstringHTML(fHTML(j):fHTML(j+1)-1); - catch - fcand=fstringHTML(fHTML(j):end); + % Create the HTML tag to embed the image + imgHTML = [imgHTML ' ']; + end + close(figHandles); % Cleanup end - else - fendHTML=regexp(fstringHTML,'
','once'); - if isempty(fcode) - fcode=Inf; - end - fimg=regexp(fcand,'']; end + htmlBlocco = [htmlBlocco imgHTML]; + + % Save the result ready to be injected into the final file + texttoadd{k} = htmlBlocco; end + % Remove the previously created temporary file + delete(fullPathToScript); + % --- END OF NEW NATIVE BLOCK --- + % Now insert the strings which have been stored in cell texttoadd in the % appropriate position of outstring a=cell2mat(listEx(:,4)); @@ -4363,4 +4335,4 @@ % end % end -%FScategory:UTIHELP \ No newline at end of file +%FScategory:UTIHELP' strtrim(txtOutput) '