www.gusucode.com > mbcexpr 工具箱 matlab 源码程序 > mbcexpr/@cgexprgui/@EquationView/pEquation2HTML.m

    function ret = pEquation2HTML(obj,str)
%PEQUATION2HTML Converts a plain text equation into an HTML string.
%
%  Text inside brackets is shown in a different colour and a smaller size.

%  Copyright 2000-2011 The MathWorks, Inc. and Ford Global Technologies, Inc.


i = 5; % initial fontsize
c = { 'navy','gray','black' };

% First escape all ampersand, greater-than and less-than symbols with their
% HTML character entities. We have to escape the ampersands first, so that
% we don't then escape those inserted later.
str = strrep(str,'&','&');
str = strrep(str,'>','>');
str = strrep(str,'<','&lt;');

% Check for spare brackets enclosing entire expression
% We want to remove the outer brackets from an expression like
% (x + y), but not from an expression like (x + y) * (m + n).
if strcmp(str(1), '(') && strcmp(str(end), ')')
    remove = 1;
    depth = 0;
    % Count the nesting depth throughout the expression.
    for n=1:length(str)
        if strcmp(str(n), '(')
            depth = depth + 1;
        elseif strcmp(str(n), ')')
            depth = depth - 1;
        end
        if depth==0 && n<length(str)
            % If the nesting depth is zero anywhere in the middle
            % we need to keep the outer brackets.
            remove = 0;
            break;
        end
    end
    if remove==1
        str = str(2:end-1);
    end
end

ret = sprintf('<font size=%d color=black>', i);
if ~isempty(obj.ExpressionName)
    ret = [ret obj.ExpressionName ' = '];
end

while (1)
    inc = strfind(str,'(');
    dec = strfind(str,')');

    if isempty(inc)
        if isempty(dec)
            ret = [ret str];
            break
        end
        inc = Inf;
    end
    if isempty(dec)
        dec = Inf;
    end
    if inc(1)<dec(1)
        i = i - 1;
        k = i;
        if k<2
            k = 2;
        end
        ret = [ret str(1:inc(1)-1) sprintf('(<font size=%d color=%s>', k, c{mod(i,length(c))+1})];
        str = str(inc(1)+1:end);
    else
        i = i + 1;
        ret = [ret str(1:dec(1)-1) '</font>)'];
        str = str(dec(1)+1:end);
    end
end