Merge pull request #1289 from nguillaumin/freemarker-mode

Added mode for FreeMarker Java template engine
This commit is contained in:
Harutyun Amirjanyan 2013-03-10 05:57:02 -07:00
commit 212214dfad
5 changed files with 288 additions and 0 deletions

View file

@ -76,6 +76,7 @@ var docs = {
"docs/dart.dart": "Dart",
"docs/diff.diff": "Diff",
"docs/dot.dot": "Dot",
"docs/freemarker.ftl" : "FreeMarker",
"docs/glsl.glsl": "Glsl",
"docs/golang.go": "Go",
"docs/groovy.groovy": "Groovy",

View file

@ -0,0 +1,46 @@
<#ftl encoding="utf-8" />
<#setting locale="en_US" />
<#import "library" as lib />
<#--
FreeMarker comment
${abc} <#assign a=12 />
-->
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8" />
<title>${title!"FreeMarker"}<title>
</head>
<body>
<h1>Hello ${name!""}</h1>
<p>Today is: ${.now?date}</p>
<#assign x = 13>
<#if x &gt; 12 && x lt 14>x equals 13: ${x}</#if>
<ul>
<#list items as item>
<li>${item_index}: ${item.name!?split("\n")[0]}</li>
</#list>
</ul>
User directive: <@lib.function attr1=true attr2='value' attr3=-42.12>Test</@lib.function>
<@anotherOne />
<#if variable?exists>
Deprecated
<#elseif variable??>
Better
<#else>
Default
</#if>
<img src="images/${user.id}.png" />
</body>
</html>

View file

@ -46,6 +46,7 @@ var modesByName = {
dart: ["Dart" , "dart"],
diff: ["Diff" , "diff|patch"],
dot: ["Dot" , "dot"],
ftl: ["FreeMarker" , "ftl"],
glsl: ["Glsl" , "glsl|frag|vert"],
golang: ["Go" , "go"],
groovy: ["Groovy" , "groovy"],

50
lib/ace/mode/ftl.js Normal file
View file

@ -0,0 +1,50 @@
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2010, Ajax.org B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) {
"use strict";
var oop = require("../lib/oop");
var TextMode = require("./text").Mode;
var Tokenizer = require("../tokenizer").Tokenizer;
var FtlHighlightRules = require("./ftl_highlight_rules").FtlHighlightRules;
var Mode = function() {
var highlighter = new FtlHighlightRules();
this.$tokenizer = new Tokenizer(highlighter.getRules());
};
oop.inherits(Mode, TextMode);
(function() {
}).call(Mode.prototype);
exports.Mode = Mode;
});

View file

@ -0,0 +1,190 @@
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2010, Ajax.org B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) {
"use strict";
var oop = require("../lib/oop");
var HtmlHighlightRules = require("./html_highlight_rules").HtmlHighlightRules;
var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
var FtlLangHighlightRules = function () {
var stringBuiltIns = "\\?|substring|cap_first|uncap_first|capitalize|chop_linebreak|date|time|datetime|"
+ "ends_with|html|groups|index_of|j_string|js_string|json_string|last_index_of|length|lower_case|"
+ "left_pad|right_pad|contains|matches|number|replace|rtf|url|split|starts_with|string|trim|"
+ "upper_case|word_list|xhtml|xml";
var numberBuiltIns = "c|round|floor|ceiling";
var dateBuiltIns = "iso_[a-z_]+";
var seqBuiltIns = "first|last|seq_contains|seq_index_of|seq_last_index_of|reverse|size|sort|sort_by|chunk";
var hashBuiltIns = "keys|values";
var xmlBuiltIns = "children|parent|root|ancestors|node_name|node_type|node_namespace";
var expertBuiltIns = "byte|double|float|int|long|short|number_to_date|number_to_time|number_to_datetime|"
+ "eval|has_content|interpret|is_[a-z_]+|namespacenew";
var allBuiltIns = stringBuiltIns + numberBuiltIns + dateBuiltIns + seqBuiltIns + hashBuiltIns
+ xmlBuiltIns + expertBuiltIns;
var deprecatedBuiltIns = "default|exists|if_exists|web_safe";
var variables = "data_model|error|globals|lang|locale|locals|main|namespace|node|current_node|"
+ "now|output_encoding|template_name|url_escaping_charset|vars|version";
var operators = "gt|gte|lt|lte|as|in|using";
var reserved = "true|false";
var attributes = "encoding|parse|locale|number_format|date_format|time_format|datetime_format|time_zone|"
+ "url_escaping_charset|classic_compatible|strip_whitespace|strip_text|strict_syntax|ns_prefixes|"
+ "attributes";
this.$rules = {
"start" : [{
token : "constant.character.entity",
regex : /&[^;]+;/
}, {
token : "support.function",
regex : "\\?("+allBuiltIns+")"
}, {
token : "support.function.deprecated",
regex : "\\?("+deprecatedBuiltIns+")"
}, {
token : "language.variable",
regex : "\\.(?:"+variables+")"
}, {
token : "constant.language",
regex : "\\b("+reserved+")\\b"
}, {
token : "keyword.operator",
regex : "\\b(?:"+operators+")\\b"
}, {
token : "entity.other.attribute-name",
regex : attributes
}, {
token : "string", //
regex : /['"]/,
next : "qstring"
}, {
// Deal with variable names that contains number
// e.g. <#if var42 == 42 >
token : function(value) {
if (value.match("^[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?$")) {
return "constant.numeric";
} else {
return "variable";
}
},
regex : /[\w.+\-]+/
}, {
token : "keyword.operator",
regex : "!|\\.|\\$|%|&|\\*|\\-\\-|\\-|\\+\\+|\\+|~|===|==|=|!=|!==|<=|>=|<<=|>>=|>>>=|<>|<|>|&&|\\|\\||\\?\\:|\\*=|%=|\\+=|\\-=|&=|\\^="
}, {
token : "paren.lparen",
regex : "[[({]"
}, {
token : "paren.rparen",
regex : "[\\])}]"
}, {
token : "text",
regex : "\\s+"
}],
"qstring" : [{
token : "constant.character.escape",
regex : '\\\\[nrtvef\\\\"$]'
}, {
token : "string",
regex : /['"]/,
next : "start"
}, {
defaultToken : "string"
}]
};
};
oop.inherits(FtlLangHighlightRules, TextHighlightRules);
var FtlHighlightRules = function() {
var directives = "assign|attempt|break|case|compress|default|elseif|else|escape|fallback|function|flush|"
+ "ftl|global|if|import|include|list|local|lt|macro|nested|noescape|noparse|nt|recover|recurse|return|rt|"
+ "setting|stop|switch|t|visit";
HtmlHighlightRules.call(this);
for (var i in this.$rules) {
this.$rules[i].unshift({
token : "string.interpolated",
regex : "\\${",
push : "ftl-start"
}, {
token : "keyword.function",
regex : "</?#("+directives+")",
push : "ftl-start"
}, {
token : "keyword.other",
regex : "</?@[a-zA-Z\\.]+",
push : "ftl-start"
});
}
this.embedRules(FtlLangHighlightRules, "ftl-");
this.$rules["ftl-start"].unshift({
token : "keyword",
regex : "/?>",
next : "pop"
}, {
token : "string.interpolated",
regex : "}",
next : "pop"
});
this.$rules.start.unshift({
token : "comment",
regex : "<#--",
next : "comment"
});
this.$rules.comment.unshift({
token : "comment",
regex : ".*?-->",
next : "start"
}, {
token : "comment",
regex : ".+"
});
this.normalizeRules();
};
oop.inherits(FtlHighlightRules, HtmlHighlightRules);
exports.FtlHighlightRules = FtlHighlightRules;
});