|
| 1 | +%% PUBLISH_GEN_INDEX_HTML generate index.html for package docs |
| 2 | +% publish (generate) docs from Matlab project |
| 3 | +% called from buildfile.m |
| 4 | +% |
| 5 | +% Ref: |
| 6 | +% * https://www.mathworks.com/help/matlab/ref/publish.html |
| 7 | +% * https://www.mathworks.com/help/matlab/matlab_prog/marking-up-matlab-comments-for-publishing.html |
| 8 | +% |
| 9 | +% for package code -- assumes no classes and depth == 1 |
| 10 | +function publish_gen_index_html(pkg_name, tagline, outdir) |
| 11 | + |
| 12 | +pkg = what("+" + pkg_name); |
| 13 | +% "+" avoids picking up cwd of same name |
| 14 | +assert(~isempty(pkg), pkg_name + " is not detected as a Matlab directory or package") |
| 15 | + |
| 16 | +%% generate docs |
| 17 | +readme = fullfile(outdir, "index.html"); |
| 18 | + |
| 19 | +if ~isfolder(outdir) |
| 20 | + mkdir(outdir); |
| 21 | +end |
| 22 | + |
| 23 | +txt = ["<!DOCTYPE html> <head> <title>" + pkg_name + " API</title> <body>", ... |
| 24 | + "<h1>" + pkg_name + " API</h1>", ... |
| 25 | + tagline, ... |
| 26 | + "<h2>API Reference</h2>"]; |
| 27 | +fid = fopen(readme, 'w'); |
| 28 | +fprintf(fid, join(txt, "\n")); |
| 29 | + |
| 30 | +for sub = pkg.m.' |
| 31 | + |
| 32 | +s = sub{1}; |
| 33 | +[~, name] = fileparts(s); |
| 34 | +doc_fn = publish(pkg_name + "." + name, evalCode=false, outputDir=outdir); |
| 35 | +disp(doc_fn) |
| 36 | + |
| 37 | +% inject summary into Readme.md |
| 38 | +summary = split(string(help(pkg_name + "." + name)), newline); |
| 39 | +words = split(strip(summary(1)), " "); |
| 40 | + |
| 41 | +% purposefully this will error if no docstring |
| 42 | +fname = words(1); |
| 43 | +if(lower(fname) ~= lower(name)) |
| 44 | + error("fname %s does not match name %s", fname, name) |
| 45 | +end |
| 46 | + |
| 47 | +line = "<a href=" + name + ".html>" + fname + "</a> "; |
| 48 | +if(length(words) > 1) |
| 49 | + line = line + join(words(2:end)); |
| 50 | +end |
| 51 | + |
| 52 | +fprintf(fid, line + "<br>\n"); |
| 53 | + |
| 54 | +end |
| 55 | + |
| 56 | +fprintf(fid, "</body> </html>"); |
| 57 | + |
| 58 | +fclose(fid); |
| 59 | + |
| 60 | +end |
0 commit comments