Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text to HTLM 1

Status
Not open for further replies.

mayuso

Programmer
Jun 16, 2003
8
BZ
I had written a small function to return the HTML format of a string and would like to expand on it or use something better. I use the returned content of the function to send it in the body of my emails within my VFP9 app.

Here is my small function
Code:
FUNCTION formathtmlbody
PARAMETERS html_Body
ret_val = ;
'<!DOCTYPE html>'+chr(13)+;
'<html>'+chr(13)+;
'<body>'+chr(13)+;
'<p>' + html_Body + '</p>'+chr(13)+;
'</body>'+chr(13)+;
'</html>'
RETURN (ret_val)
ENDFUNC

I do not have any complex formatting to do but would like to send more than one paragraph. Let's say for example I have an edit text box for the user to enter some sentences, or paragraphs. I would then use the content of that text and call my function to format the content to HTML and then I call my email program to send the email as HTML.

Code:
My_Text_Entered = formathtmlbody("Sentence 1" + chr(13) + "Sentence 2" + chr(13) + "Sentence 3" + chr(13))
=MyEmailFunction(My_Text_Entered)

Currently my function will obviously return:
"Sentence 1. Sentence 2. Sentence 3."

However, I would like it to return each sentence in a separate paragraph as in :
"Sentence 1.
Sentence 2.
Sentence 3."

I can do some coding to look for the carriage returns and all to put them in a different paragraph, but this might be too tedious. I am sure there are better ways of doing this and would like your help or suggestions please.

Just a note. I do not need help on how to send emails using VFP because I am already doing so with no problems.

Many thanks,
Mike

 
HTML renderers (browsers) will disregard Chr(13). It's part of the design.

The line break character in HTML is the <BR> tag, and <P> is the paragraph mark.

You can either parse the text yourself or do something more elaborate where you send individual document elements to an object and have it compose and return the aggregate HTML.
 
To create HTML text, I strongly suggest using Text-Endtext.

I agree, but someone that just wants <html></html> wrapped around a block of text probably hasn't got there yet. [smile]
 
Maybe overkill for what you're trying to achieve, but maybe have the edit box entering HTML in the first place with an editor?

thread184-1752388

I like work. It fascinates me. I can sit and look at it for hours...
 
What I'd do here is STRTRAN every double CR or CRLF to </p><p> and every single CR/CRLF remaining after replacing the double ones with <BR>.

Code:
textstring=STRTRAN(textstring,chr(13)+chr(13),"</p><p>")
textstring=STRTRAN(textstring,chr(13),"<br>")
Short explanation: The places in text with a double chr(13) are ending one paragraph and beginning the next one, therefore </p><p> and not <p></p>
Together with the <p> and final </p> of your previous code you'll always have the correct pairing of <p> and </p>.

You'll get "Sentence1.<br>Sentence2.<br>Sentence3" in your example, though, no paragraphs, but that also puts them below each other. Paragaphs also add half an empty line between paragraphs, typically.

I talked of CRLF, as that is more usual in Windows Texts and means CHR(13)+CHR(10). You could simply stripe out all CHR(10) first: textstring=CHRTRAN(textstring,CHR(10),'). That step is not needed, if you prepare your texts with CHR(13) only, though.

Finally:
Code:
FUNCTION formathtmlbody
PARAMETERS html_Body

html_Body = STRTRAN(html_Body,chr(13)+chr(13),"</p><p>")
html_Body = STRTRAN(html_Body,chr(13),"<br>")

ret_val = ;
'<!DOCTYPE html>'+chr(13)+;
'<html>'+chr(13)+;
'<body>'+chr(13)+;
'<p>' + html_Body + '</p>'+chr(13)+;
'</body>'+chr(13)+;
'</html>'
RETURN (ret_val)
ENDFUNC

Bye, Olaf.
 
As a side note: There is a more general idea about such transformations of simpler text to HTML (the markUP language), which is called markDOWN, ie see
That Markdown definition also turns an empty line (cause by two CR) into a paragraph transition. Some of the syntax definitions there are not as easily turned into simple STRTRANs, eg the header markdowns, but you could also use the ideas for italic and bold text decorations by simply turning each first _ to <i> and each second _ to </i>. An error in the initial text would leave an open tag, but that shouldn't matter much, browsers and mail clients are forgiving about incomplete HTML and simply show that decoration all the way to the end of the text and so that becomes visible and is easily fixed.

I already used / for /italic/ instead of _, * for *bold*, and _ for _underlined_ text (though that text decoration should rather be avoided, as it's strongly associated to links).
 
Hi Olaf,

While waiting for better answers I continued working on this and I actually came up with a similar concept to what you posted. Your logic was way better using the STRTRAN function. I have decided to use your concept since it is much simpler. But allow me share what I came up with before reading your post:

Code:
FUNCTION formathtmlbody
PARAMETERS html_Body
Ret_Formathtmlbody = ""
HTMLString = ;
'<!DOCTYPE html>'+chr(13)+;
'<html>'+chr(13)+;
'<body>'+chr(13) + ;
'<p>'
this_length = LEN(ALLTRIM(html_Body))
Carriage_Return = CHR(13)
this_char = ""
FOR x = 1 TO this_length
	this_char = SUBSTR(html_Body,x,1)
	IF this_char  == Carriage_Return
		HTMLString = HTMLString + ;
				'<br>'+chr(13)
	ELSE
		HTMLString = HTMLString + this_char
	ENDIF
ENDFOR
HTMLString = HTMLString + this_char
HTMLString = HTMLString + chr(13) + ;
	'</p>'+chr(13)+;
	'</body>'+chr(13)+;
	'</html>'
Ret_Formathtmlbody = HTMLString
RETURN (Ret_Formathtmlbody)
ENDFUNC
 
Yes, that'll turn two chr(13) to <br><br> and that visually also looks like a paragraph, but semantically you'll only stay with one paragraph overall, as your html will only have the one <p>...</p> paragraph span.

It won't matter much.

Bye, Olaf.
 
For my purposes this is just fine because my html text is only used for the body of my emails.

Regards,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top