## Copyright (C) 2016 Julien Bect
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see .
function ofadmin_update_latest_news (user)
INDEX = "index.html"; NEWS = "NEWS.html"; NB_ITEMS_IN_LATEST_NEWS = 8;
NEWS_START_TOKEN = "";
NEWS_END_TOKEN = "";
## Download index & news files from the server
ofadmin_remote_exec ("sftp", user, ...
sprintf ("get htdocs/%s", INDEX), sprintf ("get htdocs/%s", NEWS));
## Make a backup copy locally
fprintf ("Make a backup copy of %s locally...\n", INDEX);
if (copyfile (INDEX, [INDEX ".bak"]) ~= 1)
error (sprintf ("Failed to make local backup copy of %s", INDEX));
endif
# Extract all news items
fprintf ("Extract all new items...\n");
news_page = type (NEWS); news_page = news_page{1};
C = regexp (news_page, '(.*?\s*.*?)', 'tokens');
# Concatenate latest news
fprintf ("Concatenate latest news...\n");
latest_news = [];
for i = 1:NB_ITEMS_IN_LATEST_NEWS
latest_news = strcat (latest_news, C{i}{1});
endfor
# Update index page
fprintf ("Update %s (locally)...\n", INDEX);
index_page = type (INDEX); index_page = index_page{1};
s1 = [NEWS_START_TOKEN ".*" NEWS_END_TOKEN];
s2 = sprintf ("%s\n%s\n%s\n", NEWS_START_TOKEN, latest_news, NEWS_END_TOKEN);
index_page = regexprep (index_page, s1, s2);
FID = fopen (INDEX, "wt"); fputs (FID, index_page); fclose (FID);
# Upload updated file to server
fprintf ("Upload updated file to server...\n\n");
ofadmin_remote_exec ("sftp", user, ...
sprintf ("put %s htdocs/", INDEX), sprintf ("put %s.bak htdocs/", INDEX));
endfunction