~ubuntu-branches/ubuntu/wily/octave-miscellaneous/wily

« back to all changes in this revision

Viewing changes to inst/publish.m

  • Committer: Package Import Robot
  • Author(s): Sébastien Villemot, Rafael Laboissiere, Sébastien Villemot
  • Date: 2012-10-17 13:40:55 UTC
  • mfrom: (1.2.1) (12 sid)
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: package-import@ubuntu.com-20121017134055-vatltexghy77fnv7
Tags: 1.2.0-1
[ Rafael Laboissiere ]
* Imported Upstream version 1.2.0
* Bump Standards-Version to 3.9.4 (no changes needed)
* Refresh for new upstream release
* Use Sébastien Villemot's @debian.org email address
* Remove obsolete DM-Upload-Allowed flag
* Add patch autoload-yes.patch
* Add copyright info for file lauchli.m (included in a Debian patch)
* Add patch partcnt-test-succeeds.patch
* Build-depends on octave-pkg-dev >= 1.0.3

[ Sébastien Villemot ]
* debian/control: fix versioned dependency on debhelper
* Add lintian override for false positive on hardening (fortify)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## Copyright (C) 2010 Fotios Kasolis <fotios.kasolis@gmail.com>
 
2
##
 
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
 
6
## version.
 
7
##
 
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
 
11
## details.
 
12
##
 
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/>.
 
15
 
 
16
## -*- texinfo -*-
 
17
## @deftypefn {Function File} {} publish (@var{filename})
 
18
## @deftypefnx {Function File} {} publish (@var{filename}, @var{options})
 
19
## Produces latex reports from scripts.
 
20
##
 
21
## @example
 
22
## publish (@var{my_script})
 
23
## @end example
 
24
##
 
25
## @noindent
 
26
## where the argument is a string that contains the file name of
 
27
## the script we want to report.
 
28
##
 
29
## If two arguments are given, they are interpreted as follows.
 
30
##
 
31
## @example
 
32
## publish (@var{filename}, [@var{option}, @var{value}, ...])
 
33
## @end example
 
34
##
 
35
## @noindent
 
36
## The following options are available:
 
37
##
 
38
## @itemize @bullet
 
39
## @item format
 
40
## 
 
41
## the only available format values are the strings `latex' and
 
42
## `html'.
 
43
##
 
44
## @item
 
45
## imageFormat:
 
46
##
 
47
## string that specifies the image format, valid formats are `pdf',
 
48
## `png', and `jpg'(or `jpeg').
 
49
##
 
50
## @item
 
51
## showCode:
 
52
##
 
53
## boolean value that specifies if the source code will be included
 
54
## in the report.
 
55
##
 
56
## @item
 
57
## evalCode:
 
58
##
 
59
## boolean value that specifies if execution results will be included
 
60
## in the report.
 
61
## 
 
62
## @end itemize
 
63
##
 
64
## Default @var{options}
 
65
##
 
66
## @itemize @bullet
 
67
## @item format = latex
 
68
##
 
69
## @item imageFormat = pdf
 
70
##
 
71
## @item showCode =  1
 
72
##
 
73
## @item evalCode =  1
 
74
##
 
75
## @end itemize
 
76
##
 
77
## Remarks
 
78
##
 
79
## @itemize @bullet
 
80
## @item Any additional non-valid field is removed without
 
81
## notification.
 
82
##
 
83
## @item To include several figures in the resulting report you must
 
84
## use figure with a unique number for each one of them.
 
85
##
 
86
## @item You do not have to save the figures manually, publish will
 
87
## do it for you.
 
88
##
 
89
## @item The functions works only for the current path and no way ...
 
90
## to specify other path is allowed.
 
91
##
 
92
## @end itemize
 
93
##
 
94
## Assume you have the script `myscript.m' which looks like
 
95
##
 
96
## @example
 
97
## @group
 
98
## x = 0:0.1:pi;
 
99
## y = sin(x)
 
100
## figure(1)
 
101
## plot(x,y);
 
102
## figure(2)
 
103
## plot(x,y.^2);
 
104
## @end group
 
105
## @end example
 
106
##
 
107
## You can then call publish with default @var{options}
 
108
## 
 
109
## @example
 
110
## publish("myscript")
 
111
## @end example
 
112
## @end deftypefn
 
113
 
 
114
function publish (file_name, varargin)
 
115
 
 
116
  if ((nargin < 1) || (rem (numel (varargin), 2) != 0))
 
117
    print_usage ();
 
118
  endif
 
119
 
 
120
  if (!strcmp (file_name(end-1:end), ".m"))
 
121
    ifile = strcat (file_name, ".m");
 
122
    ofile = strcat (file_name, ".tex");
 
123
  else
 
124
    ifile = file_name;
 
125
    ofile = strcat (file_name(1:end-1), "tex");
 
126
  endif
 
127
 
 
128
  if (exist (ifile, "file") != 2)
 
129
    error ("File %s does not exist.", ifile);
 
130
  endif
 
131
 
 
132
  options = set_default (struct ());
 
133
  options = read_options (varargin, "op1", "format imageFormat showCode evalCode", "default", options);
 
134
 
 
135
  if (strcmpi (options.format, "latex"))
 
136
    create_latex (ifile, ofile, options);
 
137
  elseif strcmpi(options.format, "html")
 
138
    create_html (ifile, options);
 
139
  endif
 
140
 
 
141
endfunction
 
142
 
 
143
function def_options = set_default (options);
 
144
 
 
145
  if (!isfield (options, "format"))
 
146
    def_options.format = "latex";
 
147
  elseif (!ischar (options.format))
 
148
    error("Option format must be a string.");
 
149
  else
 
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.");
 
154
    else
 
155
      def_options.format = options.format;
 
156
    endif
 
157
  endif
 
158
 
 
159
  if (! isfield (options, "imageFormat"))
 
160
    def_options.imageFormat = "pdf";
 
161
  elseif (! ischar(options.imageFormat))
 
162
    error("Option imageFormat must be a string.");
 
163
  else
 
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.");
 
168
    else
 
169
      def_options.imageFormat = options.imageFormat;
 
170
    endif
 
171
  endif
 
172
  
 
173
  if (!isfield (options,"showCode"))
 
174
    def_options.showCode = true;
 
175
  elseif (!isbool (options.showCode))
 
176
    error ("Option showCode must be a boolean.");
 
177
  else
 
178
    def_options.showCode = options.showCode;
 
179
  endif
 
180
 
 
181
  if (!isfield (options,"evalCode"))
 
182
    def_options.evalCode = true;
 
183
  elseif (!isbool (options.evalCode))
 
184
    error ("Option evalCode must be a boolean.");
 
185
  else
 
186
    def_options.evalCode = options.evalCode;
 
187
  endif
 
188
endfunction
 
189
 
 
190
function create_html (ifile, options)
 
191
 
 
192
  ofile = strcat (ifile(1:end-1), "html");
 
193
  html_start = "<html>\n<body>\n";
 
194
  html_end   = "</body>\n</html>\n";
 
195
 
 
196
  if options.showCode
 
197
    section1_title = "<h2>Source code</h2>\n";
 
198
    fid = fopen (ifile, "r");
 
199
    source_code = fread (fid, "*char")';
 
200
    fclose(fid);
 
201
  else
 
202
    section1_title = "";
 
203
    source_code    = "";
 
204
  endif
 
205
 
 
206
  if options.evalCode
 
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);
 
210
  else
 
211
    section2_title = "";
 
212
    oct_command    = "";
 
213
    script_result  = "";
 
214
  endif
 
215
 
 
216
  [section3_title, disp_fig] = exec_print (ifile, options);
 
217
 
 
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);
 
221
 
 
222
  
 
223
  fid = fopen (ofile, "w");
 
224
  fputs (fid, final_document);
 
225
  fclose (fid);
 
226
 
 
227
endfunction
 
228
 
 
229
function create_latex (ifile, ofile, options)
 
230
  latex_preamble = "\
 
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";
 
237
 
 
238
  listing_source_option = "\
 
239
\\lstset{\n\
 
240
language = Octave,\n\
 
241
basicstyle =\\footnotesize,\n\
 
242
numbers = left,\n\
 
243
numberstyle = \\footnotesize,\n\
 
244
backgroundcolor=\\color{lightgray},\n\
 
245
frame=single,\n\
 
246
tabsize=2,\n\
 
247
breaklines=true}\n";
 
248
 
 
249
  listing_exec_option = "\
 
250
\\lstset{\n\
 
251
language = Octave,\n\
 
252
basicstyle =\\footnotesize,\n\
 
253
numbers = none,\n\
 
254
backgroundcolor=\\color{white},\n\
 
255
frame=none,\n\
 
256
tabsize=2,\n\
 
257
breaklines=true}\n";
 
258
 
 
259
  if options.showCode
 
260
    section1_title = strcat ("\\section*{Source code: \\texttt{", ifile, "}}\n");
 
261
    source_code    = strcat ("\\lstinputlisting{", ifile, "}\n");
 
262
  else
 
263
    section1_title = "";
 
264
    source_code    = "";
 
265
  endif
 
266
  
 
267
  if options.evalCode
 
268
    section2_title = "\\section*{Execution results}\n";
 
269
    oct_command    = strcat ("octave> ", ifile(1:end-2), "\n");
 
270
    script_result = exec_script (ifile);
 
271
  else
 
272
    section2_title = "";
 
273
    oct_command    = "";
 
274
    script_result  = "";
 
275
  endif
 
276
 
 
277
  [section3_title, disp_fig] = exec_print (ifile, options);
 
278
 
 
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",
 
286
                           section3_title,
 
287
                           "\\begin{center}\n",
 
288
                           disp_fig,
 
289
                           "\\end{center}\n",
 
290
                           "\\end{document}");
 
291
 
 
292
  fid = fopen (ofile, "w");
 
293
  fputs(fid, final_document);
 
294
  fclose(fid);
 
295
endfunction
 
296
 
 
297
function script_result = exec_script (ifile)
 
298
  diary publish_tmp_script.txt
 
299
  eval (ifile(1:end-2))
 
300
  diary off
 
301
  fid = fopen ("publish_tmp_script.txt", 'r');
 
302
  script_result = fread (fid, '*char')';
 
303
  fclose(fid);
 
304
  delete ("publish_tmp_script.txt");
 
305
endfunction
 
306
 
 
307
function [section3_title, disp_fig] = exec_print (ifile, options)
 
308
  figures = findall (0, "type", "figure");
 
309
  section3_title = "";
 
310
  disp_fig       = "";
 
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");
 
324
      endif
 
325
    endfor
 
326
  endif
 
327
endfunction
 
328
 
 
329
# TODO
 
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)
 
334
#
 
335
#   * KNOWN PROBLEM: THE COMMENTING LINES IN HTML