Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. if ($tod::nextid $= "") {
  2. $tod::nextid = 0;
  3. }
  4.  
  5. function ____tod_escape(%input) {
  6. %length = strlen(%input);
  7.  
  8. for (%i = 0; %i < %length; %i++) {
  9. %char = getSubStr(%input, %i, 1);
  10.  
  11. // Not perfectly safe yet - doesn't escape the following:
  12. // ! @ $ % ( ) + { } [ ]
  13. switch$ (%char) {
  14. case "&": %output = %output @ "&";
  15. case "<": %output = %output @ "<";
  16. case ">": %output = %output @ ">";
  17. case "\"": %output = %output @ """;
  18. case "'": %output = %output @ "&apos;";
  19. case "`": %output = %output @ "&#96;";
  20. case " ": %output = %output @ "&nbsp;";
  21. case "=": %output = %output @ "&#61;";
  22.  
  23. default: %output = %output @ %char;
  24. }
  25. }
  26.  
  27. return %output;
  28. }
  29.  
  30. function tod::compile(%template) {
  31. %index = 0;
  32. %length = strlen(%template);
  33.  
  34. while (1) {
  35. %start = strpos(%template, "{{", %index);
  36.  
  37. if (%start == -1) {
  38. %start = %length;
  39. }
  40.  
  41. if (%start > %index) {
  42. %text = expandEscape(getSubStr(%template, %index, %start - %index));
  43. %code = %code @ "%_r=%_r@\"" @ %text @ "\";";
  44. }
  45.  
  46. if (%start >= %length) {
  47. break;
  48. }
  49.  
  50. %hair = "";
  51. %in_string = false;
  52. %end = -1;
  53.  
  54. for (%i = %start + 2; %i < %length; %i++) {
  55. if (%in_string) {
  56. %char = getSubStr(%template, %i, 1);
  57. %hair = %hair @ %char;
  58.  
  59. if (!%in_escape && %char $= "\"") {
  60. %in_string = false;
  61. } else {
  62. %in_escape = %char $= "\\";
  63. }
  64. } else {
  65. %char = getSubStr(%template, %i, 1);
  66.  
  67. if (%char $= "}" && getSubStr(%template, %i + 1, 1) $= "}") {
  68. %end = %i;
  69. break;
  70. }
  71.  
  72. if (%char $= "#") {
  73. %hair = %hair @ "%_d.";
  74. } else {
  75. if (%char $= "\"") {
  76. %in_string = true;
  77. }
  78.  
  79. %hair = %hair @ %char;
  80. }
  81. }
  82. }
  83.  
  84. if (%end == -1) {
  85. error("ERROR: Unclosed {{ at " @ %start);
  86. return "";
  87. }
  88.  
  89. %hair = trim(%hair);
  90.  
  91. if (%hair $= "") {
  92. error("ERROR: Empty {{ expression }} at " @ %start);
  93. return "";
  94. }
  95.  
  96. if (getSubStr(%hair, 0, 1) $= ":") {
  97. %fun = getSubStr(firstWord(%hair), 1, strlen(%hair));
  98. %arg = restWords(%hair);
  99.  
  100. switch$ (%fun) {
  101. case "ts": %code = %code @ %arg;
  102. case "raw": %code = %code @ "%_r=%_r@" @ %hair @ ";";
  103. case "end": %code = %code @ "}";
  104.  
  105. case "if": %code = %code @ "if(" @ %arg @ "){";
  106. case "for": %code = %code @ "for(" @ %arg @ "){";
  107. case "while": %code = %code @ "raw(" @ %arg @ "){";
  108.  
  109. default:
  110. error("ERROR: Unknown {{ :function }} at " @ %start);
  111. return "";
  112. }
  113. } else {
  114. %code = %code @ "%_r=%_r@____tod_escape(" @ %hair @ ");";
  115. }
  116.  
  117. %index = %end + 2;
  118. }
  119.  
  120. %name = "____tod_template_" @ $tod::nextid;
  121. eval("function " @ %name @ "(%_d){" @ %code @ "return %_r;}");
  122.  
  123. if (!isFunction(%name)) {
  124. error("ERROR: Failed to compile template");
  125. return "";
  126. }
  127.  
  128. $tod::nextid = ($tod::nextid + 1) | 0;
  129. return %name;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement