Advertisement
Guest User

Custom Torque Markup Language Parser

a guest
Oct 6th, 2012
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.45 KB | None | 0 0
  1. // +--------------------------------------+
  2. // | Custom Torque Markup Language Parser |
  3. // |                                      |
  4. // | Author: Greek2me (11902)             |
  5. // +--------------------------------------+
  6. // | Allows for parsing of custom TML     |
  7. // | formatting.                          |
  8. // +--------------------------------------+
  9. // | USAGE:                               |
  10. // | -Create a function called            |
  11. // |  "customTMLParser_IDENTIFIER", where |
  12. // |  IDENTIFIER is a unique name for     |
  13. // |  your parser. See the function,      |
  14. // |  "customTMLParser_default", for an   |
  15. // |  example.                            |
  16. // |                                      |
  17. // | -Parse your text like this:          |
  18. // |  parseCustomTML(%str,%obj,%identi);  |
  19. // | -%str is the string, %obj is the     |
  20. // |  object, %identi is the identifier.  |
  21. // | -%obj is not required but is HIGHLY  |
  22. // |  recommended.                        |
  23. // |                                      |
  24. // | -You may use multiple identifiers by |
  25. // |  separating them by tabs. The string |
  26. // |  will be parsed according to all of  |
  27. // |  them.                               |
  28. // +--------------------------------------+
  29.  
  30. if($customTMLParser::version >= 1)
  31.     return;
  32. $customTMLParser::version = 1;
  33.  
  34. $customTMLParser::leftBracket = "<";
  35. $customTMLParser::rightBracket = ">";
  36. $customTMLParser::div = ":";
  37.  
  38. function customTMLParser_default(%obj,%value0,%value1,%value2,%value3,%value4,%value5,%value6,%value7,%value8,%value9,%value10,%value11,%value12,%value13,%value14,%value15)
  39. {
  40.     switch$(%value[0])
  41.     {
  42.         case "sPush":
  43.             %obj.TML_oldFontTypes = setField(%obj.TML_oldFontTypes,getFieldCount(%obj.TML_oldFontTypes),%obj.TML_fontType);
  44.             %obj.TML_oldFontSizes = setField(%obj.TML_oldFontSizes,getFieldCount(%obj.TML_oldFontSizes),%obj.TML_fontSize);
  45.  
  46.         case "sPop":
  47.             %obj.TML_fontType = getField(%obj.TML_oldFontTypes,getFieldCount(%obj.TML_oldFontTypes) - 1);
  48.             %obj.TML_fontSize = getField(%obj.TML_oldFontSizes,getFieldCount(%obj.TML_oldFontSizes) - 1);
  49.  
  50.             %obj.TML_oldFontTypes = removeField(%obj.TML_oldFontTypes,getFieldCount(%obj.TML_oldFontTypes) - 1);
  51.             %obj.TML_oldFontSizes = removeField(%obj.TML_oldFontSizes,getFieldCount(%obj.TML_oldFontSizes) - 1);
  52.  
  53.         case "b":
  54.             if(isObject(%obj) && striPos(%obj.TML_fontType,"bold") < 0)
  55.                 %replace = "<sPush><font:" @ %obj.TML_fontType SPC "bold:" @ %obj.TML_fontSize @ ">";
  56.             else
  57.                 %replace = "<sPush><font:arial bold:15>";
  58.  
  59.         case "/b":
  60.             %replace = "<sPop>";
  61.  
  62.         case "i":
  63.             if(isObject(%obj) && striPos(%obj.TML_fontType,"italic") < 0)
  64.                 %replace = "<sPush><font:" @ %obj.TML_fontType SPC "italic:" @ %obj.TML_fontSize @ ">";
  65.             else
  66.                 %replace = "<sPush><font:arial italic:15>";
  67.  
  68.         case "/i":
  69.             %replace = "<sPop>";
  70.  
  71.         case "font":
  72.             %obj.TML_fontType = %value[1];
  73.             %obj.TML_fontSize = %value[2];
  74.  
  75.         case "colorHex":
  76.             %replace = "<sPush><color:" @ %value[1] @ ">";
  77.  
  78.         case "/color":
  79.             %replace = "<sPop>";
  80.  
  81.         case "/just":
  82.             %replace = "<just:left>";
  83.  
  84.         case "h1":
  85.             %replace = "<sPush><font:arial bold:24>";
  86.  
  87.         case "/h1":
  88.             %replace = "<sPop><br>";
  89.  
  90.         case "h2":
  91.             %replace = "<sPush><font:arial bold:20>";
  92.  
  93.         case "/h2":
  94.             %replace = "<sPop><br>";
  95.  
  96.         case "h3":
  97.             %replace = "<sPush><font:arial bold:17>";
  98.  
  99.         case "/h3":
  100.             %replace = "<sPop><br>";
  101.  
  102.         case "bullet":
  103.             switch$(%value[1])
  104.             {
  105.                 case "bug":
  106.                     %replace = "<bitmap:" @ $Slayer::Client::Directory @ "/Images/Bullets/bullet_bug.png>\t";
  107.                    
  108.                 case "add":
  109.                     %replace = "<bitmap:" @ $Slayer::Client::Directory @ "/Images/Bullets/bullet_add.png>\t";
  110.  
  111.                 case "remove":
  112.                     %replace = "<bitmap:" @ $Slayer::Client::Directory @ "/Images/Bullets/bullet_delete.png>\t";
  113.  
  114.                 case "gamemode":
  115.                     %replace = "<bitmap:" @ $Slayer::Client::Directory @ "/Images/Bullets/bullet_controller.png>\t";
  116.  
  117.                 case "pref":
  118.                     %replace = "<bitmap:" @ $Slayer::Client::Directory @ "/Images/Bullets/bullet_cog.png>\t";
  119.  
  120.                 case "module":
  121.                     %replace = "<bitmap:" @ $Slayer::Client::Directory @ "/Images/Bullets/bullet_plugin.png>\t";
  122.  
  123.                 case "gui":
  124.                     %replace = "<bitmap:" @ $Slayer::Client::Directory @ "/Images/Bullets/bullet_application.png>\t";
  125.  
  126.                 default:
  127.                     %replace = "<bitmap:" @ $Slayer::Client::Directory @ "/Images/Bullets/bullet_black.png>\t";
  128.             }
  129.  
  130.         case "/bullet":
  131.             %replace = "<br><br>";
  132.  
  133.         case "*":
  134.             %replace = "<bullet>";
  135.  
  136.         case "/*":
  137.             %replace = "</bullet>";
  138.     }
  139.  
  140.     return %replace;
  141. }
  142.  
  143. function parseCustomTML(%string,%obj,%parserFunction)
  144. {
  145.     if(%parserFunction $= "")
  146.         %parserFunction = "default";
  147.  
  148.     if(isObject(%obj) && %obj.getClassName() $= "GuiMLTextCtrl")
  149.     {
  150.         %text = %obj.getText();
  151.         if(%text $= "" || %text $= %obj.text)
  152.         {
  153.             %obj.TML_fontType = %obj.profile.fontType;
  154.             %obj.TML_fontSize = %obj.profile.fontSize;
  155.  
  156.             %obj.TML_oldFontTypes = "";
  157.             %obj.TML_oldFontSizes = "";
  158.         }
  159.     }
  160.  
  161.     for(%i=0; %i < strLen(%string); %i++)
  162.     {
  163.         %char = getSubStr(%string,%i,1);
  164.  
  165.         if(%char $= $customTMLParser::leftBracket)
  166.             %start = %i;
  167.  
  168.         if(%char $= $customTMLParser::rightBracket && %start !$= "")
  169.         {
  170.             %end = %i;
  171.  
  172.             %full = getSubStr(%string,%start,%end-%start+1);
  173.             %contents = getSubStr(%full,1,strLen(%full) - 2);
  174.  
  175.             %search = %contents;
  176.             %pos = -1;
  177.             %numValues = 0;
  178.             while(strPos(%search,$customTMLParser::div) >= 0)
  179.             {
  180.                 %search = getSubStr(%search,%pos+1,strLen(%search));
  181.  
  182.                 %pos = strPos(%search,$customTMLParser::div);
  183.                 if(%pos >= 0)
  184.                 {
  185.                     %value[%numValues] = getSubStr(%search,0,%pos);
  186.                 }
  187.                 else
  188.                 {
  189.                     %value[%numValues] = %search;
  190.                 }
  191.                 %numValues ++;
  192.             }
  193.  
  194.             if(%numValues <= 0)
  195.             {
  196.                 if(%contents !$= "")
  197.                 {
  198.                     %value[0] = %contents;
  199.                     %numValues = 1;
  200.                 }
  201.             }
  202.  
  203.             for(%e = 0; %e < getFieldCount(%parserFunction); %e ++)
  204.             {
  205.                 %replace = call("customTMLParser_" @ getField(%parserFunction,%e),%obj,%value0,%value1,%value2,%value3,%value4,%value5,%value6,%value7,%value8,%value9,%value10,%value11,%value12,%value13,%value14,%value15);
  206.                 if(%replace !$= "")
  207.                 {
  208.                     %string = strReplace(%string,%full,%replace);
  209.                     %replaced = 1;
  210.                 }
  211.             }
  212.             if(%replaced)
  213.                 %i = %start-1;
  214.  
  215.             %start = "";
  216.             %end = "";
  217.             %full = "";
  218.             %contents = "";
  219.             %search = "";
  220.             %replace = "";
  221.             %replaced = "";
  222.             for(%e=0; %e < %numValues; %e++)
  223.                 %value[%e] = "";
  224.             %numValues = "";
  225.         }
  226.     }
  227.  
  228.     return %string;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement