0
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
1 function highlight(mfile,options,outfile)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
2 %HIGHLIGHT - Syntax Highlighting of Matlab M-files in HTML, LaTeX, RTF and XML.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
3 % HIGHLIGHT(MFILE) takes an M-file MFILE in input and writes on disk an HTML
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
4 % file with the same basename ('foo.m' => 'foo.html') adding colored syntax
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
5 % highlighting on comment, string and Matlab keyword elements.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
6 % HIGHLIGHT(MFILE,OPTIONS) with OPTIONS being string 'html', 'xhtml', 'tex',
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
7 % 'rtf' or 'xml', allows to choose the output format between HTML, XHTML, LaTeX
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
8 % RTF or XML. The same rule is used to determine the name of the output file.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
9 % HIGHLIGHT(MFILE,OPTIONS) allows to specify some options in a structure:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
10 % options.type - Output file type
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
11 % [ {'html'} | 'tex' | 'rtf' | 'xml' | 'xhtml']
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
12 % options.tabs - Replace '\t' in source code by n white space
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
13 % [ 0 ... {4} ... n]
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
14 % options.linenb - Display line number in front of each line [ 0 | {1} ]
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
15 % HIGHLIGHT(MFILE,OPTIONS,OUTFILE) allows to specify the name of the output
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
16 % file. OUTFILE can also be a file handle. In that case, no header will be
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
17 % written, only the highlighted Matlab code will be sent to the OUTFILE stream.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
18
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
19 % Output file can be customized (font style, font color, font size, ...):
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
20 % o HTML: use CSS (Cascading Style Sheets) to define 'comment', 'string',
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
21 % 'keyword', 'cont' and 'code' SPAN elements and 'mcode' PRE tag.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
22 % o LaTeX: packages 'alltt' and 'color' are required, you can modify colors
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
23 % in defining colors 'string', 'comment' and 'keyword' using command
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
24 % \definecolor{mycolor}{rgb}{a,b,c}
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
25 % o RTF: Colors are defined in the Color Table at the beginning of the
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
26 % document. See Rich Text Format Specifications for more details:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
27 % <http://msdn.microsoft.com/library/en-us/dnrtfspec/html/rtfspec.asp>
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
28 % o XML: you will find the DTD of the resulting XML file in matlab.dtd
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
29 % You can then use XSL to transform your XML file in what you want.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
30 % For example, mat2html.xsl transforms your XML file in HTML as it would
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
31 % be if you would have used highlight.m to to so.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
32 % On Matlab 6.5, the command is:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
33 % xslt highlight.xml mat2html.xsl highlight.html
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
34 % On Linux, using libxslt <http://xmlsoft.org/XSLT/>, the command is:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
35 % xsltproc -o highlight.html mat2html.xsl highlight.xml
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
36
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
37 % Copyright (C) 2003 Guillaume Flandin <Guillaume@artefact.tk>
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
38 % $Revision: 1.1 $Date: 2010/02/24 10:14:32 $
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
39
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
40 % This program is free software; you can redistribute it and/or
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
41 % modify it under the terms of the GNU General Public License
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
42 % as published by the Free Software Foundation; either version 2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
43 % of the License, or any later version.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
44 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
45 % This program is distributed in the hope that it will be useful,
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
46 % but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
47 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
48 % GNU General Public License for more details.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
49 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
50 % You should have received a copy of the GNU General Public License
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
51 % along with this program; if not, write to the Free Software
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
52 % Foundation Inc, 59 Temple Pl. - Suite 330, Boston, MA 02111-1307, USA.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
53
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
54 % Suggestions for improvement and fixes are always welcome, although no
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
55 % guarantee is made whether and when they will be implemented.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
56 % Send requests to Guillaume@artefact.tk
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
57
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
58 % TODO % Improve distinction between 'end' keyword and array subscript
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
59 % TODO % Smart indentation is *very* buggy (unusable => undocumented)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
60 % TODO % Handle wrap mode for long lines in LaTeX mode (and in HTML)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
61 % TODO % Improve the XSL transformer from XML to LaTeX
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
62
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
63 %- Set up options
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
64 error(nargchk(1,3,nargin));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
65
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
66 opt = struct('type', 'html', ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
67 'tabs', 4, ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
68 'linenb', 1, ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
69 'indent', 0);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
70 if nargin >= 2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
71 if isstruct(options)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
72 names = fieldnames(options);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
73 for i=1:length(names)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
74 opt = setfield(opt,names{i},getfield(options,names{i}));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
75 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
76 elseif ischar(options)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
77 opt.type = options;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
78 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
79 error('Bad input argument.');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
80 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
81 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
82 if strcmp(lower(opt.type),'latex'), opt.type = 'tex'; end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
83 if strcmp(lower(opt.type),'xml'), opt.linenb = 1; end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
84
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
85 %- If no output filename is provided, one is chosen according to mfile
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
86 if nargin < 3
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
87 [pathstr, name, ext] = fileparts(mfile);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
88 outfile = fullfile(pathstr, [name, '.' lower(opt.type)]);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
89 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
90
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
91 %- If an output filename is provided, a standard header is created
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
92 if ischar(outfile)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
93 outfid = fopen(outfile,'wt');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
94 if outfid == -1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
95 error(sprintf('Cannot open ',outfile));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
96 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
97 feval([lower(opt.type) '_file_start'],outfid,mfile);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
98 %- Otherwise a file handle is provided
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
99 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
100 outfid = outfile;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
101 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
102
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
103 %- Open the Matlab mfile to be highlighted
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
104 mfid = fopen(mfile,'rt');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
105 if mfid == -1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
106 error(sprintf('Cannot open ',mfile));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
107 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
108
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
109 %- Write the syntax highlighted mfile code in the output file
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
110 write_highlighted_code(mfid,outfid,opt)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
111
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
112 %- Close the Matlab mfile and potentially the output file
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
113 fclose(mfid);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
114 if ischar(outfile),
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
115 feval([lower(opt.type) '_file_end'],outfid);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
116 fclose(outfid);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
117 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
118
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
119 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
120 function write_highlighted_code(mfid,outfid,opt)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
121 matlabKeywords = getMatlabKeywords;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
122 mKeySort = getMatlabKeywordsSorted;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
123 out_format = feval([lower(opt.type) '_format']);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
124 strtok_delim = sprintf(' \t\n\r(){}[]<>+-*~!|\\@&/.,:;="''%');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
125
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
126 fprintf(outfid,out_format.pre_start);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
127 nbline = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
128 nbblanks = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
129 flagnextline = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
130 while 1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
131 tline = fgetl(mfid);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
132 if ~ischar(tline), break, end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
133 tline = feval([lower(opt.type) '_special_char'],horztab(tline,opt.tabs));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
134 %- Display the line number at each line
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
135 if opt.linenb
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
136 fprintf(outfid,out_format.nb_line,nbline);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
137 nbline = nbline + 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
138 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
139 %- Remove blanks at the beginning of the line
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
140 if opt.indent
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
141 tline = fliplr(deblank(fliplr(tline)));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
142 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
143 nbblanks = nbblanks + flagnextline;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
144 flagnextline = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
145 %- Split code into meaningful chunks
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
146 splitc = splitcode(tline);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
147 newline = '';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
148 for i=1:length(splitc)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
149 if isempty(splitc{i})
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
150 elseif splitc{i}(1) == ''''
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
151 newline = [newline sprintf(out_format.string,splitc{i})];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
152 elseif splitc{i}(1) == '%'
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
153 newline = [newline sprintf(out_format.comment,splitc{i})];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
154 elseif ~isempty(strmatch('...',splitc{i}))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
155 newline = [newline sprintf(out_format.cont,'...')];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
156 if ~isempty(splitc{i}(4:end))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
157 newline = [newline sprintf(out_format.comment,splitc{i}(4:end))];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
158 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
159 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
160 %- Look for Matlab keywords
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
161 r = splitc{i};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
162 stringcode = '';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
163 while 1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
164 [t,r,q] = strtok(r,strtok_delim);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
165 stringcode = [stringcode q];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
166 if isempty(t), break, end;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
167 isNextIncr = any(strcmp(t,mKeySort.nextincr));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
168 isNextIncr2 = any(strcmp(t,mKeySort.nextincr2));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
169 isCurrDecr = any(strcmp(t,mKeySort.currdecr));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
170 isNextDecr = any(strcmp(t,mKeySort.nextdecr));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
171 isOther = any(strcmp(t,mKeySort.other));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
172 if isNextDecr % if strcmp(t,'end')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
173 % 'end' is keyword or array subscript ?
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
174 rr = fliplr(deblank(fliplr(r)));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
175 icomma = strmatch(',',rr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
176 isemicolon = strmatch(';',rr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
177 if ~(isempty(rr) | ~isempty([icomma isemicolon]))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
178 isNextDecr = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
179 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
180 nbblanks = nbblanks - 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
181 flagnextline = flagnextline + 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
182 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
183 % TODO % false detection of a([end,1])
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
184 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
185 if isNextIncr, flagnextline = flagnextline + 1; end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
186 if isNextIncr2, flagnextline = flagnextline + 2; end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
187 if isNextDecr, flagnextline = flagnextline - 1; end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
188 if isCurrDecr, nbblanks = nbblanks - 1; end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
189 % if any(strcmp(t,matlabKeywords))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
190 if isNextIncr | isNextIncr2 |isCurrDecr | isNextDecr | isOther
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
191 if ~isempty(stringcode)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
192 newline = [newline sprintf(out_format.code,stringcode)];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
193 stringcode = '';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
194 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
195 newline = [newline sprintf(out_format.keyword,t)];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
196 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
197 stringcode = [stringcode t];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
198 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
199 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
200 if ~isempty(stringcode)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
201 newline = [newline sprintf(out_format.code,stringcode)];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
202 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
203 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
204 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
205 if ~opt.indent, nbblanks = 0; end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
206 %- Print the syntax highlighted line
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
207 fprintf(outfid,out_format.line,[blanks(nbblanks*opt.tabs) newline]);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
208 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
209 fprintf(outfid,out_format.pre_end);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
210
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
211 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
212 % HTML FORMAT %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
213 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
214 function html_file_start(fid,mfile)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
215 fprintf(fid,[ ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
216 '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
217 '\t"http://www.w3.org/TR/REC-html40/loose.dtd">\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
218 '<html>\n<head>\n<title>%s</title>\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
219 '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
220 '<meta name="generator" content="highlight.m © 2003 Guillaume Flandin">\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
221 '<style type="text/css">\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
222 ' .comment {color: #228B22;}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
223 ' .string {color: #B20000;}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
224 ' .keyword, .cont {color: #0000FF;}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
225 ' .cont {text-decoration: underline;}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
226 ' .code {color: #000000;}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
227 '</style>\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
228 '</head>\n<body>\n'],mfile);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
229
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
230 %-------------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
231 function html_file_end(fid)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
232 fprintf(fid,'\n</body>\n</html>');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
233
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
234 %-------------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
235 function format = html_format
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
236 format.string = '<span class="string">%s</span>';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
237 format.comment = '<span class="comment">%s</span>';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
238 format.code = '%s'; %'<span class="code">%s</span>';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
239 format.keyword = '<span class="keyword">%s</span>';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
240 format.cont = '<span class="cont">%s</span>';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
241 format.pre_start = '<pre class="mcode">';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
242 format.pre_end = '</pre>\n';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
243 format.nb_line = '%04d ';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
244 format.line = '%s\n';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
245
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
246 %-------------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
247 function str = html_special_char(str)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
248 %- See http://www.w3.org/TR/html4/charset.html#h-5.3.2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
249 str = strrep(str,'&','&');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
250 str = strrep(str,'<','<');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
251 str = strrep(str,'>','>');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
252 str = strrep(str,'"','"');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
253
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
254 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
255 % XHTML FORMAT %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
256 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
257 function xhtml_file_start(fid,mfile)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
258 fprintf(fid,[ ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
259 '<?xml version="1.0"?>\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
260 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
261 '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
262 '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
263 '<head>\n<title>%s</title>\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
264 '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
265 '<meta name="generator" content="highlight.m © 2003 Guillaume Flandin" />\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
266 '<style type="text/css">\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
267 ' .comment {color: #228B22;}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
268 ' .string {color: #B20000;}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
269 ' .keyword, .cont {color: #0000FF;}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
270 ' .cont {text-decoration: underline;}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
271 ' .code {color: #000000;}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
272 '</style>\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
273 '</head>\n<body>\n'],mfile);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
274
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
275 %-------------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
276 function xhtml_file_end(fid)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
277 fprintf(fid,'\n</body>\n</html>');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
278
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
279 %-------------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
280 function format = xhtml_format
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
281 format.string = '<span class="string">%s</span>';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
282 format.comment = '<span class="comment">%s</span>';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
283 format.code = '%s'; %'<span class="code">%s</span>';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
284 format.keyword = '<span class="keyword">%s</span>';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
285 format.cont = '<span class="cont">%s</span>';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
286 format.pre_start = '<pre class="mcode">';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
287 format.pre_end = '</pre>\n';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
288 format.nb_line = '%04d ';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
289 format.line = '%s\n';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
290
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
291 %-------------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
292 function str = xhtml_special_char(str)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
293 %- See http://www.w3.org/TR/html4/charset.html#h-5.3.2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
294 str = strrep(str,'&','&');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
295 str = strrep(str,'<','<');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
296 str = strrep(str,'>','>');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
297 str = strrep(str,'"','"');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
298
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
299
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
300 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
301 % XML FORMAT %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
302 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
303 function xml_file_start(fid,mfile)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
304 fprintf(fid,[ ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
305 '<?xml version="1.0" encoding="iso-8859-1" ?>\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
306 '<!DOCTYPE mfile SYSTEM "matlab.dtd">\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
307 '<mfile name="%s">\n'],mfile);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
308
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
309 %-------------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
310 function xml_file_end(fid)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
311 fprintf(fid,'</mfile>');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
312
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
313 %-------------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
314 function format = xml_format
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
315 format.string = '<string>%s</string>';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
316 format.comment = '<comment>%s</comment>';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
317 format.code = '%s'; %'<code>%s</code>';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
318 format.keyword = '<keyword>%s</keyword>';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
319 format.cont = '<cont>%s</cont>';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
320 format.pre_start = '';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
321 format.pre_end = '';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
322 format.nb_line = '<line nb="%04d ">';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
323 format.line = '%s</line>\n';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
324
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
325 %-------------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
326 function str = xml_special_char(str)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
327 %- See http://www.w3.org/TR/REC-xml#sec-predefined-ent
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
328 str = strrep(str,'&','&');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
329 str = strrep(str,'<','<');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
330 str = strrep(str,'>','>');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
331 str = strrep(str,'"','"');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
332 %str = strrep(str,'''',''');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
333
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
334 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
335 % LaTeX FORMAT %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
336 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
337 function tex_file_start(fid,mfile)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
338 fprintf(fid,['\\documentclass[a4paper,10pt]{article}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
339 ' \\usepackage{alltt}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
340 ' \\usepackage{color}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
341 ' \\usepackage{fullpage}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
342 ' \\definecolor{string}{rgb}{0.7,0.0,0.0}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
343 ' \\definecolor{comment}{rgb}{0.13,0.54,0.13}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
344 ' \\definecolor{keyword}{rgb}{0.0,0.0,1.0}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
345 ' \\title{%s}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
346 ' \\author{\\textsc{Matlab}, The Mathworks, Inc.}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
347 '\\begin{document}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
348 '\\maketitle\n'],mfile);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
349
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
350 %-------------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
351 function tex_file_end(fid)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
352 fprintf(fid,'\\end{document}');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
353
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
354 %-------------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
355 function format = tex_format
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
356 format.string = '\\textcolor{string}{%s}';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
357 format.comment = 'textcolor{comment}{%s}'; % '%' has been replaced by '\%'
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
358 format.code = '%s';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
359 format.keyword = '\\textcolor{keyword}{%s}';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
360 format.cont = '\\textcolor{keyword}{\\underline{%s}}';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
361 format.pre_start = '\\begin{alltt}\n';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
362 format.pre_end = '\\end{alltt}\n';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
363 format.nb_line = '%04d ';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
364 format.line = '%s\n';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
365
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
366 %-------------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
367 function str = tex_special_char(str)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
368 % Special characters: # $ % & ~ _ ^ \ { }
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
369 str = strrep(str,'\','\(\backslash\)'); % $\backslash$ or \textbackslash or \verb+\+
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
370 str = strrep(str,'#','\#');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
371 str = strrep(str,'$','\$');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
372 str = strrep(str,'%','\%');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
373 str = strrep(str,'&','\&');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
374 str = strrep(str,'_','\_');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
375 str = strrep(str,'{','\{');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
376 str = strrep(str,'}','\}');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
377 str = strrep(str,'^','\^{}');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
378 str = strrep(str,'~','\~{}'); % or \textasciitilde
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
379
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
380 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
381 % RTF FORMAT %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
382 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
383 function rtf_file_start(fid,mfile)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
384 fprintf(fid,['{\\rtf1\\ansi\n\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
385 '{\\fonttbl{\\f0\\fmodern\\fcharset0\\fprq1 Courier New;}}\n\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
386 '{\\colortbl;\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
387 '\\red0\\green0\\blue0;\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
388 '\\red0\\green0\\blue255;\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
389 '\\red33\\green138\\blue33;\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
390 '\\red178\\green0\\blue0;}\n\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
391 '{\\info{\\title %s}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
392 '{\\author HighLight.m Copyright 2003}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
393 '{\\creatim\\yr%s\\mo%s\\dy%s}' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
394 '{\\*\\manager Guillaume Flandin}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
395 '{\\*\\company Artefact.tk}\n' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
396 '{\\*\\hlinkbase http://www.madic.org/download/' ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
397 'matlab/highlight/}}\n\n'],mfile,...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
398 datestr(date,'yyyy'),datestr(date,'mm'),datestr(date,'dd'));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
399
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
400 %-------------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
401 function rtf_file_end(fid)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
402 fprintf(fid,'}');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
403
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
404 %-------------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
405 function format = rtf_format
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
406 format.string = '{\\cf4 %s}';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
407 format.comment = '{\\cf3 %s}';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
408 format.code = '{%s}';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
409 format.keyword = '{\\cf2 %s}';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
410 format.cont = '{\\cf2 %s}';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
411 format.pre_start = '{\\f0\\fs16{';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
412 format.pre_end = '}}';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
413 format.nb_line = '{%04d }';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
414 format.line = '%s\n\\par ';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
415
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
416 %-------------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
417 function str = rtf_special_char(str)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
418 str = strrep(str,'\','\\');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
419 str = strrep(str,'{','\{');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
420 str = strrep(str,'}','\}');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
421
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
422 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
423 % MATLAB KEYWORDS %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
424 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
425 function matlabKeywords = getMatlabKeywords
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
426 %matlabKeywords = iskeyword; % Matlab R13
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
427 matlabKeywords = {'break', 'case', 'catch', 'continue', 'elseif', 'else',...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
428 'end', 'for', 'function', 'global', 'if', 'otherwise', ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
429 'persistent', 'return', 'switch', 'try', 'while'};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
430
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
431 %-------------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
432 function mKeySort = getMatlabKeywordsSorted
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
433 mKeySort.nextincr = {'for', 'while', 'if', 'else', 'elseif', ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
434 'case', 'otherwise', 'try', 'catch'};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
435 mKeySort.nextincr2 = {'switch'};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
436 mKeySort.currdecr = {'else', 'elseif', 'case', 'otherwise', 'catch'};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
437 mKeySort.nextdecr = {'end'};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
438 mKeySort.other = {'break', 'continue', 'function', 'global', ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
439 'persistent', 'return'};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
440
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
441 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
442 % HANDLE TAB CHARACTER %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
443 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
444 function str = horztab(str,n)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
445 %- For browsers, the horizontal tab character is the smallest non-zero
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
446 %- number of spaces necessary to line characters up along tab stops that are
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
447 %- every 8 characters: behaviour obtained when n = 0.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
448 if n > 0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
449 str = strrep(str,sprintf('\t'),blanks(n));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
450 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
451
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
452 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
453 % MATLAB CODE PARSER %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
454 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
455 function splitc = splitcode(code)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
456 %Split a line of Matlab code in string, comment and other
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
457
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
458 %- Label quotes in {'transpose', 'beginstring', 'midstring', 'endstring'}
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
459 iquote = findstr(code,'''');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
460 quotetransp = [double('_''.)}]') ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
461 double('A'):double('Z') ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
462 double('0'):double('9') ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
463 double('a'):double('z')];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
464 flagstring = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
465 flagdoublequote = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
466 jquote = [];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
467 for i=1:length(iquote)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
468 if ~flagstring
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
469 if iquote(i) > 1 & any(quotetransp == double(code(iquote(i)-1)))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
470 % => 'transpose';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
471 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
472 % => 'beginstring';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
473 jquote(size(jquote,1)+1,:) = [iquote(i) length(code)];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
474 flagstring = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
475 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
476 else % if flagstring
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
477 if flagdoublequote | ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
478 (iquote(i) < length(code) & strcmp(code(iquote(i)+1),''''))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
479 % => 'midstring';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
480 flagdoublequote = ~flagdoublequote;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
481 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
482 % => 'endstring';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
483 jquote(size(jquote,1),2) = iquote(i);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
484 flagstring = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
485 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
486 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
487 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
488
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
489 %- Find if a portion of code is a comment
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
490 ipercent = findstr(code,'%');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
491 jpercent = [];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
492 for i=1:length(ipercent)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
493 if isempty(jquote) | ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
494 ~any((ipercent(i) > jquote(:,1)) & (ipercent(i) < jquote(:,2)))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
495 jpercent = [ipercent(i) length(code)];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
496 break;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
497 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
498 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
499
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
500 %- Find continuation punctuation '...'
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
501 icont = findstr(code,'...');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
502 for i=1:length(icont)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
503 if (isempty(jquote) | ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
504 ~any((icont(i) > jquote(:,1)) & (icont(i) < jquote(:,2)))) & ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
505 (isempty(jpercent) | ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
506 icont(i) < jpercent(1))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
507 jpercent = [icont(i) length(code)];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
508 break;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
509 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
510 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
511
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
512 %- Remove strings inside comments
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
513 if ~isempty(jpercent) & ~isempty(jquote)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
514 jquote(find(jquote(:,1) > jpercent(1)),:) = [];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
515 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
516
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
517 %- Split code in a cell array of strings
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
518 icode = [jquote ; jpercent];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
519 splitc = {};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
520 if isempty(icode)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
521 splitc{1} = code;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
522 elseif icode(1,1) > 1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
523 splitc{1} = code(1:icode(1,1)-1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
524 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
525 for i=1:size(icode,1)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
526 splitc{end+1} = code(icode(i,1):icode(i,2));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
527 if i < size(icode,1) & icode(i+1,1) > icode(i,2) + 1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
528 splitc{end+1} = code((icode(i,2)+1):(icode(i+1,1)-1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
529 elseif i == size(icode,1) & icode(i,2) < length(code)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
530 splitc{end+1} = code(icode(i,2)+1:end);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
531 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
532 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
533
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
534 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
535 % MODIFIED VERSION OF STRTOK %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
536 %===============================================================================
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
537 function [token, remainder, quotient] = strtok(string, delimiters)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
538 % Modified version of STRTOK to also return the quotient
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
539 % string = [quotient token remainder]
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
540 %STRTOK Find token in string.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
541 % STRTOK(S) returns the first token in the string S delimited
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
542 % by "white space". Any leading white space characters are ignored.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
543 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
544 % STRTOK(S,D) returns the first token delimited by one of the
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
545 % characters in D. Any leading delimiter characters are ignored.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
546 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
547 % [T,R] = STRTOK(...) also returns the remainder of the original
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
548 % string.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
549 % If the token is not found in S then R is an empty string and T
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
550 % is same as S.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
551 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
552 % Copyright 1984-2002 The MathWorks, Inc.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
553 % $Revision: 1.1 $ $Date: 2010/02/24 10:14:32 $
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
554
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
555 token = []; remainder = []; quotient = string;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
556
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
557 len = length(string);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
558 if len == 0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
559 return
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
560 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
561
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
562 if (nargin == 1)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
563 delimiters = [9:13 32]; % White space characters
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
564 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
565
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
566 i = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
567 while (any(string(i) == delimiters))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
568 i = i + 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
569 if (i > len), return, end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
570 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
571 start = i;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
572 while (~any(string(i) == delimiters))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
573 i = i + 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
574 if (i > len), break, end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
575 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
576 finish = i - 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
577
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
578 token = string(start:finish);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
579
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
580 if (nargout >= 2)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
581 remainder = string(finish + 1:length(string));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
582 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
583
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
584 if (nargout == 3 & start > 1)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
585 quotient = string(1:start-1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
586 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
587 quotient = [];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
588 end
|