aboutsummaryrefslogtreecommitdiff
path: root/imports/codemirror/mode/php
diff options
context:
space:
mode:
Diffstat (limited to 'imports/codemirror/mode/php')
-rwxr-xr-ximports/codemirror/mode/php/index.html48
-rwxr-xr-ximports/codemirror/mode/php/php.js120
2 files changed, 168 insertions, 0 deletions
diff --git a/imports/codemirror/mode/php/index.html b/imports/codemirror/mode/php/index.html
new file mode 100755
index 00000000..7949044e
--- /dev/null
+++ b/imports/codemirror/mode/php/index.html
@@ -0,0 +1,48 @@
1<!doctype html>
2<html>
3 <head>
4 <title>CodeMirror: PHP mode</title>
5 <link rel="stylesheet" href="../../lib/codemirror.css">
6 <script src="../../lib/codemirror.js"></script>
7 <script src="../xml/xml.js"></script>
8 <script src="../javascript/javascript.js"></script>
9 <script src="../css/css.js"></script>
10 <script src="../clike/clike.js"></script>
11 <script src="php.js"></script>
12 <style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
13 <link rel="stylesheet" href="../../doc/docs.css">
14 </head>
15 <body>
16 <h1>CodeMirror: PHP mode</h1>
17
18<form><textarea id="code" name="code">
19<?php
20function hello($who) {
21 return "Hello " . $who;
22}
23?>
24<p>The program says <?= hello("World") ?>.</p>
25<script>
26 alert("And here is some JS code"); // also colored
27</script>
28</textarea></form>
29
30 <script>
31 var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
32 lineNumbers: true,
33 matchBrackets: true,
34 mode: "application/x-httpd-php",
35 indentUnit: 4,
36 indentWithTabs: true,
37 enterMode: "keep",
38 tabMode: "shift"
39 });
40 </script>
41
42 <p>Simple HTML/PHP mode based on
43 the <a href="../clike/">C-like</a> mode. Depends on XML,
44 JavaScript, CSS, and C-like modes.</p>
45
46 <p><strong>MIME types defined:</strong> <code>application/x-httpd-php</code> (HTML with PHP code), <code>text/x-php</code> (plain, non-wrapped PHP code).</p>
47 </body>
48</html>
diff --git a/imports/codemirror/mode/php/php.js b/imports/codemirror/mode/php/php.js
new file mode 100755
index 00000000..f88b3f84
--- /dev/null
+++ b/imports/codemirror/mode/php/php.js
@@ -0,0 +1,120 @@
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 cfunction class clone const continue declare " +
17 "default do else elseif enddeclare endfor endforeach endif endswitch endwhile extends " +
18 "final for foreach function global goto if implements interface instanceof namespace " +
19 "new or private protected public static switch throw try use var while xor return" +
20 "die echo empty exit eval include include_once isset list require require_once print unset"),
21 blockKeywords: keywords("catch do else elseif for foreach if switch try while"),
22 atoms: keywords("true false null TRUE FALSE NULL"),
23 multiLineStrings: true,
24 hooks: {
25 "$": function(stream, state) {
26 stream.eatWhile(/[\w\$_]/);
27 return "variable-2";
28 },
29 "<": function(stream, state) {
30 if (stream.match(/<</)) {
31 stream.eatWhile(/[\w\.]/);
32 state.tokenize = heredoc(stream.current().slice(3));
33 return state.tokenize(stream, state);
34 }
35 return false;
36 },
37 "#": function(stream, state) {
38 stream.skipToEnd();
39 return "comment";
40 }
41 }
42 };
43
44 CodeMirror.defineMode("php", function(config, parserConfig) {
45 var htmlMode = CodeMirror.getMode(config, {name: "xml", htmlMode: true});
46 var jsMode = CodeMirror.getMode(config, "javascript");
47 var cssMode = CodeMirror.getMode(config, "css");
48 var phpMode = CodeMirror.getMode(config, phpConfig);
49
50 function dispatch(stream, state) { // TODO open PHP inside text/css
51 if (state.curMode == htmlMode) {
52 var style = htmlMode.token(stream, state.curState);
53 if (style == "meta" && /^<\?/.test(stream.current())) {
54 state.curMode = phpMode;
55 state.curState = state.php;
56 state.curClose = /^\?>/;
57 state.mode = 'php';
58 }
59 else if (style == "tag" && stream.current() == ">" && state.curState.context) {
60 if (/^script$/i.test(state.curState.context.tagName)) {
61 state.curMode = jsMode;
62 state.curState = jsMode.startState(htmlMode.indent(state.curState, ""));
63 state.curClose = /^<\/\s*script\s*>/i;
64 state.mode = 'javascript';
65 }
66 else if (/^style$/i.test(state.curState.context.tagName)) {
67 state.curMode = cssMode;
68 state.curState = cssMode.startState(htmlMode.indent(state.curState, ""));
69 state.curClose = /^<\/\s*style\s*>/i;
70 state.mode = 'css';
71 }
72 }
73 return style;
74 }
75 else if (stream.match(state.curClose, false)) {
76 state.curMode = htmlMode;
77 state.curState = state.html;
78 state.curClose = null;
79 state.mode = 'html';
80 return dispatch(stream, state);
81 }
82 else return state.curMode.token(stream, state.curState);
83 }
84
85 return {
86 startState: function() {
87 var html = htmlMode.startState();
88 return {html: html,
89 php: phpMode.startState(),
90 curMode: parserConfig.startOpen ? phpMode : htmlMode,
91 curState: parserConfig.startOpen ? phpMode.startState() : html,
92 curClose: parserConfig.startOpen ? /^\?>/ : null,
93 mode: parserConfig.startOpen ? 'php' : 'html'}
94 },
95
96 copyState: function(state) {
97 var html = state.html, htmlNew = CodeMirror.copyState(htmlMode, html),
98 php = state.php, phpNew = CodeMirror.copyState(phpMode, php), cur;
99 if (state.curState == html) cur = htmlNew;
100 else if (state.curState == php) cur = phpNew;
101 else cur = CodeMirror.copyState(state.curMode, state.curState);
102 return {html: htmlNew, php: phpNew, curMode: state.curMode, curState: cur, curClose: state.curClose};
103 },
104
105 token: dispatch,
106
107 indent: function(state, textAfter) {
108 if ((state.curMode != phpMode && /^\s*<\//.test(textAfter)) ||
109 (state.curMode == phpMode && /^\?>/.test(textAfter)))
110 return htmlMode.indent(state.html, textAfter);
111 return state.curMode.indent(state.curState, textAfter);
112 },
113
114 electricChars: "/{}:"
115 }
116 });
117 CodeMirror.defineMIME("application/x-httpd-php", "php");
118 CodeMirror.defineMIME("application/x-httpd-php-open", {name: "php", startOpen: true});
119 CodeMirror.defineMIME("text/x-php", phpConfig);
120})();