[wxMSW[3.0.3] Convert plain text to html text

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Rudra
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 217
Joined: Fri Sep 13, 2013 2:59 pm

[wxMSW[3.0.3] Convert plain text to html text

Post by Rudra » Fri Aug 02, 2019 7:46 am

Hi

In my wx application, I am getting plain text from wxTextCtrl and loading to wxSimpleHtmlListBox. The text can contain new line and any html entity like & >, < etc. I am handling it manually (i.e Replacing _T('<') with _T("&lt;"), _T('\n') with _T("<br/>") and so on) when I find issue in displaying the text.

I want a way to do it automatically like we have wxURI which converts/encodes plain text to HTML URL. I tried loading URI but it comes as plain text.
"A\nB" displays as "A%0aB". It want it to be displayed in two line in the list.

Let me know if there is way where it automatically coverts/encodes to HTML string.

Please suggest.

Thanks
R.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2057
Joined: Sun Jan 03, 2010 5:45 pm

Re: [wxMSW[3.0.3] Convert plain text to html text

Post by PB » Fri Aug 02, 2019 8:54 am

I do not think wxWidgets provide such function.

The closest thing it has is probably this private code doing that (for XML), that should be easy to adapt for your needs:
https://github.com/wxWidgets/wxWidgets/ ... .cpp#L1016

But perhaps it would be easier to find another and perhaps better C++ code for this...

User avatar
doublemax
Moderator
Moderator
Posts: 14098
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: [wxMSW[3.0.3] Convert plain text to html text

Post by doublemax » Fri Aug 02, 2019 9:47 am

when I find issue in displaying the text.
What kind of issues?

I don't think there is much more to do than you already did. I use a similar code:

Code: Select all

wxString ConvertTextToHTML( const wxString &in )
{
  wxString out;
  out.reserve( in.length() * 4 );
 
  for( wxString::const_iterator p = in.begin(); p!=in.end(); p++ )
  {
    wxUniChar c = *p;

    if( c >= 128 )
    {
      out.Append( wxString::Format("&#%d", c.GetValue()) );
    }
    else if( c == '<' )
      out.Append( wxT("&lt;") );
    else if( c == '>' )
      out.Append( wxT("&gt;") );
    else if( c == '&' )
      out.Append( wxT("&amp;") );
    else
      out.Append( c );
  };

  out.Shrink();
  
  return out;
}
Use the source, Luke!

Rudra
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 217
Joined: Fri Sep 13, 2013 2:59 pm

Re: [wxMSW[3.0.3] Convert plain text to html text

Post by Rudra » Mon Aug 05, 2019 9:06 am

Thanks for the reply.
What kind of issues?
Initially, I was not handling '<', so it was not getting displayed in list if input string has it. e.g if input string is "a<c" then I was seeing only "a" in list.
I don't see any problem with '>' and '&'. It gets displayed okay. I have to replace the '<'' with "&lt;". Recently, I see problem with '\n', "A\nB" is displayed as "A B" in the list. I had to replace the '\n' with "<br/>". I find another one '\t'(Tab). "A\tB" is displayed as "A B".

I don't know which other chars can have issues, So, I wanted a way where it can be done automatically. As there is no such way, I need to know which other chars can create similar issue.

Thanks
R.

User avatar
T-Rex
Moderator
Moderator
Posts: 1182
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: [wxMSW[3.0.3] Convert plain text to html text

Post by T-Rex » Mon Aug 05, 2019 10:30 am

You probably could use libhtmlcxx and HTML::decode_entities function.

Post Reply