Dotclear

source: plugins/themeEditor/codemirror/php.js @ 948:206fd3d17d64

Revision 948:206fd3d17d64, 6.0 KB checked in by franck <carnet.franck.paul@…>, 13 years ago (diff)

Add syntax color option to theme editor plugin, using CodeMirror? ( http://codemirror.net/), fixes #628

Line 
1(function() {
2  function keywords(str) {
3    var obj = {}, words = str.split(" ");
4    for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
5    return obj;
6  }
7  function heredoc(delim) {
8    return function(stream, state) {
9      if (stream.match(delim)) state.tokenize = null;
10      else stream.skipToEnd();
11      return "string";
12    };
13  }
14  var phpConfig = {
15    name: "clike",
16    keywords: keywords("abstract and array as break case catch class clone const continue declare default " +
17                       "do else elseif enddeclare endfor endforeach endif endswitch endwhile extends final " +
18                       "for foreach function global goto if implements interface instanceof namespace " +
19                       "new or private protected public static switch throw trait try use var while xor " +
20                       "die echo empty exit eval include include_once isset list require require_once return " +
21                       "print unset __halt_compiler self static parent"),
22    blockKeywords: keywords("catch do else elseif for foreach if switch try while"),
23    atoms: keywords("true false null TRUE FALSE NULL"),
24    multiLineStrings: true,
25    hooks: {
26      "$": function(stream, state) {
27        stream.eatWhile(/[\w\$_]/);
28        return "variable-2";
29      },
30      "<": function(stream, state) {
31        if (stream.match(/<</)) {
32          stream.eatWhile(/[\w\.]/);
33          state.tokenize = heredoc(stream.current().slice(3));
34          return state.tokenize(stream, state);
35        }
36        return false;
37      },
38      "#": function(stream, state) {
39        while (!stream.eol() && !stream.match("?>", false)) stream.next();
40        return "comment";
41      },
42      "/": function(stream, state) {
43        if (stream.eat("/")) {
44          while (!stream.eol() && !stream.match("?>", false)) stream.next();
45          return "comment";
46        }
47        return false;
48      }
49    }
50  };
51
52  CodeMirror.defineMode("php", function(config, parserConfig) {
53    var htmlMode = CodeMirror.getMode(config, {name: "xml", htmlMode: true});
54    var jsMode = CodeMirror.getMode(config, "javascript");
55    var cssMode = CodeMirror.getMode(config, "css");
56    var phpMode = CodeMirror.getMode(config, phpConfig);
57
58    function dispatch(stream, state) { // TODO open PHP inside text/css
59      var isPHP = state.curMode == phpMode;
60      if (stream.sol() && state.pending != '"') state.pending = null;
61      if (state.curMode == htmlMode) {
62        if (stream.match(/^<\?\w*/)) {
63          state.curMode = phpMode;
64          state.curState = state.php;
65          state.curClose = "?>";
66          return "meta";
67        }
68        if (state.pending == '"') {
69          while (!stream.eol() && stream.next() != '"') {}
70          var style = "string";
71        } else if (state.pending && stream.pos < state.pending.end) {
72          stream.pos = state.pending.end;
73          var style = state.pending.style;
74        } else {
75          var style = htmlMode.token(stream, state.curState);
76        }
77        state.pending = null;
78        var cur = stream.current(), openPHP = cur.search(/<\?/);
79        if (openPHP != -1) {
80          if (style == "string" && /\"$/.test(cur) && !/\?>/.test(cur)) state.pending = '"';
81          else state.pending = {end: stream.pos, style: style};
82          stream.backUp(cur.length - openPHP);
83        } else if (style == "tag" && stream.current() == ">" && state.curState.context) {
84          if (/^script$/i.test(state.curState.context.tagName)) {
85            state.curMode = jsMode;
86            state.curState = jsMode.startState(htmlMode.indent(state.curState, ""));
87            state.curClose = /^<\/\s*script\s*>/i;
88          }
89          else if (/^style$/i.test(state.curState.context.tagName)) {
90            state.curMode = cssMode;
91            state.curState = cssMode.startState(htmlMode.indent(state.curState, ""));
92            state.curClose = /^<\/\s*style\s*>/i;
93          }
94        }
95        return style;
96      } else if ((!isPHP || state.php.tokenize == null) &&
97                 stream.match(state.curClose, isPHP)) {
98        state.curMode = htmlMode;
99        state.curState = state.html;
100        state.curClose = null;
101        if (isPHP) return "meta";
102        else return dispatch(stream, state);
103      } else {
104        return state.curMode.token(stream, state.curState);
105      }
106    }
107
108    return {
109      startState: function() {
110        var html = htmlMode.startState();
111        return {html: html,
112                php: phpMode.startState(),
113                curMode: parserConfig.startOpen ? phpMode : htmlMode,
114                curState: parserConfig.startOpen ? phpMode.startState() : html,
115                curClose: parserConfig.startOpen ? /^\?>/ : null,
116          mode: parserConfig.startOpen ? "php" : "html",
117                pending: null};
118      },
119
120      copyState: function(state) {
121        var html = state.html, htmlNew = CodeMirror.copyState(htmlMode, html),
122            php = state.php, phpNew = CodeMirror.copyState(phpMode, php), cur;
123        if (state.curState == html) cur = htmlNew;
124        else if (state.curState == php) cur = phpNew;
125        else cur = CodeMirror.copyState(state.curMode, state.curState);
126        return {html: htmlNew, php: phpNew, curMode: state.curMode, curState: cur,
127                curClose: state.curClose, mode: state.mode,
128                pending: state.pending};
129      },
130
131      token: dispatch,
132
133      indent: function(state, textAfter) {
134        if ((state.curMode != phpMode && /^\s*<\//.test(textAfter)) ||
135            (state.curMode == phpMode && /^\?>/.test(textAfter)))
136          return htmlMode.indent(state.html, textAfter);
137        return state.curMode.indent(state.curState, textAfter);
138      },
139
140      electricChars: "/{}:",
141
142      innerMode: function(state) { return {state: state.curState, mode: state.curMode}; }
143    };
144  }, "xml", "clike", "javascript", "css");
145  CodeMirror.defineMIME("application/x-httpd-php", "php");
146  CodeMirror.defineMIME("application/x-httpd-php-open", {name: "php", startOpen: true});
147  CodeMirror.defineMIME("text/x-php", phpConfig);
148})();
Note: See TracBrowser for help on using the repository browser.

Sites map