1
## Copyright (C) 2010 Fotios Kasolis <fotios.kasolis@gmail.com>
3
## This program is free software; you can redistribute it and/or modify it under
4
## the terms of the GNU General Public License as published by the Free Software
5
## Foundation; either version 3 of the License, or (at your option) any later
8
## This program is distributed in the hope that it will be useful, but WITHOUT
9
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13
## You should have received a copy of the GNU General Public License along with
14
## this program; if not, see <http://www.gnu.org/licenses/>.
17
## @deftypefn {Function File} {} publish (@var{filename})
18
## @deftypefnx {Function File} {} publish (@var{filename}, @var{options})
19
## Produces latex reports from scripts.
22
## publish (@var{my_script})
26
## where the argument is a string that contains the file name of
27
## the script we want to report.
29
## If two arguments are given, they are interpreted as follows.
32
## publish (@var{filename}, [@var{option}, @var{value}, ...])
36
## The following options are available:
41
## the only available format values are the strings `latex' and
47
## string that specifies the image format, valid formats are `pdf',
48
## `png', and `jpg'(or `jpeg').
53
## boolean value that specifies if the source code will be included
59
## boolean value that specifies if execution results will be included
64
## Default @var{options}
67
## @item format = latex
69
## @item imageFormat = pdf
80
## @item Any additional non-valid field is removed without
83
## @item To include several figures in the resulting report you must
84
## use figure with a unique number for each one of them.
86
## @item You do not have to save the figures manually, publish will
89
## @item The functions works only for the current path and no way ...
90
## to specify other path is allowed.
94
## Assume you have the script `myscript.m' which looks like
107
## You can then call publish with default @var{options}
110
## publish("myscript")
114
function publish (file_name, varargin)
116
if ((nargin < 1) || (rem (numel (varargin), 2) != 0))
120
if (!strcmp (file_name(end-1:end), ".m"))
121
ifile = strcat (file_name, ".m");
122
ofile = strcat (file_name, ".tex");
125
ofile = strcat (file_name(1:end-1), "tex");
128
if (exist (ifile, "file") != 2)
129
error ("File %s does not exist.", ifile);
132
options = set_default (struct ());
133
options = read_options (varargin, "op1", "format imageFormat showCode evalCode", "default", options);
135
if (strcmpi (options.format, "latex"))
136
create_latex (ifile, ofile, options);
137
elseif strcmpi(options.format, "html")
138
create_html (ifile, options);
143
function def_options = set_default (options);
145
if (!isfield (options, "format"))
146
def_options.format = "latex";
147
elseif (!ischar (options.format))
148
error("Option format must be a string.");
150
valid_formats={"latex", "html"};
151
validity_test = strcmpi (valid_formats, options.format);
152
if (isempty (find (validity_test)))
153
error ("The supplied format is not currently supported.");
155
def_options.format = options.format;
159
if (! isfield (options, "imageFormat"))
160
def_options.imageFormat = "pdf";
161
elseif (! ischar(options.imageFormat))
162
error("Option imageFormat must be a string.");
164
valid_formats = {"pdf", "png", "jpg", "jpeg"};
165
validity_test = strcmpi (valid_formats, options.imageFormat);
166
if (isempty (find (validity_test)))
167
error ("The supplied image format is not available.");
169
def_options.imageFormat = options.imageFormat;
173
if (!isfield (options,"showCode"))
174
def_options.showCode = true;
175
elseif (!isbool (options.showCode))
176
error ("Option showCode must be a boolean.");
178
def_options.showCode = options.showCode;
181
if (!isfield (options,"evalCode"))
182
def_options.evalCode = true;
183
elseif (!isbool (options.evalCode))
184
error ("Option evalCode must be a boolean.");
186
def_options.evalCode = options.evalCode;
190
function create_html (ifile, options)
192
ofile = strcat (ifile(1:end-1), "html");
193
html_start = "<html>\n<body>\n";
194
html_end = "</body>\n</html>\n";
197
section1_title = "<h2>Source code</h2>\n";
198
fid = fopen (ifile, "r");
199
source_code = fread (fid, "*char")';
207
section2_title = "<h2>Execution results</h2>\n";
208
oct_command = strcat ("<listing>octave> ", ifile(1:end-2), "\n");
209
script_result = exec_script (ifile);
216
[section3_title, disp_fig] = exec_print (ifile, options);
218
final_document = strcat (html_start, section1_title, "<listing>\n", source_code,"\n",...
219
"</listing>\n", section2_title, oct_command, script_result,...
220
"</listing>", section3_title, disp_fig, html_end);
223
fid = fopen (ofile, "w");
224
fputs (fid, final_document);
229
function create_latex (ifile, ofile, options)
231
\\documentclass[a4paper,12pt]{article}\n\
232
\\usepackage{listings}\n\
233
\\usepackage{graphicx}\n\
234
\\usepackage{color}\n\
235
\\usepackage[T1]{fontenc}\n\
236
\\definecolor{lightgray}{rgb}{0.9,0.9,0.9}\n";
238
listing_source_option = "\
240
language = Octave,\n\
241
basicstyle =\\footnotesize,\n\
243
numberstyle = \\footnotesize,\n\
244
backgroundcolor=\\color{lightgray},\n\
249
listing_exec_option = "\
251
language = Octave,\n\
252
basicstyle =\\footnotesize,\n\
254
backgroundcolor=\\color{white},\n\
260
section1_title = strcat ("\\section*{Source code: \\texttt{", ifile, "}}\n");
261
source_code = strcat ("\\lstinputlisting{", ifile, "}\n");
268
section2_title = "\\section*{Execution results}\n";
269
oct_command = strcat ("octave> ", ifile(1:end-2), "\n");
270
script_result = exec_script (ifile);
277
[section3_title, disp_fig] = exec_print (ifile, options);
279
final_document = strcat (latex_preamble, listing_source_option,
280
"\\begin{document}\n",
281
section1_title, source_code,
282
section2_title, listing_exec_option,
283
"\\begin{lstlisting}\n",
284
oct_command, script_result,
285
"\\end{lstlisting}\n",
292
fid = fopen (ofile, "w");
293
fputs(fid, final_document);
297
function script_result = exec_script (ifile)
298
diary publish_tmp_script.txt
299
eval (ifile(1:end-2))
301
fid = fopen ("publish_tmp_script.txt", 'r');
302
script_result = fread (fid, '*char')';
304
delete ("publish_tmp_script.txt");
307
function [section3_title, disp_fig] = exec_print (ifile, options)
308
figures = findall (0, "type", "figure");
311
if (!isempty (figures))
312
for nfig = 1:length (figures)
313
figure (figures(nfig));
314
print (sprintf ("%s%d.%s", ifile(1:end-2), nfig, options.imageFormat),
315
sprintf ("-d%s", options.imageFormat), "-color");
316
if (strcmpi (options.format, "html"));
317
section3_title = "<h2>Generated graphics</h2>\n";
318
disp_fig = strcat (disp_fig, "<img src=\"", ifile(1:end-2),
319
sprintf ("%d", nfig), ".", options.imageFormat, "\"/>");
320
elseif (strcmpi (options.format, "latex"))
321
section3_title = "\\section*{Generated graphics}\n";
322
disp_fig = strcat (disp_fig, "\\includegraphics[scale=0.6]{", ifile(1:end-2),
323
sprintf("%d",nfig), "}\n");
330
# * ADD VARARGIN FOR ADDITIONAL FILES SOURCES TO BE INLCUDED AS
331
# APPENDICES (THIS SOLVES THE PROBLEM OF FUNCTIONS SINCE YOU CAN
332
# PUT THE FUNCTION CALL IN SCRIPT CALL PUBLISH(SCRIPT) AND ADD
333
# THE FUNCTIONS CODE AS APPENDIX)
335
# * KNOWN PROBLEM: THE COMMENTING LINES IN HTML