Unicode and HTML for Fields

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Unicode and HTML for Fields

Post by dave_probertGA6e24 » Sat Oct 20, 2012 8:29 am

Hi All,

I have various simple html files translated into asian languages (e.g. Thai) that contain html tags to provide bold, font colours, etc. I would like to be able to load these into a normal Field object on a card. I can get the utf8 text to display fine, but the html tags show (using unicodetext property), or I can show gobbledigook text with bold/colours/etc (using htmltext property).

Is there a way to have the best of both worlds like this? Or am I stuck with messing around with a browser view or something? Has anyone else done anything similar with success?

For further information the html files have extra tags in them that I pre-parse to let the program know things like pictures to use (and where they should appear) and also to tell the program to split the text. These are easy to get via basic text location functions, etc. So I'm not too interested in using RTF formatting (as I don't know how to split that or insert data into the text-stream)

Any ideas would be great.
Cheers,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Unicode and HTML for Fields

Post by Simon » Sat Oct 20, 2012 4:32 pm

Hi Dave,
Not sure but when I use htmlText I get:
input 才出吃
output <p><font face="Heiti SC Light" size="11">&#25165;&#20986;&#21507;</font></p>
and the reverse works, no gobbelygook.

How does your html look?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Unicode and HTML for Fields

Post by dave_probertGA6e24 » Mon Oct 22, 2012 9:45 am

Hi.

Sorry for not replying earlier - had a Modem crash!

If I have something like:

<b>才出吃</b>

in my html/text file then I get the same appearing in the field for unicodetext (i.e. - characters are there along with the <b> tags), but I would get a proper bold text, but it is not readable for htmltext.

What I'm wondering is is there a way to convert the raw utf8 to the &#25165; style on the fly - so as to preserve the original (editable) text, but to then be able to display it in an htmltext field?

At this point in time the translators are simply writing the translations using a text editor - and dropping the basic tags in the appropriate places. They have no knowledge of the numerical codes for the characters they are typing as such.

Cheers,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Unicode and HTML for Fields

Post by Simon » Mon Oct 22, 2012 4:24 pm

As you say they add simple tags I think one way of doing it is:
put the utf into fld1 you get:
<b>才出吃</b>
put the htmlText fld1 into fld2:
<p><b>&#25165;&#20986;&#21507;</b></p>
then
replace "<" with "<" in fld2
replace ">" with ">" in fld2
result <p><b>&#25165;&#20986;&#21507;</b></p>
and then set the htmlText of a fld2 to the output fld.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Unicode and HTML for Fields

Post by dave_probertGA6e24 » Mon Oct 22, 2012 7:02 pm

Simon - that was Perfect - exactly what I needed, and so simple too.

The only thing I added was a replace """ with quote - to handle some <font color="red"> bits.

Code: Select all

      // tdat contains html tags and raw utf8 data
      set the unicodetext of field "textField" to uniEncode(tdat,"utf8")
      put the htmltext of field "textField" into tagged
      replace "<" with "<" in tagged
      replace ">" with ">" in tagged
      replace """ with quote in tagged
      set the htmltext of field "textfield" to tagged
I really hope that this can help others too, who may be having difficulty with utf8/unicode/html.

Thanks again.

Cheers,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Unicode and HTML for Fields

Post by Simon » Tue Oct 23, 2012 2:05 am

Great, I'm glad it worked for you.
A couple of weeks ago I went through double byte terror so it was all still fresh in my mind.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Post Reply