error(number): converting occurrences remaining in all .sce .sci files
[scilab.git] / scilab / modules / helptools / macros / find_links.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2008 - INRIA
3 //
4 // Copyright (C) 2012 - 2016 - Scilab Enterprises
5 //
6 // This file is hereby licensed under the terms of the GNU GPL v2.0,
7 // pursuant to article 5.3.4 of the CeCILL v.2.1.
8 // This file was originally licensed under the terms of the CeCILL v2.1,
9 // and continues to be available under such terms.
10 // For more information, see the COPYING file which you should have received
11 // along with this program.
12
13 // =============================================================================================
14 // find_links
15 //
16 // Private function !!!
17 // =============================================================================================
18
19 function flag = find_links(filein,fileout)
20
21     // - return %t if a LINK were found
22     // - Adapt the dtd link
23     // - Adapt the date
24
25     [lhs,rhs]=argn(0);
26
27     flag=%f;
28
29     if rhs<>2 then
30         msg = _("%s: Wrong number of input arguments: %d expected.\n")
31         error(msprintf(msg, "find_links", 2))
32     end
33
34     sep=filesep();
35
36     txt=mgetl(filein);
37
38     //---------------------------------------------------------------------------------
39     // Gestion de la DTD
40     //---------------------------------------------------------------------------------
41
42     dtd_line = grep(convstr(txt,"u"),"<!DOCTYPE MAN SYSTEM");
43
44     if getos() == "Windows" then
45         txt(dtd_line) = "<!DOCTYPE MAN SYSTEM ""file://"+SCI+"\modules\helptools\help.dtd"">";
46     else
47         txt(dtd_line) = "<!DOCTYPE MAN SYSTEM """+SCI+"/modules/helptools/help.dtd"">";
48     end
49
50     //---------------------------------------------------------------------------------
51     // Gestion de la date (<DATE>$LastChangedDate: 2006-07-27 10:51:33 +0200 (jeu, 27 jui 2006) $</DATE>)
52     //---------------------------------------------------------------------------------
53
54     date_line =  grep(convstr(txt,"u"),"<DATE>");
55     start_date = strindex(txt(date_line(1)),"$LastChangedDate");
56
57     if start_date <> [] then
58         start_date = start_date + 18;
59         end_date = start_date + 10;
60         txt(date_line) = "    <DATE>"+part(txt(date_line),start_date:end_date)+"</DATE>";
61     end
62
63     //---------------------------------------------------------------------------------
64     // Gestion des liens
65     //---------------------------------------------------------------------------------
66
67     d=grep(txt,"<LINK>");
68
69     if d==[] then mputl(txt,fileout); return; end
70
71     for k=d
72         tt=txt(k);
73         l1=strindex(tt,"<LINK>");
74         l2=strindex(tt,"</LINK>");
75         nlink=size(l1,"*")
76         for i=1:nlink
77             name=part(tt,[l1(1)+6:l2(1)-1])
78             path=get_absolute_file_path(filein);
79             if length(name)<>0 then
80                 l=getlink(name,path,filein)
81             else
82                 l="unknown";
83             end
84             // jpc 17 sept 2003
85             l=strsubst(l,"//","/")
86             tt=part(tt,[1:l1(1)-1])+"<A href="""+l+"""><VERB>"+name+"</VERB></A>"+part(tt,[l2(1)+7:length(tt)])
87             l1=strindex(tt,"<LINK>")
88             l2=strindex(tt,"</LINK>")
89         end
90         txt(k)=tt
91     end
92
93     mputl(txt,fileout);
94     flag = %t;
95
96 endfunction
97
98
99 // =============================================================================================
100 // getlink
101 // =============================================================================================
102
103 function t=getlink(name,absolute_path,path)
104
105     global %helps;
106     global %helps_modules;
107     if %helps_modules == [] then
108         moduleslist = getmodules();
109         for i = 1:size(moduleslist,"*")
110             add_module_help_chapter(moduleslist(i));
111         end
112     end
113     %HELPS=[%helps_modules;%helps];
114
115     name=stripblanks(name)
116
117     if getos() == "Windows" then
118         sep="\";
119     else
120         sep="/";
121     end
122
123     man_found = [];
124
125     //---------------------------------------------------------------------------------
126     // On commmence par chercher dans le répertoire "name" ( cas le plus fréquent ).
127     //---------------------------------------------------------------------------------
128
129     if fileinfo(absolute_path+".list_htm") <> [] then
130
131         whatis=mgetl(absolute_path+".list_htm");
132         // 1er test ( avec le contenu de la balise title )
133         f = grep(whatis,"- "+name+"==>");
134         if f<>[] then
135             for k1=f
136                 w = whatis(k1);
137                 w = strsubst(w,"- "+name+"==>","");
138                 man_found = absolute_path + w;
139             end
140         else
141             // 2nd test ( avec le nom du fichier )
142             f = grep(whatis,"==>"+name+".htm");
143             if f<>[] then
144                 for k1=f
145                     w = whatis(k1);
146                     w = strsubst(w,"- "+name+"==>","");
147                     man_found = absolute_path + name + ".htm";
148                 end
149             end
150         end
151
152     end
153
154     //---------------------------------------------------------------------------------
155     // On recherche maintenant dans les répertoires désignés dans %helps
156     //---------------------------------------------------------------------------------
157
158     if man_found == [] then
159
160         for k=1:size(%HELPS,1)
161
162             current_help_path = %HELPS(k,1)+sep;
163
164             if fileinfo(current_help_path+".list_htm") <> [] then
165
166                 whatis=mgetl(current_help_path+".list_htm");
167                 // 1er test ( avec le contenu de la balise title )
168                 f = grep(whatis,"- "+name+"==>");
169                 if f<>[] then
170                     for k1=f
171                         w = whatis(k1);
172                         w = strsubst(w,"- "+name+"==>","");
173                         man_found = current_help_path + w;
174                     end
175                 else
176                     // 2nd test ( avec le nom du fichier )
177                     f = grep(whatis,"==>"+name+".htm");
178                     if f<>[] then
179                         for k1=f
180                             w = whatis(k1);
181                             w = strsubst(w,"- "+name+"==>","");
182                             man_found = current_help_path + name + ".htm";
183                         end
184                     end
185                 end
186
187                 if man_found<>[] then break; end
188
189             else
190
191                 if fileinfo(%HELPS(k,1)+sep+"whatis.htm") <> [] then
192                     whatis = mgetl(%HELPS(k,1)+sep+"whatis.htm");
193                     f      = grep(whatis,name);
194                 else
195                     f      = [];
196                 end
197
198                 if f<>[] then
199                     for k1=f
200                         w=whatis(k1)
201                         i=strindex(w,">"); j=strindex(w,"</A>")
202                         if j<>[] then
203                             lname=part(w,i(2)+1:j-1)
204                             lnames=getwords(lname)
205                             // transforms "toto titi tata" into ["toto" "titi" "tata"]
206                             for ii=lnames
207                                 ok=%F
208                                 if ii==name then
209                                     i=strindex(w,"HREF="""); j=strindex(w,""">")
210                                     man_found=current_help_path+part(w,[i+6:j-1])
211                                 end
212                                 if man_found<>[] then break; end
213                             end
214                             if man_found<>[] then break; end
215                         end
216                     end
217                 end
218
219                 if man_found<>[] then break; end
220
221             end // if fileinfo ....
222
223         end // for k=1:size(%HELPS,1)
224
225     end // if  man_found == []
226
227     if man_found == [] then
228         write(%io(2),"Bad LINK """+name+""" in file "+path);
229         t=[];
230         return;
231     end
232
233     t=getrelativefilename(absolute_path,man_found);
234     t=strsubst(t,"\","/");
235
236 endfunction
237
238 // =============================================================================================
239 // getwords
240 // =============================================================================================
241
242 function vnames=getwords(names)
243     v=strindex(names," ")
244     if v==[] then
245         vnames=[names]
246     else
247         vnames=[]; i=1
248         for j=v
249             vnames=[vnames,part(names,i:j-1)]
250             i=j+1
251         end
252         vnames=[vnames,part(names,i:length(names))]
253     end
254 endfunction