Skip to content

Commit

Permalink
Added support for FTL (#2080)
Browse files Browse the repository at this point in the history
This adds support for FTL (Freemarker language).
  • Loading branch information
RunDevelopment committed Oct 16, 2019
1 parent fdb7de0 commit 2f3da7e
Show file tree
Hide file tree
Showing 19 changed files with 740 additions and 3 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@
"title": "Fortran",
"owner": "Golmote"
},
"ftl": {
"title": "FreeMarker Template Language",
"require": "markup-templating",
"owner": "RunDevelopment"
},
"gcode": {
"title": "G-code",
"owner": "RunDevelopment"
Expand Down
97 changes: 97 additions & 0 deletions components/prism-ftl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
(function (Prism) {

// https://freemarker.apache.org/docs/dgui_template_exp.html

// FTL expression with 4 levels of nesting supported
var FTL_EXPR = /(?!<#--)[^()"']|\((?:<expr>)*\)|<#--[\s\S]*?-->|"(?:[^\\"]|\\.)*"|'(?:[^\\']|\\.)*'/.source;
for (var i = 0; i < 2; i++) {
FTL_EXPR = FTL_EXPR.replace(/<expr>/g, FTL_EXPR);
}
FTL_EXPR = FTL_EXPR.replace(/<expr>/g, '[^\s\S]');

var ftl = {
'comment': /<#--[\s\S]*?-->/,
'string': [
{
// raw string
pattern: /\br("|')(?:(?!\1)[^\\]|\\.)*\1/,
greedy: true
},
{
pattern: RegExp(/("|')(?:(?!\1|\$\{)[^\\]|\\.|\$\{(?:<expr>)*?\})*\1/.source.replace(/<expr>/g, FTL_EXPR)),
greedy: true,
inside: {
'interpolation': {
pattern: RegExp(/((?:^|[^\\])(?:\\\\)*)\$\{(?:<expr>)*?\}/.source.replace(/<expr>/g, FTL_EXPR)),
lookbehind: true,
inside: {
'interpolation-punctuation': {
pattern: /^\$\{|\}$/,
alias: 'punctuation'
},
rest: null
}
}
}
}
],
'keyword': /\b(?:as)\b/,
'boolean': /\b(?:true|false)\b/,
'builtin-function': {
pattern: /((?:^|[^?])\?\s*)\w+/,
lookbehind: true,
alias: 'function'
},
'function': /\w+(?=\s*\()/,
'number': /\d+(?:\.\d+)?/,
'operator': /\.\.[<*!]?|->|--|\+\+|&&|\|\||\?{1,2}|[-+*/%!=<>]=?|\b(?:gt|gte|lt|lte)\b/,
'punctuation': /[,;.:()[\]{}]/
};

ftl.string[1].inside.interpolation.inside.rest = ftl;

Prism.languages.ftl = {
'ftl-comment': {
// the pattern is shortened to be more efficient
pattern: /^<#--[\s\S]*/,
alias: 'comment'
},
'ftl-directive': {
pattern: /^<[\s\S]+>$/,
inside: {
'directive': {
pattern: /(^<\/?)[#@][a-z]\w*/i,
lookbehind: true,
alias: 'keyword'
},
'punctuation': /^<\/?|\/?>$/,
'content': {
pattern: /[\s\S]*\S[\s\S]*/,
alias: 'ftl',
inside: ftl
}
}
},
'ftl-interpolation': {
pattern: /^\$\{[\s\S]*\}$/,
inside: {
'punctuation': /^\$\{|\}$/,
'content': {
pattern: /[\s\S]*\S[\s\S]*/,
alias: 'ftl',
inside: ftl
}
}
}
};

Prism.hooks.add('before-tokenize', function (env) {
var pattern = RegExp(/<#--[\s\S]*?-->|<\/?[#@][a-zA-Z](?:<expr>)*?>|\$\{(?:<expr>)*?\}/.source.replace(/<expr>/g, FTL_EXPR), 'gi');
Prism.languages['markup-templating'].buildPlaceholders(env, 'ftl', pattern);
});

Prism.hooks.add('after-tokenize', function (env) {
Prism.languages['markup-templating'].tokenizePlaceholders(env, 'ftl');
});

}(Prism));
1 change: 1 addition & 0 deletions components/prism-ftl.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions examples/prism-ftl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<h2>Full example</h2>
<pre><code>&lt;html>
&lt;head>
&lt;title>Welcome!&lt;/title>
&lt;/head>
&lt;body>
&lt;h1>
Welcome ${user}&lt;#if user == "Big Joe">, our beloved leader&lt;/#if>!
&lt;/h1>
&lt;p>Our latest product:
&lt;a href="${latestProduct.url}">${latestProduct.name}&lt;/a>!
&lt;p>See what our happy customers have to say!&lt;/p>
&lt;ul>
&lt;#list userStories as story>
&lt;li>
&lt;p>${story.text?esc} - by &lt;span>${story.user.name}&lt;/span>
&lt;li>
&lt;/#list>
&lt;/ul>
&lt;/body>
&lt;/html></code></pre>
1 change: 1 addition & 0 deletions plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"fsharp": "clike",
"firestore-security-rules": "clike",
"flow": "javascript",
"ftl": "markup-templating",
"glsl": "clike",
"gml": "clike",
"go": "clike",
Expand Down
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/show-language/prism-show-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"erb": "ERB",
"fsharp": "F#",
"firestore-security-rules": "Firestore security rules",
"ftl": "FreeMarker Template Language",
"gcode": "G-code",
"gdscript": "GDScript",
"gedcom": "GEDCOM",
Expand Down
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2f3da7e

Please sign in to comment.