% publish a single subpage

% *** BEGIN CONFIG ***

root_dir_name = 'web_final';

% build-system slash
slash='\'; % Windows (non-escaped)

% web-server-system slash (escaped where relevant)
sslash='/'; % unix - works on Apache under windows though

% *** END CONFIG ***

str_nav='"includes/navigation.php"'; % navigation string in xsl file
parent = ['..' sslash];
nav = ['includes' sslash 'navigation.php'];
nav_course =[parent nav];
nav_page = [parent parent nav];

[~,filename]=fileparts(pwd);

% Handle exceptional page names
if strcmp(filename,'ii') || strcmp(filename,'ib')
    filename=['part' filename];
    nav_link=nav_course;
elseif strcmp(filename,root_dir_name)
    filename='index';
    nav_link=nav;
else
    nav_link=nav_page;
end

% override for further.m
% filename='further'

fprintf(['Publishing ' filename '.m\n']);
publish( [filename '.m'], struct('outputDir', pwd,'figureSnapMethod','getframe'));
copyfile([filename '.html'], [filename '.php']);
fid = fopen([filename '.html'], 'r');
fod = fopen([filename '.php'], 'w');
while ( ~feof(fid) )
    s = fgetl(fid);
    s = strrep(s, str_nav, ['"' nav_link '"']);
    fprintf(fod, '%s\n', s);
end
fclose(fid);
fclose(fod);
delete([filename '.html']);
close all force;
fprintf('Success!\n');