Skip to content

Instantly share code, notes, and snippets.

@wiserweb
Last active July 26, 2016 17:22
Show Gist options
  • Save wiserweb/ce73e663a15930018723a311a5649e8b to your computer and use it in GitHub Desktop.
Save wiserweb/ce73e663a15930018723a311a5649e8b to your computer and use it in GitHub Desktop.
This is an ace ide rule that allows for mixed syntax highlighting of handlebars and html in a .html document. (https://github.com/ajaxorg/ace) : Tested here: https://ace.c9.io/tool/mode_creator.html
define(function(require, exports, module) {
"use strict";
var oop = require("../lib/oop");
var lang = require("../lib/lang");
var CssHighlightRules = require("./css_highlight_rules").CssHighlightRules;
var JavaScriptHighlightRules = require("./javascript_highlight_rules").JavaScriptHighlightRules;
var XmlHighlightRules = require("./xml_highlight_rules").XmlHighlightRules;
var tagMap = lang.createMap({
a : 'anchor',
button : 'form',
form : 'form',
img : 'image',
input : 'form',
label : 'form',
option : 'form',
script : 'script',
select : 'form',
textarea : 'form',
style : 'style',
table : 'table',
tbody : 'table',
td : 'table',
tfoot : 'table',
th : 'table',
tr : 'table'
});
function pop2(currentState, stack) {
stack.splice(0, 3);
return stack.shift() || "start";
}
var HtmlHighlightRules = function() {
XmlHighlightRules.call(this);
this.addRules({
attributes: [{
include : "tag_whitespace"
}, {
token : "entity.other.attribute-name.xml",
regex : "[-_a-zA-Z0-9:.]+"
}, {
token : "keyword.operator.attribute-equals.xml",
regex : "=",
push : [{
include: "tag_whitespace"
}, {
token : "string.unquoted.attribute-value.html",
regex : "[^<>='\"`\\s]+",
next : "pop"
}, {
token : "empty",
regex : "",
next : "pop"
}]
}, {
include : "attribute_value"
}],
tag: [{
token : function(start, tag) {
var group = tagMap[tag];
return ["meta.tag.punctuation." + (start == "<" ? "" : "end-") + "tag-open.xml",
"meta.tag" + (group ? "." + group : "") + ".tag-name.xml"];
},
regex : "(</?)([-_a-zA-Z0-9:.]+)",
next: "tag_stuff"
}],
tag_stuff: [
{include : "attributes"},
{token : "meta.tag.punctuation.tag-close.xml", regex : "/?>", next : "start"}
]
});
var hbs = {
regex : "(?={{)",
push : "handlebars"
};
for (var key in this.$rules) {
this.$rules[key].unshift(hbs);
}
this.$rules.handlebars = [{
token : "comment.start",
regex : "{{!--",
push : [{
token : "comment.end",
regex : "--}}",
next : pop2
}, {
defaultToken : "comment"
}]
}, {
token : "comment.start",
regex : "{{!",
push : [{
token : "comment.end",
regex : "}}",
next : pop2
}, {
defaultToken : "comment"
}]
}, {
token : "support.function", // unescaped variable
regex : "{{{",
push : [{
token : "support.function",
regex : "}}}",
next : pop2
}, {
token : "variable.parameter",
regex : "[a-zA-Z_$][a-zA-Z0-9_$]*"
}]
}, {
token : "storage.type.start", // begin section
regex : "{{[#\\^/&]?",
push : [{
token : "storage.type.end",
regex : "}}",
next : pop2
}, {
token : "variable.parameter",
regex : "[a-zA-Z_$][a-zA-Z0-9_$]*"
}]
}];
this.embedTagRules(CssHighlightRules, "css-", "style");
this.embedTagRules(new JavaScriptHighlightRules({noJSX: true}).getRules(), "js-", "script");
if (this.constructor === HtmlHighlightRules)
this.normalizeRules();
};
oop.inherits(HtmlHighlightRules, XmlHighlightRules);
exports.HtmlHighlightRules = HtmlHighlightRules;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment