// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"), require("../javascript/javascript"), require("../css/css"), require("../htmlmixed/htmlmixed"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror", "../javascript/javascript", "../css/css", "../htmlmixed/htmlmixed"], mod);
else // Plain browser env
})(function(CodeMirror) {
CodeMirror.defineMode("pug", function (config) {
var jsMode = CodeMirror.getMode(config, 'javascript');
this.javaScriptLine = false;
this.javaScriptLineExcludesColon = false;
this.javaScriptArguments = false;
this.javaScriptArgumentsDepth = 0;
this.isInterpolating = false;
this.interpolationNesting = 0;
this.jsState = CodeMirror.startState(jsMode);
this.isIncludeFiltered = false;
this.inAttributeName = true;
this.attributeIsType = false;
this.indentOf = Infinity;
this.innerModeForLine = false;
State.prototype.copy = function () {
res.javaScriptLine = this.javaScriptLine;
res.javaScriptLineExcludesColon = this.javaScriptLineExcludesColon;
res.javaScriptArguments = this.javaScriptArguments;
res.javaScriptArgumentsDepth = this.javaScriptArgumentsDepth;
res.isInterpolating = this.isInterpolating;
res.interpolationNesting = this.interpolationNesting;
res.jsState = CodeMirror.copyState(jsMode, this.jsState);
res.innerMode = this.innerMode;
if (this.innerMode && this.innerState) {
res.innerState = CodeMirror.copyState(this.innerMode, this.innerState);
res.restOfLine = this.restOfLine;
res.isIncludeFiltered = this.isIncludeFiltered;
res.isEach = this.isEach;
res.lastTag = this.lastTag;
res.scriptType = this.scriptType;
res.isAttrs = this.isAttrs;
res.attrsNest = this.attrsNest.slice();
res.inAttributeName = this.inAttributeName;
res.attributeIsType = this.attributeIsType;
res.attrValue = this.attrValue;
res.indentOf = this.indentOf;
res.indentToken = this.indentToken;
res.innerModeForLine = this.innerModeForLine;
function javaScript(stream, state) {
// if javaScriptLine was set at end of line, ignore it
state.javaScriptLine = false;
state.javaScriptLineExcludesColon = false;
if (state.javaScriptLine) {
if (state.javaScriptLineExcludesColon && stream.peek() === ':') {
state.javaScriptLine = false;
state.javaScriptLineExcludesColon = false;
var tok = jsMode.token(stream, state.jsState);
if (stream.eol()) state.javaScriptLine = false;
function javaScriptArguments(stream, state) {
if (state.javaScriptArguments) {
if (state.javaScriptArgumentsDepth === 0 && stream.peek() !== '(') {
state.javaScriptArguments = false;
if (stream.peek() === '(') {
state.javaScriptArgumentsDepth++;
} else if (stream.peek() === ')') {
state.javaScriptArgumentsDepth--;
if (state.javaScriptArgumentsDepth === 0) {
state.javaScriptArguments = false;
var tok = jsMode.token(stream, state.jsState);
function yieldStatement(stream) {
if (stream.match(/^yield\b/)) {
function doctype(stream) {
if (stream.match(/^(?:doctype) *([^\n]+)?/)) {
function interpolation(stream, state) {
if (stream.match('#{')) {
state.isInterpolating = true;
state.interpolationNesting = 0;
function interpolationContinued(stream, state) {
if (state.isInterpolating) {
if (stream.peek() === '}') {
state.interpolationNesting--;
if (state.interpolationNesting < 0) {
state.isInterpolating = false;
} else if (stream.peek() === '{') {
state.interpolationNesting++;
return jsMode.token(stream, state.jsState) || true;
function caseStatement(stream, state) {
if (stream.match(/^case\b/)) {
state.javaScriptLine = true;
function when(stream, state) {
if (stream.match(/^when\b/)) {
state.javaScriptLine = true;
state.javaScriptLineExcludesColon = true;
function defaultStatement(stream) {
if (stream.match(/^default\b/)) {
function extendsStatement(stream, state) {
if (stream.match(/^extends?\b/)) {
state.restOfLine = 'string';
function append(stream, state) {
if (stream.match(/^append\b/)) {
state.restOfLine = 'variable';
function prepend(stream, state) {
if (stream.match(/^prepend\b/)) {
state.restOfLine = 'variable';
function block(stream, state) {
if (stream.match(/^block\b *(?:(prepend|append)\b)?/)) {
state.restOfLine = 'variable';
function include(stream, state) {
if (stream.match(/^include\b/)) {
state.restOfLine = 'string';
function includeFiltered(stream, state) {
if (stream.match(/^include:([a-zA-Z0-9\-]+)/, false) && stream.match('include')) {
state.isIncludeFiltered = true;
function includeFilteredContinued(stream, state) {
if (state.isIncludeFiltered) {
var tok = filter(stream, state);
state.isIncludeFiltered = false;
state.restOfLine = 'string';
function mixin(stream, state) {
if (stream.match(/^mixin\b/)) {
state.javaScriptLine = true;
function call(stream, state) {
if (stream.match(/^\+([-\w]+)/)) {
if (!stream.match(/^\( *[-\w]+ *=/, false)) {
state.javaScriptArguments = true;
state.javaScriptArgumentsDepth = 0;
if (stream.match(/^\+#{/, false)) {
state.mixinCallAfter = true;
return interpolation(stream, state);
function callArguments(stream, state) {
if (state.mixinCallAfter) {
state.mixinCallAfter = false;
if (!stream.match(/^\( *[-\w]+ *=/, false)) {
state.javaScriptArguments = true;
state.javaScriptArgumentsDepth = 0;
function conditional(stream, state) {
if (stream.match(/^(if|unless|else if|else)\b/)) {
state.javaScriptLine = true;
function each(stream, state) {
if (stream.match(/^(- *)?(each|for)\b/)) {
function eachContinued(stream, state) {
if (stream.match(/^ in\b/)) {
state.javaScriptLine = true;
} else if (stream.sol() || stream.eol()) {
} else if (stream.next()) {
while (!stream.match(/^ in\b/, false) && stream.next());
function whileStatement(stream, state) {
if (stream.match(/^while\b/)) {
state.javaScriptLine = true;
function tag(stream, state) {
if (captures = stream.match(/^(\w(?:[-:\w]*\w)?)\/?/)) {
state.lastTag = captures[1].toLowerCase();
if (state.lastTag === 'script') {
state.scriptType = 'application/javascript';
function filter(stream, state) {
if (stream.match(/^:([\w\-]+)/)) {
if (config && config.innerModes) {
innerMode = config.innerModes(stream.current().substring(1));
innerMode = stream.current().substring(1);
if (typeof innerMode === 'string') {
innerMode = CodeMirror.getMode(config, innerMode);
setInnerMode(stream, state, innerMode);
function code(stream, state) {
if (stream.match(/^(!?=|-)/)) {
state.javaScriptLine = true;
if (stream.match(/^#([\w-]+)/)) {
function className(stream) {
if (stream.match(/^\.([\w-]+)/)) {
function attrs(stream, state) {
if (stream.peek() == '(') {
state.inAttributeName = true;
state.attributeIsType = false;
function attrsContinued(stream, state) {
if (ATTRS_NEST[stream.peek()]) {
state.attrsNest.push(ATTRS_NEST[stream.peek()]);
if (state.attrsNest[state.attrsNest.length - 1] === stream.peek()) {
} else if (stream.eat(')')) {
if (state.inAttributeName && stream.match(/^[^=,\)!]+/)) {
if (stream.peek() === '=' || stream.peek() === '!') {
state.inAttributeName = false;
state.jsState = CodeMirror.startState(jsMode);
if (state.lastTag === 'script' && stream.current().trim().toLowerCase() === 'type') {
state.attributeIsType = true;
state.attributeIsType = false;
var tok = jsMode.token(stream, state.jsState);
if (state.attributeIsType && tok === 'string') {
state.scriptType = stream.current().toString();
if (state.attrsNest.length === 0 && (tok === 'string' || tok === 'variable' || tok === 'keyword')) {
Function('', 'var x ' + state.attrValue.replace(/,\s*$/, '').replace(/^!/, ''));
state.inAttributeName = true;
stream.backUp(stream.current().length);
return attrsContinued(stream, state);
//not the end of an attribute
state.attrValue += stream.current();
function attributesBlock(stream, state) {
if (stream.match(/^&attributes\b/)) {
state.javaScriptArguments = true;
state.javaScriptArgumentsDepth = 0;
function indent(stream) {
if (stream.sol() && stream.eatSpace()) {
function comment(stream, state) {
if (stream.match(/^ *\/\/(-)?([^\n]*)/)) {
state.indentOf = stream.indentation();
state.indentToken = 'comment';
if (stream.match(/^: */)) {
function text(stream, state) {
if (stream.match(/^(?:\| ?| )([^\n]+)/)) {
if (stream.match(/^(<[^\n]*)/, false)) {
setInnerMode(stream, state, 'htmlmixed');
state.innerModeForLine = true;
return innerMode(stream, state, true);
function dot(stream, state) {
if (state.lastTag === 'script' && state.scriptType.toLowerCase().indexOf('javascript') != -1) {
innerMode = state.scriptType.toLowerCase().replace(/"|'/g, '');
} else if (state.lastTag === 'style') {
setInnerMode(stream, state, innerMode);
function setInnerMode(stream, state, mode) {
mode = CodeMirror.mimeModes[mode] || mode;
mode = config.innerModes ? config.innerModes(mode) || mode : mode;
mode = CodeMirror.mimeModes[mode] || mode;
mode = CodeMirror.getMode(config, mode);
state.indentOf = stream.indentation();
if (mode && mode.name !== 'null') {
state.indentToken = 'string';
function innerMode(stream, state, force) {
if (stream.indentation() > state.indentOf || (state.innerModeForLine && !stream.sol()) || force) {
state.innerState = state.innerMode.startState ? CodeMirror.startState(state.innerMode, stream.indentation()) : {};
return stream.hideFirstChars(state.indentOf + 2, function () {
return state.innerMode.token(stream, state.innerState) || true;