Link to home
Start Free TrialLog in
Avatar of seabear
seabear

asked on

Code needed to deal with MIME enriched text format

I am writing a Delphi email client and I want to deal with email messages where the content-type is text/enriched.

Text/enriched is a MIME standard relying on tags for formatting where the tags are like <bigger> </bigger>. They are not HTML tags.

Can someone please let me know where to find Delphi code to either convert from enriched text to HTML or to plain text without the enriched tags.

With thanks - Dan
ASKER CERTIFIED SOLUTION
Avatar of AvonWyss
AvonWyss
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of seabear
seabear

ASKER

Hi AvonWyss,
   Thanks for the URl, this gives C code for the conversion, does anyone know of Delphi code to do the same job (convert enriched text to html)

regards - Dan
I don't have any Delphi code at hand for this, but I believe that it should not be too hard (just a little time-consuming) to write this code.
I don't have any Delphi code at hand for this, but I believe that it should not be too hard (just a little time-consuming) to write this code.
Avatar of seabear

ASKER

AvonWyss is right, the Delphi code is not that hard, even took less time than I thought, I have just been putting the job off. Here is the code I have written and am now using to convert enriched text to HTML - Dan

function enrichedToHTML(erText : string) : string;
var
    i, j, maxLen : integer;
    ch, etag, htag, htmText : string;
begin
    maxLen := length(erText);
    i := 1;
    while i <= maxLen - 1 do
    begin
        ch := erText[i];
        if ch = #13 then  //new line
        begin
            htmText := htmText + '<br>';
            inc(i,2); //assume next char is #10
        end
        else if ch <> '<' then     //ordinary character
        begin
            htmText := htmText + ch;
            inc(i);
        end
        else
        begin
            j := i + 1;
            while (erText[j] <> '>') and (j < maxLen) do inc(j);
            hTag := '';
            eTag := copy(erText, i + 1, j - i - 1);

            if lowercase(etag) = 'bold' then
            begin
                htag := '<b>';
            end
            else if lowercase(etag) = '/bold' then
            begin
                htag := '</b>';
            end

            else if lowercase(etag) = 'italic' then
            begin
                htag := '<i>';
            end
            else if lowercase(etag) = '/italic' then
            begin
                htag := '</i>';
            end

            else if lowercase(etag) = 'center' then
            begin
                htag := '<i>';
            end
            else if lowercase(etag) = '/center' then
            begin
                htag := '</i>';
            end


            else if lowercase(etag) = 'color' then
            //assume an enriched tag like this
            //<color><param>FF00,0000,0000</param>
            //note RGB colors are in 4 digit hex, Windows uses 2 didgit hex 0-255
            //so I will truncate to this
            begin
                htag := '<font color="#'
                 + copy(erText,i + 15,2)
                 + copy(erText,i + 20,2)
                 + copy(erText,i + 25,2) + '">';
                 j := i + 35; //to end of </param>
            end
            else if lowercase(etag) = '/color' then
            begin
                htag := '</font>';
            end

            else if lowercase(etag) = 'bigger' then
            //may have <bigger> or <bigger><bigger> or <bigger><bigger><bigger>
            begin
                if lowercase(copy(erText,i+9,16)) = '<bigger><bigger>' then
                begin
                    htag := '<font size="+4">';
                    j := i + 23;
                end
                else if lowercase(copy(erText,i+9,8)) = '<bigger>' then
                begin
                    htag := '<font size="+3">';
                    j := i + 15;
                end
                else
                begin
                    htag := '<font size="+2">';
                    j := i + 7;
                end;
            end
            else if lowercase(etag) = 'smaller' then
            begin
                htag := '</font>';
            //may have <smaller> or <smaller><smaller>
            //or <smaller><smaller><smaller>
                if lowercase(copy(erText,i+9,16)) = '<smaller><smaller>' then
                begin
                    j := i + 26;
                end
                else if lowercase(copy(erText,i+9,8)) = '<bigger>' then
                begin
                    j := i + 17;
                end
                else
                begin
                    j := i + 8;
                end;
            end

            ;
            htmText := htmText + htag;
            i := j + 1;

        end;
    end;
    result := htmText;
end;
Avatar of seabear

ASKER

Hi AvonWyss,
     Thanks for pointing me in the right direction - Dan
Glad to help! Also, thank you (in the name of the community) for posting your code, so that it will appear in the PAQ if someone wants to see the solution.