[795] | 1 | /****************************************************************** |
---|
| 2 | highlight.js Muze Helene |
---|
| 3 | ------------------------------------------------------------------ |
---|
| 4 | Author: Muze (info@muze.nl) |
---|
| 5 | Date: 28 februari 2004 |
---|
| 6 | |
---|
| 7 | Copyright 2002 Muze |
---|
| 8 | |
---|
| 9 | This file is part of Helene. |
---|
| 10 | |
---|
| 11 | Helene is free software; you can redistribute it and/or modify |
---|
| 12 | it under the terms of the GNU General Public License as published |
---|
| 13 | by the Free Software Foundation; either version 2 of the License, |
---|
| 14 | or (at your option) any later version. |
---|
| 15 | |
---|
| 16 | Helene is distributed in the hope that it will be useful, |
---|
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 19 | GNU General Public License for more details. |
---|
| 20 | |
---|
| 21 | You should have received a copy of the GNU General Public License |
---|
| 22 | along with Helene; if not, write to the Free Software |
---|
| 23 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
| 24 | 02111-1307 USA |
---|
| 25 | |
---|
| 26 | ------------------------------------------------------------------- |
---|
| 27 | |
---|
| 28 | This file contains the syntax highlighting parser |
---|
| 29 | |
---|
| 30 | ******************************************************************/ |
---|
| 31 | // Parsing smarty tags http://smarty.php.net/ |
---|
| 32 | // Cofigure the smarty delimiters below ('{%' and '%}' by default) |
---|
| 33 | // Unset this flag if you need to parse only PHP |
---|
| 34 | var parseSmarty = true; |
---|
| 35 | |
---|
| 36 | /* states */ |
---|
| 37 | var YY_STATE_HTML = 0; |
---|
| 38 | var YY_STATE_PINP = 1; |
---|
| 39 | var YY_STATE_DQSTRING = 2; |
---|
| 40 | var YY_STATE_SQSTRING = 3; |
---|
| 41 | var YY_STATE_SCRIPT = 4; |
---|
| 42 | var YY_STATE_BLOCKCOMMENT = 5; |
---|
| 43 | |
---|
| 44 | /* Smarty states */ |
---|
| 45 | var YYS_STATE_TAG = 101; |
---|
| 46 | var YYS_STATE_PARAM = 102; |
---|
| 47 | var YYS_STATE_QPARAM = 103; |
---|
| 48 | |
---|
| 49 | /* tokens */ |
---|
| 50 | var T_VAR = 0; |
---|
| 51 | var T_IDENT = 1; |
---|
| 52 | var T_FUNCTION = 2; |
---|
| 53 | var T_TOKEN = 3; |
---|
| 54 | var T_UNKNOWN = 4; |
---|
| 55 | var T_PINP_START = 5; |
---|
| 56 | var T_PINP_BLOCK = 6; |
---|
| 57 | var T_PINP_END = 7; |
---|
| 58 | var T_SPACE = 8; |
---|
| 59 | var T_DQUOTE = 9; |
---|
| 60 | var T_SQUOTE = 10; |
---|
| 61 | var T_ESCAPE = 11; |
---|
| 62 | var T_SPECIAL_CHAR = 12; |
---|
| 63 | var T_OPERATOR = 13; |
---|
| 64 | var T_SINGLE_COMMENTS = 14; |
---|
| 65 | var T_BLOCKCOMMENT = 15; |
---|
| 66 | var T_BLOCKCOMMENT_END = 16; |
---|
| 67 | var T_PHP_START = 17; |
---|
| 68 | var T_PHP_END = 18; |
---|
| 69 | var T_SCRIPT_START = 19; |
---|
| 70 | var T_SCRIPT_END = 20; |
---|
| 71 | var T_TAB = 21; |
---|
| 72 | var T_EMPTY_LINE = 22; |
---|
| 73 | |
---|
| 74 | /*Smarty tokens*/ |
---|
| 75 | var TS_SMARTY_START = 101; |
---|
| 76 | var TS_SMARTY_END = 102; |
---|
| 77 | var TS_KEYWORD = 103; |
---|
| 78 | var TS_ATTRIBUTE = 104; |
---|
| 79 | var TS_ATTRVALUE = 105; |
---|
| 80 | var TS_VAR = 106; |
---|
| 81 | var TS_ENDTAG = 107; |
---|
| 82 | |
---|
| 83 | var hLines = new Array(); |
---|
| 84 | var debug = 0; |
---|
| 85 | var scannerPos = 0; |
---|
| 86 | var hStyles = new Array(); |
---|
| 87 | var hStateStyles = new Array(); |
---|
| 88 | |
---|
| 89 | // May be there is the better place for it but now there |
---|
| 90 | initStyleDefault(); |
---|
| 91 | // Making object from keywords, it makes a big perfomance overhead in IE |
---|
| 92 | // and slightly better perfomance in Mozilla |
---|
| 93 | // You can define any amount of keyword groups |
---|
| 94 | // Words should match ([a-z0-9][a-z0-9_]*) and should be separated by spaces |
---|
| 95 | var hPHPKeywords = cacheKeywords( /// TODO: Add more keywords here |
---|
| 96 | "for php case default char include int string new endfor real __FILE__ M_LN10 else return switch bool __LINE__ integer long void M_LOG2E break endif endwhile var require extends while FALSE E_NOTICE M_SQRT1_2 E_WARNING class if float mixed do M_LN2 as continue E_PARSE array function global old_function PHP_VERSION elseif M_1_PI M_2_PI M_EULER M_2_SQRTPI M_PI_2 M_SQRT3 M_PI_4 E_ERROR double endswitch M_LNPI M_SQRT2 M_LOG10E foreach TRUE static M_E object M_PI PHP_OS M_SQRTPI", |
---|
| 97 | "echo print count array" // standart functions |
---|
| 98 | ); |
---|
| 99 | var hSmartyKeywords = cacheKeywords( |
---|
| 100 | "if foreach capture section", // Block keywords |
---|
| 101 | "include assign math counter else foreachelse sectionelse cycle elseif", // Other keywords |
---|
| 102 | "getprices getarticles" // Own plugins for smarty |
---|
| 103 | ); |
---|
| 104 | |
---|
| 105 | function hLineToken(tokenType, tokenData) { |
---|
| 106 | this.type = tokenType; |
---|
| 107 | this.data = tokenData; |
---|
| 108 | this.reallength = tokenData.length; |
---|
| 109 | this.newState = -1; |
---|
| 110 | |
---|
| 111 | switch (this.data) { |
---|
| 112 | case '<': |
---|
| 113 | this.data='<'; |
---|
| 114 | break; |
---|
| 115 | case '>': |
---|
| 116 | this.data='>'; |
---|
| 117 | break; |
---|
| 118 | case '&': |
---|
| 119 | this.data='&'; |
---|
| 120 | break; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | switch (this.type) { |
---|
| 124 | case T_PINP_START: |
---|
| 125 | case T_PHP_START: |
---|
| 126 | case T_PINP_END: |
---|
| 127 | case T_PHP_END: |
---|
| 128 | case T_SCRIPT_START: |
---|
| 129 | case T_SINGLE_COMMENTS: |
---|
| 130 | this.data = this.data.replace(/[<]/g, '<'); |
---|
| 131 | this.data = this.data.replace(/[>]/g, '>'); |
---|
| 132 | var myTabRest = scannerPos % 8; |
---|
| 133 | var addLength = 8 - myTabRest; |
---|
| 134 | this.data = this.data.replace(/[\t]/g, ' '); |
---|
| 135 | scannerPos += (addLength -1); |
---|
| 136 | break; |
---|
| 137 | case T_SCRIPT_END: |
---|
| 138 | this.data = this.data.replace(/[<]/g, '<'); |
---|
| 139 | this.data = this.data.replace(/[>]/g, '>'); |
---|
| 140 | case T_SPACE: |
---|
| 141 | this.data = this.data.replace(/[ ]/g, ' '); |
---|
| 142 | break; |
---|
| 143 | case T_TAB: |
---|
| 144 | var myTabRest = scannerPos % 8; |
---|
| 145 | var addLength = 8 - myTabRest; |
---|
| 146 | // alert( 'scannerPos ' + scannerPos + ' myTabRest ' + myTabRest + ' addLength ' + addLength); |
---|
| 147 | this.data = this.data.replace(/[\t]/g, '<span style="padding-left: '+ (addLength*letterWidth) + 'px;"></span>'); |
---|
| 148 | scannerPos += (addLength -1); |
---|
| 149 | break; |
---|
| 150 | case T_EMPTY_LINE: |
---|
| 151 | this.data = '<span style="padding-left: 1px;"></span>'; |
---|
| 152 | break; |
---|
| 153 | } |
---|
| 154 | scannerPos += tokenData.length; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | function getToken(sData) { |
---|
| 158 | var re, match; |
---|
| 159 | |
---|
| 160 | /* empty line, required for Firefox 1.5.x */ |
---|
| 161 | if (sData == "") |
---|
| 162 | { |
---|
| 163 | result = new hLineToken(T_EMPTY_LINE, ""); |
---|
| 164 | return result; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | /* white space */ |
---|
| 168 | re = /^([ ]+)/; |
---|
| 169 | match = re.exec(sData); |
---|
| 170 | if (match) { |
---|
| 171 | result = new hLineToken(T_SPACE, match[1]); |
---|
| 172 | return result; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | re = /^([\t])/; |
---|
| 176 | match = re.exec(sData); |
---|
| 177 | if (match) { |
---|
| 178 | result = new hLineToken(T_TAB, match[1]); |
---|
| 179 | return result; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | /* Smarty tokens */ |
---|
| 183 | if(parseSmarty) { |
---|
| 184 | re = /^\{%/; // Matches $smarty->left_delimiter |
---|
| 185 | match = re.exec(sData); |
---|
| 186 | if(match) { |
---|
| 187 | return new hLineToken(TS_SMARTY_START, match[0]); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | re = /^%\}/; // Matches $smarty->right_delimiter |
---|
| 191 | match = re.exec(sData); |
---|
| 192 | if(match) { |
---|
| 193 | return new hLineToken(TS_SMARTY_END, match[0]); |
---|
| 194 | } |
---|
| 195 | re = /^\/[a-z0-9][a-z0-9_]*/i; |
---|
| 196 | match = re.exec(sData); |
---|
| 197 | if (match) { |
---|
| 198 | result = new hLineToken(TS_ENDTAG, match[0]); |
---|
| 199 | return result; |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | /* end of smarty tokens */ |
---|
| 203 | |
---|
| 204 | /* variable or ident */ |
---|
| 205 | re = /^([$]|->)?([a-z0-9_][a-z0-9_]*)/i; |
---|
| 206 | match = re.exec(sData); |
---|
| 207 | if (match) { |
---|
| 208 | if (match[1]) { |
---|
| 209 | result = new hLineToken(T_VAR, match[0]); |
---|
| 210 | } else { |
---|
| 211 | result = new hLineToken(T_IDENT, match[2]); |
---|
| 212 | } |
---|
| 213 | return result; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | /* single tokens */ |
---|
| 217 | re = /^([(){},"'\\])/; |
---|
| 218 | match = re.exec(sData); |
---|
| 219 | if (match) { |
---|
| 220 | switch (match[1]) { |
---|
| 221 | case '\\': |
---|
| 222 | result = new hLineToken(T_ESCAPE, match[1]); |
---|
| 223 | break; |
---|
| 224 | case '"': |
---|
| 225 | result = new hLineToken(T_DQUOTE, match[1]); |
---|
| 226 | break; |
---|
| 227 | case "'": |
---|
| 228 | result = new hLineToken(T_SQUOTE, match[1]); |
---|
| 229 | break; |
---|
| 230 | default: |
---|
| 231 | result = new hLineToken(T_SPECIAL_CHAR, match[1]); |
---|
| 232 | break; |
---|
| 233 | } |
---|
| 234 | return result; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | re = /^((\/[*])|([*]\/))/; |
---|
| 238 | match = re.exec(sData); |
---|
| 239 | if (match) { |
---|
| 240 | if (match[2]) { |
---|
| 241 | result = new hLineToken(T_BLOCKCOMMENT, match[2]); |
---|
| 242 | } else { |
---|
| 243 | result = new hLineToken(T_BLOCKCOMMENT_END, match[3]); |
---|
| 244 | } |
---|
| 245 | return result; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | /* comments */ |
---|
| 249 | re = /^(\/\/.*)/; |
---|
| 250 | match = re.exec(sData); |
---|
| 251 | if (match) { |
---|
| 252 | result = new hLineToken(T_SINGLE_COMMENTS, match[1]); |
---|
| 253 | return result; |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | /* php end tags */ |
---|
| 257 | re = /^([\?\%]>)/; |
---|
| 258 | match = re.exec(sData); |
---|
| 259 | if (match) { |
---|
| 260 | result = new hLineToken(T_PHP_END, match[0]); |
---|
| 261 | return result; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | re = /^([\-\+\.\*\/\=\%])/; |
---|
| 265 | match = re.exec(sData); |
---|
| 266 | if (match) { |
---|
| 267 | result = new hLineToken(T_OPERATOR, match[1]); |
---|
| 268 | return result; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | |
---|
| 272 | /* pinp/php tags */ |
---|
| 273 | re = /^((<(\/)?pinp>)|(<[%?]php|<\?|<script[^>]+language[^>]*=[^>]*php[^>]*>))/i; |
---|
| 274 | match = re.exec(sData); |
---|
| 275 | if (match) { |
---|
| 276 | if (match[3]) { |
---|
| 277 | result = new hLineToken(T_PINP_END, match[0]); |
---|
| 278 | } else |
---|
| 279 | if (match[2]) { |
---|
| 280 | result = new hLineToken(T_PINP_START, match[0]); |
---|
| 281 | } else { |
---|
| 282 | result = new hLineToken(T_PINP_START, match[0]); |
---|
| 283 | } |
---|
| 284 | return result; |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | /* javascript */ |
---|
| 288 | re = /^<(\/)?script[^>]*>/; |
---|
| 289 | match = re.exec(sData); |
---|
| 290 | if (match) { |
---|
| 291 | if (match[1]) { |
---|
| 292 | result = new hLineToken(T_SCRIPT_END, match[0]); |
---|
| 293 | } else { |
---|
| 294 | result = new hLineToken(T_SCRIPT_START, match[0]); |
---|
| 295 | } |
---|
| 296 | return result; |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | return new hLineToken(T_UNKNOWN, sData.charAt(0)); |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | function hLineParseString(sData) { |
---|
| 303 | var token; |
---|
| 304 | this.tokens = new Array(); |
---|
| 305 | |
---|
| 306 | scannerPos = 0; |
---|
| 307 | do |
---|
| 308 | { |
---|
| 309 | token = getToken(sData); |
---|
| 310 | this.tokens[this.tokens.length] = token; |
---|
| 311 | sData=sData.substring(token.reallength); |
---|
| 312 | } while (sData != ''); |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | function getElmSpan(token) { /// In the past styles were here |
---|
| 316 | var result = ''; // UPDATE: it is better to produce whole span tag here |
---|
| 317 | var cls = hStyles[token.type]; |
---|
| 318 | if(cls!='') result = '<span class="'+cls+'">'; |
---|
| 319 | else result = '<span>'; |
---|
| 320 | return result; |
---|
| 321 | } |
---|
| 322 | function getStateSpan(token) { /// Style span for states |
---|
| 323 | var result = ''; // UPDATE: it is better to produce whole span tag here |
---|
| 324 | var cls = hStateStyles[token.newState]; |
---|
| 325 | if(cls!='') result = '<span class="'+cls+'">'; |
---|
| 326 | else result = '<span>'; |
---|
| 327 | return result; |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | function hLineDoHighlight(callback) { |
---|
| 331 | var state = new Array(); |
---|
| 332 | var result = ''; |
---|
| 333 | if (this.lineNo) { |
---|
| 334 | /* load parent state */ |
---|
| 335 | state = state.concat(hLines[this.lineNo-1].getEndState()); |
---|
| 336 | // alert((this.lineNo-1)+':'+state.length); |
---|
| 337 | } |
---|
| 338 | for (var i = 0; i<state.length; i++) { |
---|
| 339 | if (!state[i].noMultiLine) { |
---|
| 340 | result += getStateSpan(state[i]); |
---|
| 341 | } |
---|
| 342 | } |
---|
| 343 | if (this.tokens) { |
---|
| 344 | for (var i=0; i<this.tokens.length; i++) { |
---|
| 345 | var cState = 0; |
---|
| 346 | var token = this.tokens[i]; |
---|
| 347 | if (state.length) { |
---|
| 348 | cState = state[state.length-1].newState; |
---|
| 349 | status = cState; |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | switch (cState) { // In the past state was the type of opening token |
---|
| 353 | case YY_STATE_HTML: // It is a real state now |
---|
| 354 | switch (token.type) { |
---|
| 355 | //Smarty highlighting |
---|
| 356 | case TS_SMARTY_START: |
---|
| 357 | token.newState = YYS_STATE_TAG; |
---|
| 358 | result += getStateSpan(token) + getElmSpan(token) + token.data + '</span>'; |
---|
| 359 | state.push(token); |
---|
| 360 | break; |
---|
| 361 | //End of smarty highlighting |
---|
| 362 | case T_PHP_START: |
---|
| 363 | case T_PINP_START: |
---|
| 364 | token.newState = YY_STATE_PINP; // We fix the new state here |
---|
| 365 | if (i == 1 && this.tokens[i-1].type == T_SPACE) { |
---|
| 366 | result = getStateSpan(token) + result + getElmSpan(token) + token.data + '</span>'; |
---|
| 367 | } else { |
---|
| 368 | result += getStateSpan(token) + getElmSpan(token) + token.data + '</span>'; |
---|
| 369 | } |
---|
| 370 | state.push(token); // But we still saving opening token |
---|
| 371 | break; |
---|
| 372 | case T_SCRIPT_START: |
---|
| 373 | token.newState = YY_STATE_SCRIPT; |
---|
| 374 | if (i == 1 && this.tokens[i-1].type == T_SPACE) { |
---|
| 375 | result = getStateSpan(token) + result + getElmSpan(token) + token.data + '</span>'; |
---|
| 376 | } else { |
---|
| 377 | result += getStateSpan(token) + getElmSpan(token) + token.data + '</span>'; |
---|
| 378 | } |
---|
| 379 | state.push(token); |
---|
| 380 | break; |
---|
| 381 | default: |
---|
| 382 | result += token.data; |
---|
| 383 | break; |
---|
| 384 | } |
---|
| 385 | break; |
---|
| 386 | case YY_STATE_SCRIPT: |
---|
| 387 | switch (token.type) { |
---|
| 388 | case T_PHP_START: |
---|
| 389 | case T_PINP_START: |
---|
| 390 | token.newState = YY_STATE_PINP; |
---|
| 391 | if (i == 1 && this.tokens[i-1].type == T_SPACE) { |
---|
| 392 | result = getStateSpan(token) + result + getElmSpan(token) + token.data + '</span>'; |
---|
| 393 | } else { |
---|
| 394 | result += getStateSpan(token) + getElmSpan(token) + token.data + '</span>'; |
---|
| 395 | } |
---|
| 396 | state.push(token); |
---|
| 397 | break; |
---|
| 398 | case T_SCRIPT_END: |
---|
| 399 | result += getElmSpan(token)+token.data+'</span>'; |
---|
| 400 | result += '</span>'; |
---|
| 401 | state.pop(); |
---|
| 402 | break; |
---|
| 403 | default: |
---|
| 404 | result += token.data; |
---|
| 405 | break; |
---|
| 406 | } |
---|
| 407 | break; |
---|
| 408 | case YY_STATE_PINP: |
---|
| 409 | switch (token.type) { |
---|
| 410 | case T_IDENT: |
---|
| 411 | var t = hPHPKeywords[token.data]; |
---|
| 412 | if(typeof(t)!='undefined') { |
---|
| 413 | result += '<span class="h_phpkeywords'+t+'">'; |
---|
| 414 | } else { |
---|
| 415 | result += getElmSpan(token); |
---|
| 416 | } |
---|
| 417 | result += token.data + '</span>'; |
---|
| 418 | break; |
---|
| 419 | case T_DQUOTE: |
---|
| 420 | token.newState = YY_STATE_DQSTRING; |
---|
| 421 | case T_SQUOTE: |
---|
| 422 | if(token.newState<0) token.newState = YY_STATE_SQSTRING; |
---|
| 423 | case T_BLOCKCOMMENT: |
---|
| 424 | if(token.newState<0) token.newState = YY_STATE_BLOCKCOMMENT; |
---|
| 425 | result += getStateSpan(token); |
---|
| 426 | result += token.data; |
---|
| 427 | state.push(token); |
---|
| 428 | break; |
---|
| 429 | case T_PHP_END: |
---|
| 430 | case T_PINP_END: |
---|
| 431 | result += getElmSpan(token)+token.data+'</span>'; |
---|
| 432 | result += '</span>'; |
---|
| 433 | state.pop(); |
---|
| 434 | break; |
---|
| 435 | case T_VAR: |
---|
| 436 | case T_OPERATOR: |
---|
| 437 | case T_SPECIAL_CHAR: |
---|
| 438 | case T_SINGLE_COMMENTS: |
---|
| 439 | result += getElmSpan(token); |
---|
| 440 | result += token.data; |
---|
| 441 | result += '</span>'; |
---|
| 442 | break; |
---|
| 443 | default: |
---|
| 444 | result += token.data; |
---|
| 445 | break; |
---|
| 446 | } |
---|
| 447 | break; |
---|
| 448 | case YY_STATE_BLOCKCOMMENT: |
---|
| 449 | switch (token.type) { |
---|
| 450 | case T_BLOCKCOMMENT_END: |
---|
| 451 | result += token.data+'</span>'; |
---|
| 452 | state.pop(); |
---|
| 453 | break; |
---|
| 454 | default: |
---|
| 455 | result += token.data; |
---|
| 456 | break; |
---|
| 457 | } |
---|
| 458 | break; |
---|
| 459 | case YY_STATE_DQSTRING: |
---|
| 460 | switch (token.type) { |
---|
| 461 | case T_DQUOTE: |
---|
| 462 | result += token.data+'</span>'; |
---|
| 463 | state.pop(); |
---|
| 464 | break; |
---|
| 465 | case T_ESCAPE: |
---|
| 466 | result += token.data; |
---|
| 467 | token = this.tokens[++i]; |
---|
| 468 | result += token.data; |
---|
| 469 | break; |
---|
| 470 | case T_VAR: |
---|
| 471 | result += getElmSpan(token); |
---|
| 472 | result += token.data; |
---|
| 473 | result += '</span>'; |
---|
| 474 | break; |
---|
| 475 | default: |
---|
| 476 | result += token.data; |
---|
| 477 | break; |
---|
| 478 | } |
---|
| 479 | break; |
---|
| 480 | case YY_STATE_SQSTRING: |
---|
| 481 | switch (token.type) { |
---|
| 482 | case T_SQUOTE: |
---|
| 483 | result += token.data+'</span>'; |
---|
| 484 | state.pop(); |
---|
| 485 | break; |
---|
| 486 | case T_ESCAPE: |
---|
| 487 | result += token.data; |
---|
| 488 | token = this.tokens[++i]; |
---|
| 489 | result += token.data; |
---|
| 490 | break; |
---|
| 491 | default: |
---|
| 492 | result += token.data; |
---|
| 493 | break; |
---|
| 494 | } |
---|
| 495 | break; |
---|
| 496 | case YYS_STATE_TAG: |
---|
| 497 | switch (token.type) { |
---|
| 498 | case TS_SMARTY_END: |
---|
| 499 | result += getElmSpan(token) + token.data + '</span></span>'; |
---|
| 500 | state.pop(); |
---|
| 501 | break; |
---|
| 502 | case TS_ENDTAG: |
---|
| 503 | var t = hSmartyKeywords[token.data.substr(1)]; |
---|
| 504 | if(t==1) { // Only the first group has closing tags |
---|
| 505 | result += '<span class="h_smartykeywords'+t+'">' + token.data + '</span>'; |
---|
| 506 | } else { |
---|
| 507 | result += token.data; |
---|
| 508 | } |
---|
| 509 | break; |
---|
| 510 | case T_VAR: |
---|
| 511 | result += getElmSpan(token) + token.data + '</span>'; |
---|
| 512 | return; |
---|
| 513 | case T_IDENT: |
---|
| 514 | var t = hSmartyKeywords[token.data]; |
---|
| 515 | if(typeof(t)!='undefined' && |
---|
| 516 | (t>3 || // first three groups of keywords may appear |
---|
| 517 | (i>0 && this.tokens[i-1].type==TS_SMARTY_START)) // immediately after TS_SMARTY_START token only |
---|
| 518 | ) { |
---|
| 519 | result += '<span class="h_smartykeywords'+t+'">' + token.data + '</span>'; |
---|
| 520 | } else { |
---|
| 521 | result += token.data; |
---|
| 522 | } |
---|
| 523 | break; |
---|
| 524 | default: |
---|
| 525 | result += token.data; |
---|
| 526 | break; |
---|
| 527 | } |
---|
| 528 | break; |
---|
| 529 | default: |
---|
| 530 | result += token.data; |
---|
| 531 | break; |
---|
| 532 | } |
---|
| 533 | } |
---|
| 534 | // alert(this.lineNo+'::'+this.tokens.length+'::'+result); |
---|
| 535 | |
---|
| 536 | } |
---|
| 537 | var stateChanged = 0; |
---|
| 538 | if (state.length != this.getEndState().length) { |
---|
| 539 | stateChanged = 1; |
---|
| 540 | } |
---|
| 541 | |
---|
| 542 | for (i=state.length-1; i>=0; i--) { |
---|
| 543 | if (!stateChanged && state[i].type!=this.getEndState()[i].type) { |
---|
| 544 | stateChanged = 1; |
---|
| 545 | } |
---|
| 546 | if (!state[i].noMultiLine) { |
---|
| 547 | result += getElmSpan(state[i]); |
---|
| 548 | } |
---|
| 549 | } |
---|
| 550 | |
---|
| 551 | /* report update */ |
---|
| 552 | if (callback) { |
---|
| 553 | // alert(this.lineNo+"::"+result); |
---|
| 554 | if (result) { |
---|
| 555 | // alert(this.lineNo+': 2 eol chars: "'+result.substr(result.length-2)+'"'); |
---|
| 556 | } |
---|
| 557 | callback(this.lineNo, result); |
---|
| 558 | } |
---|
| 559 | this.setEndState(state); |
---|
| 560 | if (stateChanged) { |
---|
| 561 | if (debug) alert('updating: '+this.lineNo+1); |
---|
| 562 | /// This makes a stack overflow |
---|
| 563 | /// so we are returning 'true' that means next line must be updated |
---|
| 564 | //~ hLines[this.lineNo+1].doHighlight(callback); |
---|
| 565 | return true; |
---|
| 566 | } |
---|
| 567 | /// Next line need not be updated |
---|
| 568 | return false; |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | function hLineSetEndState(newEndState) { |
---|
| 572 | // alert(this.lineNo+': new endstate: '+newEndState.length); |
---|
| 573 | var frop=new Array(); |
---|
| 574 | if (newEndState.length) { |
---|
| 575 | // alert(':'+newEndState.toString()+':'); |
---|
| 576 | this.endState=frop.concat(newEndState); //newEndState; //.toSource(); |
---|
| 577 | } else { |
---|
| 578 | this.endState=new Array(); |
---|
| 579 | } |
---|
| 580 | /* |
---|
| 581 | var line = hLines[2]; |
---|
| 582 | if (line) { |
---|
| 583 | alert(this.lineNo+'->'+line.endState.length); |
---|
| 584 | } |
---|
| 585 | */ |
---|
| 586 | } |
---|
| 587 | |
---|
| 588 | function hLineGetEndState() { |
---|
| 589 | return this.endState; |
---|
| 590 | } |
---|
| 591 | |
---|
| 592 | function hLineRemove() { |
---|
| 593 | if (this.lineNo < (hLines.length-1)) { |
---|
| 594 | var len = hLines.length-1; |
---|
| 595 | for (var i=this.lineNo; i<len; i++) { |
---|
| 596 | hLines[i] = hLines[i+1]; |
---|
| 597 | hLines[i].lineNo = i; |
---|
| 598 | } |
---|
| 599 | } |
---|
| 600 | hLines.pop(); |
---|
| 601 | } |
---|
| 602 | |
---|
| 603 | function hLine(lineNo, lineString) { |
---|
| 604 | this.lineNo = lineNo; |
---|
| 605 | if (lineNo && (lineNo != hLines.length)) { |
---|
| 606 | var hLinesLen = hLines.length; |
---|
| 607 | for (var i=hLinesLen; i>lineNo; i--) { |
---|
| 608 | hLines[i] = hLines[i-1]; |
---|
| 609 | hLines[i].lineNo = i; |
---|
| 610 | } |
---|
| 611 | } |
---|
| 612 | hLines[lineNo] = this; |
---|
| 613 | this.tokens = new Array(); |
---|
| 614 | this.setEndState = hLineSetEndState; |
---|
| 615 | this.setEndState(new Array()); |
---|
| 616 | this.getEndState = hLineGetEndState; |
---|
| 617 | this.remove = hLineRemove; |
---|
| 618 | |
---|
| 619 | this.parseString = hLineParseString; |
---|
| 620 | this.parseString(lineString); |
---|
| 621 | |
---|
| 622 | if (debug) alert(this.lineNo); |
---|
| 623 | this.doHighlight = hLineDoHighlight; |
---|
| 624 | } |
---|
| 625 | |
---|
| 626 | function highlightUpdateLine(lineNo, lineContent, callback) { |
---|
| 627 | // alert('update line: '+lineNo+'::'+lineContent); |
---|
| 628 | hLines[lineNo].parseString(lineContent); |
---|
| 629 | while(hLines[lineNo].doHighlight(callback) && lineNo < hLines.length-1) lineNo++; |
---|
| 630 | } |
---|
| 631 | |
---|
| 632 | function highlightDeleteLine(lineNo, callback) { |
---|
| 633 | // alert('remove line: '+lineNo); |
---|
| 634 | line = hLines[lineNo]; |
---|
| 635 | line.remove(); |
---|
| 636 | if (hLines.length && (lineNo < hLines.length)) |
---|
| 637 | while(hLines[lineNo].doHighlight(callback) && lineNo < hLines.length-1) lineNo++; |
---|
| 638 | } |
---|
| 639 | |
---|
| 640 | function highlightReset() { |
---|
| 641 | hLines = new Array(); |
---|
| 642 | new hLine(0, ''); |
---|
| 643 | } |
---|
| 644 | |
---|
| 645 | function highlightInsertLine(lineNo, lineContent, callback) { |
---|
| 646 | if (lineNo) { |
---|
| 647 | lineNo -= 1; |
---|
| 648 | } |
---|
| 649 | // alert('insert at: '+lineNo+'::'+lineContent); |
---|
| 650 | line = new hLine(lineNo, lineContent); |
---|
| 651 | while(line.doHighlight(callback) && lineNo < hLines.length-1) lineNo++; |
---|
| 652 | } |
---|
| 653 | |
---|
| 654 | function highlightAppendLine(lineNo, lineContent, callback) { |
---|
| 655 | // alert('append at: '+(lineNo+1)+'::'+lineContent); |
---|
| 656 | line = new hLine(lineNo+1, new String(lineContent)); |
---|
| 657 | while(hLines[lineNo].doHighlight(callback) && lineNo < hLines.length-1) lineNo++; |
---|
| 658 | } |
---|
| 659 | |
---|
| 660 | function initStyleDefault() { |
---|
| 661 | hStyles[T_PINP_START] = |
---|
| 662 | hStyles[T_PINP_END] = |
---|
| 663 | hStyles[T_PHP_START] = 'h_pinp'; |
---|
| 664 | hStyles[T_VAR] = 'h_var'; |
---|
| 665 | hStyles[T_PHP_START] = 'h_script'; |
---|
| 666 | hStyles[T_IDENT] = 'h_ident'; |
---|
| 667 | hStyles[T_DQUOTE] = 'h_doublequote'; |
---|
| 668 | hStyles[T_SQUOTE] = 'h_singlequote'; |
---|
| 669 | hStyles[T_SPECIAL_CHAR] = 'h_special_char'; |
---|
| 670 | hStyles[T_OPERATOR] = 'h_operator'; |
---|
| 671 | hStyles[T_SINGLE_COMMENTS] = 'h_single_comments'; |
---|
| 672 | hStyles[T_BLOCKCOMMENT] = 'h_blockcomment'; |
---|
| 673 | hStyles[TS_SMARTY_START] = 'h_smartymarkers'; |
---|
| 674 | hStyles[TS_SMARTY_END] = 'h_smartymarkers'; |
---|
| 675 | hStateStyles[YY_STATE_HTML] = ''; |
---|
| 676 | hStateStyles[YY_STATE_PINP] = 'h_pinp_block'; |
---|
| 677 | hStateStyles[YY_STATE_DQSTRING] = 'h_doublequote'; |
---|
| 678 | hStateStyles[YY_STATE_SQSTRING] = 'h_singlequote'; |
---|
| 679 | hStateStyles[YY_STATE_BLOCKCOMMENT] = 'h_blockcomment'; |
---|
| 680 | hStateStyles[YY_STATE_SCRIPT] = 'h_scriptblock'; |
---|
| 681 | hStateStyles[YYS_STATE_TAG] = 'h_smartytag'; |
---|
| 682 | } |
---|
| 683 | function cacheKeywords() { |
---|
| 684 | var res = new Object(); |
---|
| 685 | for(var i=0;i<arguments.length;i++) { |
---|
| 686 | var t = String(arguments[i]).split(" "); |
---|
| 687 | for(var j=0;j<t.length;j++) |
---|
| 688 | res[t[j]]=i+1; |
---|
| 689 | } |
---|
| 690 | return res; |
---|
| 691 | } |
---|