2017-02-27 21:00:13 +00:00
|
|
|
import CodeMirror from 'codemirror';
|
|
|
|
|
|
|
|
CodeMirror.defineMode('nunjucks', (config, parserConfig) => {
|
|
|
|
const baseMode = CodeMirror.getMode(config, parserConfig.baseMode || 'text/plain');
|
|
|
|
const nunjucksMode = _nunjucksMode();
|
|
|
|
return CodeMirror.overlayMode(baseMode, nunjucksMode, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
function _nunjucksMode () {
|
2017-04-07 18:10:15 +00:00
|
|
|
const regexVariable = /^{{\s*([^ }]+)\s*[^}]*\s*}}/;
|
|
|
|
const regexTag = /^{%\s*([^ }]+)\s*[^%]*\s*%}/;
|
2017-03-12 00:31:23 +00:00
|
|
|
const regexComment = /^{#\s*[^#]+\s*#}/;
|
2017-02-27 21:00:13 +00:00
|
|
|
let ticker = 1;
|
|
|
|
|
|
|
|
return {
|
2017-03-03 20:09:08 +00:00
|
|
|
startState () {
|
2017-02-27 21:00:13 +00:00
|
|
|
return {inRaw: false};
|
|
|
|
},
|
|
|
|
token (stream, state) {
|
|
|
|
let m;
|
|
|
|
|
|
|
|
// This makes sure that adjacent tags still have unique types
|
|
|
|
ticker *= -1;
|
|
|
|
|
|
|
|
m = stream.match(regexTag, true);
|
|
|
|
if (m) {
|
|
|
|
const name = m[1];
|
|
|
|
if (state.inRaw && name === 'endraw') {
|
|
|
|
state.inRaw = false;
|
|
|
|
} else if (!state.inRaw && name === 'raw') {
|
|
|
|
state.inRaw = true;
|
|
|
|
} else if (state.inRaw) {
|
|
|
|
// Inside raw tag so do nothing
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-03-09 06:23:23 +00:00
|
|
|
return `nunjucks-tag ${ticker}`;
|
2017-02-27 21:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!state.inRaw) {
|
|
|
|
m = stream.match(regexVariable, true);
|
|
|
|
if (m) {
|
2017-03-09 06:23:23 +00:00
|
|
|
return `nunjucks-variable ${ticker}`;
|
2017-02-27 21:00:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!state.inRaw) {
|
|
|
|
m = stream.match(regexComment, true);
|
|
|
|
if (m) {
|
2017-03-09 06:23:23 +00:00
|
|
|
return `nunjucks-comment ${ticker}`;
|
2017-02-27 21:00:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (stream.next() != null) {
|
|
|
|
if (stream.match(regexVariable, false)) break;
|
|
|
|
if (stream.match(regexTag, false)) break;
|
|
|
|
if (stream.match(regexComment, false)) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2017-03-03 20:09:08 +00:00
|
|
|
};
|
2017-02-27 21:00:13 +00:00
|
|
|
}
|