1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| % Lecture du fichier ligne par ligne
X = textread('test.txt','%s','delimiter','\n');
% Recherche des indices des lignes vides
idx = find(cellfun('isempty',X));
% On modifie chaque ligne precedant ou suivant une ligne vide en ajoutant
% soit <p> soit </p>
for n=1:numel(idx)
X{idx(n)-1} = sprintf('%s</p>',X{idx(n)-1});
X{idx(n)+1} = sprintf('<p>%s',X{idx(n)+1});
end
% Cas particulier du premier titre
X{1} = ['<h1>' X{1}];
% Cas particulier du dernier paragraphe
X{end} = [X{end} '</p>'];
% Recherche a nouveau des indices des lignes vides
idx = [1 ; cellfun('isempty',X)];
% On detecte les lignes encadrees par deux lignes vides
idx = strfind(idx.',[1 0 1]);
% On modifie les lignes correspondante aux titres
for n=1:numel(idx)
X{idx(n)} = strrep(X{idx(n)},'<p>','<h1>');
X{idx(n)} = strrep(X{idx(n)},'</p>','</h1>');
end
% Ecriture du fichier final HTML en mode binaire (plus rapide)
fid = fopen('test.html','w');
fwrite(fid,'<html><head></head><body>');
fwrite(fid,[X{:}]);
fwrite(fid,'</body></html>');
fclose(fid);
% Verification du resultat
web test.html -browser |
Partager