cpp-Demo for ReplaceAll / Convert Chars to HTML-Entity's

Ask questions about how to create a script or swap scripts with other users.
Post Reply
User avatar
Walti
Posts: 51
Joined: 06 Jul 2009 11:26
Location: Schweiz / Swiss / Swizerland
Contact:

cpp-Demo for ReplaceAll / Convert Chars to HTML-Entity's

Post by Walti »

Updated 2010-03-10

Some more Special chars added

Updated 2010-02-12

New in this version:
If part of the Document is selected then only replace inside the selection, else in the whole document.

Updated 2010-06-08
Code for 'ß' addet.

Code: Select all

/*=======================================================*/
/*==           C++ - Script for RJTextEd               ==*/
/*==---------------------------------------------------==*/
/*==         Replace chars with html-entity            ==*/
/*==---------------------------------------------------==*/
/*== Author   : Walti Zubler, www.zubleredv.ch         ==*/
/*== Version : 2010-02-12  Version  2.0                ==*/
/*==         : 2010-06-08  Version  2.1                ==*/
/*=======================================================*/

// Global variables

WideString s;

string char_table[50][2];        // Enlarge if more conversations than 50
int xchar = 0, entity = 1;       // Index to Array for better readabilty

void gen_table()
{
     int i = 0;

  // German specials
  char_table[i][xchar] = "ä";   char_table[i][entity] = "ä";  i++;
  char_table[i][xchar] = "ö";   char_table[i][entity] = "ö";  i++;
  char_table[i][xchar] = "ü";   char_table[i][entity] = "ü";  i++;
  char_table[i][xchar] = "Ä";   char_table[i][entity] = "Ä";  i++;
  char_table[i][xchar] = "Ö";   char_table[i][entity] = "Ö";  i++;
  char_table[i][xchar] = "Ãœ";   char_table[i][entity] = "Ü";  i++;
  char_table[i][xchar] = "ß";   char_table[i][entity] = "ß"; i++;

  // French specials
  char_table[i][xchar] = "è";   char_table[i][entity] = "è";  i++;
  char_table[i][xchar] = "é";   char_table[i][entity] = "é";  i++;
  char_table[i][xchar] = "à";   char_table[i][entity] = "à";  i++;
  char_table[i][xchar] = "á";   char_table[i][entity] = "á";  i++;
  char_table[i][xchar] = "â";   char_table[i][entity] = "â";   i++;
  char_table[i][xchar] = "ç";   char_table[i][entity] = "ç";  i++;

  // Money
  char_table[i][xchar] = "€";   char_table[i][entity] = "€";   i++;
  char_table[i][xchar] = "¢";   char_table[i][entity] = "¢";    i++;
  char_table[i][xchar] = "£";   char_table[i][entity] = "£";   i++;

  // Mathe
  char_table[i][xchar] = "²";   char_table[i][entity] = "²";    i++;
  char_table[i][xchar] = "±";   char_table[i][entity] = "±";  i++;
  char_table[i][xchar] = "½";   char_table[i][entity] = "½";  i++;
  char_table[i][xchar] = "¼";   char_table[i][entity] = "¼";  i++;

  // others
  char_table[i][xchar] = "©";   char_table[i][entity] = "©";    i++;
  char_table[i][xchar] = "®";   char_table[i][entity] = "®";     i++;
  char_table[i][xchar] = "â„¢";   char_table[i][entity] = "™";   i++;
  char_table[i][xchar] = "§";   char_table[i][entity] = "§";    i++;
  char_table[i][xchar] = "°";   char_table[i][entity] = "°";     i++;
  char_table[i][xchar] = "¶";   char_table[i][entity] = "¶";    i++;

  // Mark end of table
  char_table[i][xchar] = NULL;   char_table[i][entity] = NULL;
}

// Start-Code
{
    int i = 0;

  gen_table();
  s = Document.SelText;
  if (s == "")
    {
      Document.SelectAll;
      s = Document.SelText;
    }
  if (s > "")
    {
      while (char_table[i][xchar] != NULL)
        {
          s = ScriptUtils.WstringReplaceAll(s,char_table[i][xchar],char_table[i][entity]);
          i++;
        }
      Document.SelText = s;
    }
}
Last edited by Walti on 09 Jun 2010 00:34, edited 5 times in total.
ramon
Posts: 160
Joined: 10 Oct 2009 20:17

Post by ramon »

Walti,

Would it be possible to use this script only for selected text?
User avatar
Walti
Posts: 51
Joined: 06 Jul 2009 11:26
Location: Schweiz / Swiss / Swizerland
Contact:

Post by Walti »

ramon,

with next release its possible and i will rewrite the script then so that if there is selected text it will work on this an if not then over the whole text.

See heere:
http://www.rjsoftware.se/Forum/viewtopic.php?t=1180
its great :lol:
ramon
Posts: 160
Joined: 10 Oct 2009 20:17

Post by ramon »

Thank you :)
User avatar
Walti
Posts: 51
Joined: 06 Jul 2009 11:26
Location: Schweiz / Swiss / Swizerland
Contact:

Post by Walti »

Script now works vor selected text, the code is updatet in the first post here.
Walti
User avatar
Rickard Johansson
Site Admin
Posts: 6627
Joined: 19 Jul 2006 14:29

Post by Rickard Johansson »

I think you should avoid using Document.ReplaceAll in a loop. It updates the document every time it's executed and it may create a huge undo buffer. Maybe you could use something like this instead:

Code: Select all

// Start-Code
{
    int i = 0, x;

  gen_table();
  s = Document.SelText;
  if (s = "") 
  {
      Document.SelectAll;
      s = Document.SelText;
  }


  if (s > "")
    {
      while (char_table[i][xchar] != NULL)
        {
          s = ScriptUtils.WstringReplaceAll(s,char_table[i][xchar],char_table[i][entity]);
          i++;
        }
      Document.SelText = s;
    }
} 
User avatar
Walti
Posts: 51
Joined: 06 Jul 2009 11:26
Location: Schweiz / Swiss / Swizerland
Contact:

Post by Walti »

thx rickard for your advice, i have rewritten the code :)
User avatar
Walti
Posts: 51
Joined: 06 Jul 2009 11:26
Location: Schweiz / Swiss / Swizerland
Contact:

Post by Walti »

New Version with more Entity's

You can find the code in the first Entry in this topic.
Post Reply