]> Creatis software - CreaPhase.git/blob - octave_packages/m/help/private/__strip_html_tags__.m
update packages
[CreaPhase.git] / octave_packages / m / help / private / __strip_html_tags__.m
1 ## Copyright (C) 2009-2012 Søren Hauberg
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {[@var{text}, @var{status}] =} __strip_html_tags__ (@var{html_text})
21 ## Undocumented internal function.
22 ## @end deftypefn
23
24 ## Remove HTML tags from text.  This is used as a simple HTML-to-text
25 ## function.
26
27 function [text, status] = __strip_html_tags__ (html_text)
28   start = find (html_text == "<");
29   stop  = find (html_text == ">");
30   if (length (start) == length (stop))
31     text = html_text;
32     for n = length(start):-1:1
33       text (start (n):stop (n)) = [];
34     endfor
35     text = strip_superfluous_endlines (text);
36     status = 0;
37   else
38     warning ("help: invalid HTML data -- raw HTML source follows...");
39     disp (html_text);
40     text = "";
41     status = 1;
42   endif
43 endfunction
44
45 ## This function removes end-lines (\n) that makes printing look bad
46 function text = strip_superfluous_endlines (text)
47   ## Find groups of end-lines
48   els = find (text == "\n");
49   dels = diff (els);
50   groups = [els(1), 1]; # list containing [start, length] of each group
51   for k = 1:length (dels)
52     if (dels (k) == 1)
53       groups (end, 2) ++;
54     else
55       groups (end+1, 1:2) = [els(k+1), 1];
56     endif
57   endfor
58
59   keep = true (size (text));
60
61   ## Remove end-lines in the beginning
62   if (groups (1, 1) == 1)
63     keep (1:groups (1, 2)) = false;
64   endif
65
66   ## Remove end-lines from the end
67   if (sum (groups (end, :)) - 1 == length (text))
68     keep (groups (end, 1):end) = false;
69   endif
70
71   ## Remove groups of end-lines with more than 3 end-lines next to each other
72   idx = find (groups (:, 2) >= 3);
73   for k = 1:length (idx)
74     start = groups (idx (k), 1);
75     stop = start + groups (idx (k), 2) - 1;
76     keep (start+2:stop) = false;
77   endfor
78
79   ## Actually remove the elements
80   text = text (keep);
81 endfunction