Created
February 12, 2015 23:13
-
-
Save 3cp/13b2f748319fb4c2172e to your computer and use it in GitHub Desktop.
ace syntax highlighting example (pdflib + liquid mix)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ace.define('ace/mode/pdftemplate', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/mode/text', 'ace/tokenizer', 'ace/mode/pdftemplate_highlight_rules'], function(require, exports, module) { | |
var oop = require("../lib/oop"); | |
var TextMode = require("./text").Mode; | |
var Tokenizer = require("../tokenizer").Tokenizer; | |
var PdfTemplateHighlightRules = require("./pdftemplate_highlight_rules").PdfTemplateHighlightRules; | |
var Mode = function() { | |
this.$tokenizer = new Tokenizer(new PdfTemplateHighlightRules().getRules()); | |
}; | |
oop.inherits(Mode, TextMode); | |
exports.Mode = Mode; | |
}); | |
ace.define('ace/mode/pdftemplate_highlight_rules', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/lib/text_highlight_rules'], function(require, exports, module) { | |
var oop = require("../lib/oop"); | |
var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules; | |
var PdfTemplateHighlightRules = function() { | |
var functions = ( | |
"date|capitalize|downcase|upcase|first|last|join|sort|map|size|escape|" + | |
"escape_once|strip_html|strip_newlines|newline_to_br|replace|replace_first|" + | |
"truncate|truncatewords|prepend|append|minus|plus|times|divided_by|split" | |
); | |
var keywords = ( | |
"capture|endcapture|case|endcase|when|comment|endcomment|" + | |
"cycle|for|endfor|in|reversed|if|endif|else|elsif|include|endinclude|unless|endunless|" + | |
"style|text|image|widget|plugin|marker|endmarker|tablerow|endtablerow|blank|empty|nil|null" | |
); | |
var builtinVariables = 'forloop|tablerowloop'; | |
var definitions = ("assign"); | |
var keywordMapper = this.createKeywordMapper({ | |
"variable.language": builtinVariables, | |
"keyword": keywords, | |
"support.function": functions, | |
"keyword.definition": definitions | |
}, "identifier"); | |
this.$rules = { | |
start : [{ | |
token : "meta.tag", | |
regex : /{(%|{)/, | |
push : "liquid_start" | |
}, { | |
token : "meta.tag", | |
regex : "<", | |
next : "pdflib" | |
}, { | |
token : "constant.character.escape", | |
regex : /\\\\/ | |
}, { | |
token : "constant.character.escape", | |
regex : /\\t/ | |
}, { | |
token : "constant.character.escape", | |
regex : /\\n/ | |
} ], | |
pdflib : [ { | |
token : "meta.tag", | |
regex : ">", | |
next : "start" | |
}, { | |
token : "meta.tag", | |
regex : /{(%|{)/, | |
push : "liquid_start" | |
}, { | |
token : "constant.language", | |
regex : /\w+(?==)/ | |
}, { | |
token : "keyword.operator", | |
regex : "=" | |
},{ | |
token : "meta.tag", | |
regex : "{", | |
push : "pdflib_group_value" | |
} ], | |
// this can be nested, need to call normalizeRules after. | |
pdflib_group_value : [{ | |
defaultToken : "string" | |
}, { | |
token : "meta.tag", | |
regex : /{(%|{)/, | |
push : "liquid_start" | |
}, { | |
token : "meta.tag", | |
regex : "{", | |
push: "pdflib_group_value", | |
}, { | |
token : "meta.tag", | |
regex : "}", | |
next : "pop" | |
}], | |
liquid_start : [{ | |
token: "meta.tag", | |
regex: /(%|})}/, | |
next : "pop" | |
}, { | |
token : "string", // single line | |
regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]' | |
}, { | |
token : "string", // single line | |
regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']" | |
}, { | |
token : "constant.numeric", // hex | |
regex : "0[xX][0-9a-fA-F]+\\b" | |
}, { | |
token : "constant.numeric", // float | |
regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b" | |
}, { | |
token : "constant.language.boolean", | |
regex : "(?:true|false)\\b" | |
}, { | |
token : keywordMapper, | |
regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b" | |
}, { | |
token : "keyword.operator", | |
regex : "\/|\\*|\\-|\\+|=|!=|\\?\\:" | |
}, { | |
token : "paren.lparen", | |
regex : /[\[\({]/ | |
}, { | |
token : "paren.rparen", | |
regex : /[\])}]/ | |
}, { | |
token : "text", | |
regex : "\\s+" | |
}] | |
}; | |
this.normalizeRules(); | |
}; | |
oop.inherits(PdfTemplateHighlightRules, TextHighlightRules); | |
exports.PdfTemplateHighlightRules = PdfTemplateHighlightRules; | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment