add `directory` option to report class

parent f896ac82
...@@ -203,18 +203,15 @@ classdef graph < handle ...@@ -203,18 +203,15 @@ classdef graph < handle
end end
o.seriesToUse = ''; o.seriesToUse = '';
o.data = ''; o.data = '';
if ~exist(o.graphDirName, 'file')
mkdir(o.graphDirName);
end
end end
end end
methods (Access = ?section, Hidden = true) methods (Access = ?section, Hidden = true)
o = addSeries(o, varargin) o = addSeries(o, varargin)
write(o, fid, pg, sec, row, col) write(o, fid, pg, sec, row, col, rep_dir)
end end
methods (Access = private) methods (Access = private)
% Methods defined in separate files % Methods defined in separate files
lastIndex = end(o, k, n) lastIndex = end(o, k, n)
graphName = writeGraphFile(o, pg, sec, row, col) graphName = writeGraphFile(o, pg, sec, row, col, rep_dir)
end end
end end
function write(o, fid, pg, sec, row, col) function write(o, fid, pg, sec, row, col, rep_dir)
%function write(o, fid, pg, sec, row, col) %function write(o, fid, pg, sec, row, col, rep_dir)
% Write a Graph object % Write a Graph object
% %
% INPUTS % INPUTS
% o [graph] graph object % o [graph] graph object
% fid [integer] file id % fid [integer] file id
% pg [integer] this page number % pg [integer] this page number
% sec [integer] this section number % sec [integer] this section number
% row [integer] this row number % row [integer] this row number
% col [integer] this col number % col [integer] this col number
% rep_dir [string] directory containing report.tex
% %
% OUTPUTS % OUTPUTS
% o [graph] graph object % o [graph] graph object
...@@ -34,6 +35,6 @@ function write(o, fid, pg, sec, row, col) ...@@ -34,6 +35,6 @@ function write(o, fid, pg, sec, row, col)
% along with Dynare. If not, see <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
assert(fid ~= -1); assert(fid ~= -1);
graphName = writeGraphFile(o, pg, sec, row, col); graphName = writeGraphFile(o, pg, sec, row, col, rep_dir);
fprintf(fid, '\\input{%s}', graphName); fprintf(fid, '\\input{%s}', graphName);
end end
\ No newline at end of file
function graphName = writeGraphFile(o, pg, sec, row, col) function graphName = writeGraphFile(o, pg, sec, row, col, rep_dir)
%function graphName = writeGraphFile(o, pg, sec, row, col) %function graphName = writeGraphFile(o, pg, sec, row, col, rep_dir)
% Write the tikz file that contains the graph % Write the tikz file that contains the graph
% %
% INPUTS % INPUTS
% o [graph] graph object % o [graph] graph object
% pg [integer] this page number % pg [integer] this page number
% sec [integer] this section number % sec [integer] this section number
% row [integer] this row number % row [integer] this row number
% col [integer] this col number % col [integer] this col number
% rep_dir [string] directory containing report.tex
% %
% OUTPUTS % OUTPUTS
% graphName [string] name of graph written % graphName [string] name of graph written
...@@ -38,13 +39,16 @@ if ne < 1 ...@@ -38,13 +39,16 @@ if ne < 1
return return
end end
if exist([rep_dir filesep o.graphDirName], 'file') ~= 7
mkdir([rep_dir filesep o.graphDirName])
end
if isempty(o.graphName) if isempty(o.graphName)
graphName = sprintf('%s/graph_pg%d_sec%d_row%d_col%d.tex', o.graphDirName, pg, sec, row, col); graphName = sprintf([o.graphDirName filesep 'graph_pg%d_sec%d_row%d_col%d.tex'], pg, sec, row, col);
else else
graphName = [o.graphDirName '/' o.graphName]; graphName = [o.graphDirName filesep o.graphName];
end end
[fid, msg] = fopen(graphName, 'w'); [fid, msg] = fopen([rep_dir filesep graphName], 'w');
if fid == -1 if fid == -1
error(['@graph.writeGraphFile: ' msg]); error(['@graph.writeGraphFile: ' msg]);
end end
......
...@@ -117,7 +117,7 @@ classdef page < handle ...@@ -117,7 +117,7 @@ classdef page < handle
o = addSection(o, varargin) o = addSection(o, varargin)
o = addVspace(o, varargin) o = addVspace(o, varargin)
o = addTable(o, varargin) o = addTable(o, varargin)
write(o, fid, pg) write(o, fid, pg, rep_dir)
lastIndex = end(o, k, n) lastIndex = end(o, k, n)
ns = numSections(p) ns = numSections(p)
end end
......
function write(o, fid, pg) function write(o, fid, pg, rep_dir)
%function write(o, fid, pg) %function write(o, fid, pg, rep_dir)
% Write a Page object % Write a Page object
% %
% INPUTS % INPUTS
% o [page] page object % o [page] page object
% fid [integer] file id % fid [integer] file id
% pg [integer] this page number % pg [integer] this page number
% rep_dir [string] directory containing report.tex
% %
% OUTPUTS % OUTPUTS
% o [page] page object % o [page] page object
...@@ -46,7 +47,11 @@ if ~isempty(o.latex) ...@@ -46,7 +47,11 @@ if ~isempty(o.latex)
if ~exist(o.pageDirName, 'dir') if ~exist(o.pageDirName, 'dir')
mkdir(o.pageDirName) mkdir(o.pageDirName)
end end
pagename = [o.pageDirName '/page_' num2str(pg) '.tex']; dir = [rep_dir filesep o.pageDirName];
if exist(dir, 'file') ~= 7
mkdir(dir)
end
pagename = [dir filesep 'page_' num2str(pg) '.tex'];
[fidp, msg] = fopen(pagename, 'w'); [fidp, msg] = fopen(pagename, 'w');
if fidp == -1 if fidp == -1
error(['@page.write: ' msg]); error(['@page.write: ' msg]);
...@@ -68,7 +73,7 @@ else ...@@ -68,7 +73,7 @@ else
end end
for i = 1:length(o.sections) for i = 1:length(o.sections)
o.sections{i}.write(fid, pg, i); o.sections{i}.write(fid, pg, i, rep_dir);
end end
fprintf(fid, '\\end{tabular}\n'); fprintf(fid, '\\end{tabular}\n');
end end
......
...@@ -13,7 +13,7 @@ function o = compile(o, varargin) ...@@ -13,7 +13,7 @@ function o = compile(o, varargin)
% SPECIAL REQUIREMENTS % SPECIAL REQUIREMENTS
% none % none
% Copyright (C) 2013-2017 Dynare Team % Copyright (C) 2013-2019 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
...@@ -57,7 +57,7 @@ assert(ischar(opts.compiler), '@report.compile: compiler file must be a string') ...@@ -57,7 +57,7 @@ assert(ischar(opts.compiler), '@report.compile: compiler file must be a string')
assert(islogical(opts.showReport), '@report.compile: showReport must be either true or false'); assert(islogical(opts.showReport), '@report.compile: showReport must be either true or false');
assert(islogical(opts.showOutput), '@report.compile: showOutput must be either true or false'); assert(islogical(opts.showOutput), '@report.compile: showOutput must be either true or false');
if ~exist(o.fileName, 'file') if exist([o.directory filesep o.fileName], 'file') ~= 2
o.write(); o.write();
end end
...@@ -90,6 +90,8 @@ if isempty(opts.compiler) ...@@ -90,6 +90,8 @@ if isempty(opts.compiler)
end end
end end
orig_dir = pwd;
cd(o.directory)
options = '-synctex=1 -halt-on-error'; options = '-synctex=1 -halt-on-error';
if opts.showOutput if opts.showOutput
if isoctave if isoctave
...@@ -104,6 +106,7 @@ end ...@@ -104,6 +106,7 @@ end
[~, rfn, ~] = fileparts(o.fileName); [~, rfn, ~] = fileparts(o.fileName);
if status ~= 0 if status ~= 0
cd(orig_dir)
error(['@report.compile: There was an error in compiling ' rfn '.pdf.' ... error(['@report.compile: There was an error in compiling ' rfn '.pdf.' ...
' ' opts.compiler ' returned the error code: ' num2str(status)]); ' ' opts.compiler ' returned the error code: ' num2str(status)]);
end end
...@@ -115,6 +118,7 @@ if o.showOutput || opts.showOutput ...@@ -115,6 +118,7 @@ if o.showOutput || opts.showOutput
disp(''); disp('');
end end
if opts.showReport && ~isoctave if opts.showReport && ~isoctave
open([pwd filesep rfn '.pdf']); open([rfn '.pdf']);
end end
cd(orig_dir)
end end
...@@ -21,6 +21,7 @@ classdef report < handle ...@@ -21,6 +21,7 @@ classdef report < handle
pages = {} pages = {}
end end
properties (SetAccess = private) properties (SetAccess = private)
directory = '.' % Directory in which to write/compile the report. Default: '.'
title = '' % Report Title. Default: none. title = '' % Report Title. Default: none.
orientation = 'portrait' % Paper orientation: Default: `portrait'. orientation = 'portrait' % Paper orientation: Default: `portrait'.
paper = 'a4' % Paper size. Default: `a4'. paper = 'a4' % Paper size. Default: `a4'.
...@@ -77,6 +78,7 @@ classdef report < handle ...@@ -77,6 +78,7 @@ classdef report < handle
end end
% Check options provided by user % Check options provided by user
assert(ischar(o.directory), '@report.report: directory must be a string');
assert(ischar(o.title), '@report.report: title must be a string'); assert(ischar(o.title), '@report.report: title must be a string');
assert(ischar(o.fileName), '@report.report: fileName must be a string'); assert(ischar(o.fileName), '@report.report: fileName must be a string');
assert(ischar(o.compiler), '@report.report: compiler file must be a string'); assert(ischar(o.compiler), '@report.report: compiler file must be a string');
......
...@@ -28,7 +28,10 @@ function o = write(o) ...@@ -28,7 +28,10 @@ function o = write(o)
% You should have received a copy of the GNU General Public License % You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
[fid, msg] = fopen(o.fileName, 'w'); if exist(o.directory, 'file') ~= 7
mkdir(o.directory)
end
[fid, msg] = fopen([o.directory filesep o.fileName], 'w');
if fid == -1 if fid == -1
error(['@report.write: ' msg]); error(['@report.write: ' msg]);
end end
...@@ -88,7 +91,7 @@ for i = 1:length(o.pages) ...@@ -88,7 +91,7 @@ for i = 1:length(o.pages)
if o.showOutput if o.showOutput
fprintf(1, 'Writing Page: %d\n', i); fprintf(1, 'Writing Page: %d\n', i);
end end
o.pages{i}.write(fid, i); o.pages{i}.write(fid, i, o.directory);
end end
fprintf(fid, '\\end{document}\n'); fprintf(fid, '\\end{document}\n');
......
...@@ -148,18 +148,15 @@ classdef report_table < handle ...@@ -148,18 +148,15 @@ classdef report_table < handle
end end
o.data = ''; o.data = '';
o.seriesToUse = ''; o.seriesToUse = '';
if ~exist(o.tableDirName, 'file')
mkdir(o.tableDirName);
end
end end
end end
methods (Access = ?section, Hidden = true) methods (Access = ?section, Hidden = true)
o = addData(o, varargin) o = addData(o, varargin)
o = addSeries(o, varargin) o = addSeries(o, varargin)
write(o, fid, pg, sec, row, col) write(o, fid, pg, sec, row, col, rep_dir)
end end
methods (Access = private) methods (Access = private)
o = writeTableFile(o, pg, sec, row, col) o = writeTableFile(o, pg, sec, row, col, rep_dir)
end end
end end
......
function write(o, fid, pg, sec, row, col) function write(o, fid, pg, sec, row, col, rep_dir)
%function write(o, fid, pg, sec, row, col) %function write(o, fid, pg, sec, row, col, rep_dir)
% Write a Table object % Write a Table object
% %
% INPUTS % INPUTS
% o [table] table object % o [table] table object
% fid [integer] file id % fid [integer] file id
% pg [integer] this page number % pg [integer] this page number
% sec [integer] this section number % sec [integer] this section number
% row [integer] this row number % row [integer] this row number
% col [integer] this col number % col [integer] this col number
% rep_dir [string] directory containing report.tex
% %
% OUTPUTS % OUTPUTS
% o [table] table object % o [table] table object
...@@ -34,6 +35,6 @@ function write(o, fid, pg, sec, row, col) ...@@ -34,6 +35,6 @@ function write(o, fid, pg, sec, row, col)
% along with Dynare. If not, see <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
assert(fid ~= -1); assert(fid ~= -1);
tableName = writeTableFile(o, pg, sec, row, col); tableName = writeTableFile(o, pg, sec, row, col, rep_dir);
fprintf(fid, '\\input{%s}', tableName); fprintf(fid, '\\input{%s}', tableName);
end end
function tableName = writeTableFile(o, pg, sec, row, col) function tableName = writeTableFile(o, pg, sec, row, col, rep_dir)
%function tableName = writeTableFile(o, pg, sec, row, col) %function tableName = writeTableFile(o, pg, sec, row, col, rep_dir)
% Write a Report_Table object % Write a Report_Table object
% %
% INPUTS % INPUTS
% o [report_table] report_table object % o [report_table] report_table object
% pg [integer] this page number % pg [integer] this page number
% sec [integer] this section number % sec [integer] this section number
% row [integer] this row number % row [integer] this row number
% col [integer] this col number % col [integer] this col number
% rep_dir [string] directory containing report.tex
% %
% OUTPUTS % OUTPUTS
% tableName [string] name of table written % tableName [string] name of table written
...@@ -39,13 +40,16 @@ if ne == 0 && ~is_data_table ...@@ -39,13 +40,16 @@ if ne == 0 && ~is_data_table
return return
end end
if exist([rep_dir filesep o.tableDirName], 'file') ~= 7
mkdir([rep_dir filesep o.tableDirName])
end
if isempty(o.tableName) if isempty(o.tableName)
tableName = sprintf('%s/table_pg%d_sec%d_row%d_col%d.tex', o.tableDirName, pg, sec, row, col); tableName = sprintf([o.tableDirName filesep 'table_pg%d_sec%d_row%d_col%d.tex'], pg, sec, row, col);
else else
tableName = [o.tableDirName '/' o.tableName]; tableName = [o.tableDirName filesep o.tableName];
end end
[fid, msg] = fopen(tableName, 'w'); [fid, msg] = fopen([rep_dir filesep tableName], 'w');
if fid == -1 if fid == -1
error(['@report_table.writeTableFile: ' msg]); error(['@report_table.writeTableFile: ' msg]);
end end
......
...@@ -81,6 +81,6 @@ classdef section < handle ...@@ -81,6 +81,6 @@ classdef section < handle
o = addVspace(o, varargin) o = addVspace(o, varargin)
lastIndex = end(o, k, n) lastIndex = end(o, k, n)
n = numElements(o) n = numElements(o)
write(o, fid, pg, sec) write(o, fid, pg, sec, rep_dir)
end end
end end
function write(o, fid, pg, sec) function write(o, fid, pg, sec, rep_dir)
%function write(o, fid, pg, sec) %function write(o, fid, pg, sec, rep_dir)
% Write Section object % Write Section object
% %
% INPUTS % INPUTS
...@@ -7,6 +7,7 @@ function write(o, fid, pg, sec) ...@@ -7,6 +7,7 @@ function write(o, fid, pg, sec)
% fid [integer] file id % fid [integer] file id
% pg [integer] this page number % pg [integer] this page number
% sec [integer] this section number % sec [integer] this section number
% rep_dir [string] directory containing report.tex
% %
% OUTPUTS % OUTPUTS
% o [section] section object % o [section] section object
...@@ -77,7 +78,7 @@ for i=1:ne ...@@ -77,7 +78,7 @@ for i=1:ne
if isa(o.elements{i}, 'paragraph') if isa(o.elements{i}, 'paragraph')
o.elements{i}.write(fid); o.elements{i}.write(fid);
else else
o.elements{i}.write(fid, pg, sec, row, col); o.elements{i}.write(fid, pg, sec, row, col, rep_dir);
end end
if col ~= o.cols if col ~= o.cols
fprintf(fid, ' & '); fprintf(fid, ' & ');
......
...@@ -34,7 +34,7 @@ longNames = {'Coca Cola', 'Kinder Bueno', 'Pizza', ... ...@@ -34,7 +34,7 @@ longNames = {'Coca Cola', 'Kinder Bueno', 'Pizza', ...
'Vegetarianism Is Good', 'OS X', 'Dothraki'}; 'Vegetarianism Is Good', 'OS X', 'Dothraki'};
%% Begin Report %% Begin Report
rep = report(); rep = report('directory', 'my/report/dir');
%% Page 1: GDP %% Page 1: GDP
...@@ -107,7 +107,7 @@ rep.addVspace('number', 2); ...@@ -107,7 +107,7 @@ rep.addVspace('number', 2);
% Table 2 % Table 2
rep.addTable('title', 'Output Gap', 'range', larange, ... rep.addTable('title', 'Output Gap', 'range', larange, ...
'vlineAfter', dates('2011y')); 'vlineAfter', dates('2011y'), 'tableDirName', 'page4table2');
db_a = db_a.tex_rename('Y_WORLD', 'World'); db_a = db_a.tex_rename('Y_WORLD', 'World');
rep.addSeries('data', db_a{'Y_WORLD'}); rep.addSeries('data', db_a{'Y_WORLD'});
delta = db_a{'Y_WORLD'}-dc_a{'Y_WORLD'}; delta = db_a{'Y_WORLD'}-dc_a{'Y_WORLD'};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment