source: prextra/prcontent2html.m @ 43

Last change on this file since 43 was 29, checked in by bduin, 13 years ago
File size: 6.6 KB
RevLine 
[14]1%PRCONTENT2HTML Construct HTML file from PRTools Contents file
2%
[29]3%               PRCONTENT2HTML(NAME,RECREATE,HTMLDIR)
[14]4
5
[29]6function out = prcontent2html(name,recreate,htmldir)
[14]7
[29]8if nargin < 3, htmldir = cd; end
9if nargin < 2 | isempty(recreate), recreate = 0; end
[14]10
11if exist([name '/Contents.m']) == 2
12        file = which([name '/Contents.m']);
13else
14        error('Contents file not found')
15end
16
17t = gethf(file); % help part
18
19t = strrep(t,char([10 13]),char(10));
20t = strrep(t,char([13 10]),char(10));
21t = strrep(t,char(13),char(10));
22t = strrep(t,char(9),char([32 32]));
23n = length(strfind(t,10));
24
25s = ['<html>' website_manual '<head><title>' name '</title></head>' newline];
26
27title = listn(t,1);
28version = listn(t,2);
29s = [s '<body bgcolor="#ffffff"><h3 align="center">' upper(name) '</h3>'];
30s = [s '<h3 align="center">' title ', ' version '</h3>'];
31for j=3:n                             % do for all lines
32        r1 = listn(t,j);
33        r2 = listn(t,j+1);
34        u = cleanstr(r1);
35        [tok,posttok] = strtok(u);
36        ntok = abs(tok);
37        single = isempty(strtok(posttok));
38        emptyline = isempty(tok);
39        if length(r1) > 2
40                skip = (strcmp(r1(1:3),'---'));
41        else
42                skip = 0;
43        end
44        if length(r2) > 2
45                section = strcmp(r2(1:3),'---');
46        else
47                section = 0;
48        end
49       
50        if emptyline                          % start new paragraph
51                s = [s '</p>' newline '<p>'];
52       
53        elseif section & ~skip
54                if j < 6 % first section
55                        s = [s '<h3>' u '</h3>' newline];
56                else  % following section, end old table first
57                        s = [s '</table>' newline '<h3>' u '</h3>' newline];
58                end
59                s = [s '<table cellspacing="0" cellpadding="0" width="100%"'...
60                                        ' align="center" border="0"'];       % start table
61        elseif ~skip
62               
63                file = which(tok);
64                pp = fileparts(file);
65                [pp2,subdir] = fileparts(pp);
[29]66                s = [s '<tr> <td width="100" valign="top">' commandrefs(tok,recreate,subdir,htmldir) '</td>'];
[14]67                s = [s '<td>' posttok '</td></tr>'];
68        end
69end
70s = [s '</table>'];
71s = [s '</p></body></html>'];
[29]72writf([fullfile(htmldir,name) '.html'],s);
[14]73if nargout > 0
74        out = s;
75end
76
77return
78
79function i = isupper(s)
80% true if s doesnot change by upper
81i = strcmp(s,upper(s));
82return
83
84function out = code(s,n)
85        %convert to code, add n spaces
86        if nargin < 2, n = 0; end
87        out = ['<font face="Courier">' htmlspace(n) s '</font>'];
88return
89
90function out = makecode(s,n)
91        %convert any consequetive set of uppers to lower and make them Courier
92        if strmatch(s(end),char(',','.'))
93                send = s(end); s(end) = [];
94        elseif length(s) > 1 & strmatch(s(end-1:end),char(', ','. '))
95                send = s(end-1:end); s(end-1:end) = [];
96        else
97                send = [];
98        end
99        if nargin < 2, n = 0; end
100        out = [];
101        if isupper(s)
102                %out = code(lower(s),n);
103                out = code(s,n);
104        else
105                while ~isempty(s)
106                        [t,s] = strtok(s);
107                        if isupper(t)
108                                %t = code(lower(t));
109                                t = code(t);
110                        end
111                        out = [out ' ' t];
112                end
113        end
114        out = [out send];
115return
116       
117function s = cleanstr(s)
118        V = char([9 10 11 12 13 32]);
119        for n=1:length(s)
120                if all(V~=s(n))
121                        break;
122                end
123        end
124        s = s(n:end);
125        for n=length(s):-1:1
126                if all(V~=s(n))
127                        break
128                end
129        end
130        s = s(1:n);
131return
132
[29]133function s = commandrefs(r,recreate,subdir,htmldir)
[14]134        if nargin < 2, recreate = 0; end
135        if regexp(r,'^ *$')
136                s = r;
137                return
138        end
139        s = [];
140        tok = lower(r);
141        htmlname = fullfile(subdir,[tok '.html']);
142        if recreate
143                tt = prhelp2html(tok,1,subdir);
144        end
145       
[29]146        if exist(fullfile(htmldir,htmlname),'file') == 2
[14]147                s = [s '<a href="' htmlname '">' tok '</a>, '];
148        elseif isempty(tok) | regexp(tok,'^ *')
149                ;
150        else
151                s = [s tok ', '];
152        end
153        s = strrep(s,'\','/'); % avoid  \ in html addresses
154       
155return
156
157
158function s = htmlspace(n)
159        if nargin < 1, n=1; end
160        if n > 0
161                s = repmat('&nbsp;',1,n);
162        else
163                s = [];
164        end
165return
166
167%LISTN List lines specified by their line number
168%
169% t = listn(r,n)
170% Get the lines in r given by the line numbers in n.
171function t = listn(r,n)
172k = [0,find(r==newline)];
173t = [];
174for j = n
175        if j < length(k)
176    t = [t,r(k(j)+1:k(j+1))];
177        end
178end
179return
180
181
182function s = gethf(file)
183%GETHF Create heading file from m-file
184%
185%       s = gethf(file)
186%
187% The heading of the given m-file consisting of all starting % lines
188% is isolated and returned in s.
189%
190%       gethf(dir)
191%
192% All headings of the files in dir are merged into a file named headings.doc.
193% Default dir is cd.
194%
195% Use mergeprt for restoring
196if nargin == 0
197        gethf(cd);
198end
199if isdir(file)
200        files = dir(file);
201        docfile = ['headings.doc'];
202        m = size(files,1);
203        for i = 1:m
204                if (~files(i,:).isdir) & (files(i,:).name(end-1:end) == '.m')
205                        name = files(i,:).name;
206                        s = gethf(name);
207                        s = ['##1' name(1:end-2) newline s newline '##2' newline];
208                        %disp(s)
209                        appendf(docfile,s);
210                end
211        end
212else
213        %disp(file)
214        [s,ns] = readf(file);
215        p = grep(s,'%');
216        if isempty(p) % no comments found
217                s = [];
218                return
219        end
220        p = [p p(end)+10];  % trick
221        I = find(p - [0,p(1:length(p)-1)] ~= 1);
222        if p(1) == 1 & isempty(I), I = length(p) + 1; end
223%       if length(I) > 0 & I(1) > 1
224        if length(I) > 0
225                if I(1) == 1
226                        I(1) = [];
227                end
228                n = I(1) - 1;
229                s = listn(s,p(1):n+p(1)-1);
230                s = strrep(s,[newline '%'],newline);
231                %s = strrep(s,[newline ' '],newline);
232                %s = strrep(s,[newline newline],'&*&');
233                %s = strrep(s,[newline '        '],'*&* ');
234                %s = strrep(s,newline,' ');
235                %s = strrep(s,'&*&',[newline newline]);
236                %s = strrep(s,'*&*',newline);
237                s = s(2:end);
238        else
239                s = '';
240        end
241end
242
243
244function [r,n] = readf(file,newline)
245%READF Readfile
246%
247% [r,n] = readf(file,newline)
248% Reads file into string r. The number of lines
249% is returned in n.
250if nargin < 2, newline = 13; end
251fid = fopen(deblank(file),'r');
252if fid < 0
253   error(['Cann''t open ' file])
254end
255r = fscanf(fid,'%c');
256fclose(fid);
257n = length(find(r==newline));
258if r(length(r)) ~= newline, n = n + 1; end
259return
260
261
262function writf(file,r)
263%WRITF Write file
264%
265% writf(file,r)
266% Write file from string r
267fid = fopen(file,'w');
268if fid < 0
269   error(['Cannot open file ' file])
270end
271fprintf(fid,'%c',r);
272fclose(fid);
273return
274
275
276
277function [k,z] = grep(r,s)
278%GREP Get line specific lines
279%
280% [k,n] = grep(r,s)
281% Get the numbers of all lines in the set of lines r
282% that contain s.
283% n is the total number of lines.
284n = [0,find(r==newline)];
285m = findstr(r,s);
286[i,j] = sort([n,m]);;
287q = [0,j(1:length(j)-1)]-j;
288k = j(find(q>0))-1;
289z = length(n)-1; % # of lines
290return
291
292
293function t = website_manual
294t = ['<table border="0" cellspacing="0" cellpadding="3" width="100%" '...
295           'align="center"><tr><td><a href="//prtools.org/" target="_top">website</a>' ...
296                 '</td><td><p align="right"><a '...
297     'href="http://prtools.org/manual" target="_top">manual</a></p></td></tr></table></p>'];
298return
Note: See TracBrowser for help on using the repository browser.