[d24f17c] | 1 | 'use strict'
|
---|
| 2 | var refractorMarkupTemplating = require('./markup-templating.js')
|
---|
| 3 | module.exports = ftl
|
---|
| 4 | ftl.displayName = 'ftl'
|
---|
| 5 | ftl.aliases = []
|
---|
| 6 | function ftl(Prism) {
|
---|
| 7 | Prism.register(refractorMarkupTemplating)
|
---|
| 8 | ;(function (Prism) {
|
---|
| 9 | // https://freemarker.apache.org/docs/dgui_template_exp.html
|
---|
| 10 | // FTL expression with 4 levels of nesting supported
|
---|
| 11 | var FTL_EXPR =
|
---|
| 12 | /[^<()"']|\((?:<expr>)*\)|<(?!#--)|<#--(?:[^-]|-(?!->))*-->|"(?:[^\\"]|\\.)*"|'(?:[^\\']|\\.)*'/
|
---|
| 13 | .source
|
---|
| 14 | for (var i = 0; i < 2; i++) {
|
---|
| 15 | FTL_EXPR = FTL_EXPR.replace(/<expr>/g, function () {
|
---|
| 16 | return FTL_EXPR
|
---|
| 17 | })
|
---|
| 18 | }
|
---|
| 19 | FTL_EXPR = FTL_EXPR.replace(/<expr>/g, /[^\s\S]/.source)
|
---|
| 20 | var ftl = {
|
---|
| 21 | comment: /<#--[\s\S]*?-->/,
|
---|
| 22 | string: [
|
---|
| 23 | {
|
---|
| 24 | // raw string
|
---|
| 25 | pattern: /\br("|')(?:(?!\1)[^\\]|\\.)*\1/,
|
---|
| 26 | greedy: true
|
---|
| 27 | },
|
---|
| 28 | {
|
---|
| 29 | pattern: RegExp(
|
---|
| 30 | /("|')(?:(?!\1|\$\{)[^\\]|\\.|\$\{(?:(?!\})(?:<expr>))*\})*\1/.source.replace(
|
---|
| 31 | /<expr>/g,
|
---|
| 32 | function () {
|
---|
| 33 | return FTL_EXPR
|
---|
| 34 | }
|
---|
| 35 | )
|
---|
| 36 | ),
|
---|
| 37 | greedy: true,
|
---|
| 38 | inside: {
|
---|
| 39 | interpolation: {
|
---|
| 40 | pattern: RegExp(
|
---|
| 41 | /((?:^|[^\\])(?:\\\\)*)\$\{(?:(?!\})(?:<expr>))*\}/.source.replace(
|
---|
| 42 | /<expr>/g,
|
---|
| 43 | function () {
|
---|
| 44 | return FTL_EXPR
|
---|
| 45 | }
|
---|
| 46 | )
|
---|
| 47 | ),
|
---|
| 48 | lookbehind: true,
|
---|
| 49 | inside: {
|
---|
| 50 | 'interpolation-punctuation': {
|
---|
| 51 | pattern: /^\$\{|\}$/,
|
---|
| 52 | alias: 'punctuation'
|
---|
| 53 | },
|
---|
| 54 | rest: null
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | ],
|
---|
| 60 | keyword: /\b(?:as)\b/,
|
---|
| 61 | boolean: /\b(?:false|true)\b/,
|
---|
| 62 | 'builtin-function': {
|
---|
| 63 | pattern: /((?:^|[^?])\?\s*)\w+/,
|
---|
| 64 | lookbehind: true,
|
---|
| 65 | alias: 'function'
|
---|
| 66 | },
|
---|
| 67 | function: /\b\w+(?=\s*\()/,
|
---|
| 68 | number: /\b\d+(?:\.\d+)?\b/,
|
---|
| 69 | operator:
|
---|
| 70 | /\.\.[<*!]?|->|--|\+\+|&&|\|\||\?{1,2}|[-+*/%!=<>]=?|\b(?:gt|gte|lt|lte)\b/,
|
---|
| 71 | punctuation: /[,;.:()[\]{}]/
|
---|
| 72 | }
|
---|
| 73 | ftl.string[1].inside.interpolation.inside.rest = ftl
|
---|
| 74 | Prism.languages.ftl = {
|
---|
| 75 | 'ftl-comment': {
|
---|
| 76 | // the pattern is shortened to be more efficient
|
---|
| 77 | pattern: /^<#--[\s\S]*/,
|
---|
| 78 | alias: 'comment'
|
---|
| 79 | },
|
---|
| 80 | 'ftl-directive': {
|
---|
| 81 | pattern: /^<[\s\S]+>$/,
|
---|
| 82 | inside: {
|
---|
| 83 | directive: {
|
---|
| 84 | pattern: /(^<\/?)[#@][a-z]\w*/i,
|
---|
| 85 | lookbehind: true,
|
---|
| 86 | alias: 'keyword'
|
---|
| 87 | },
|
---|
| 88 | punctuation: /^<\/?|\/?>$/,
|
---|
| 89 | content: {
|
---|
| 90 | pattern: /\s*\S[\s\S]*/,
|
---|
| 91 | alias: 'ftl',
|
---|
| 92 | inside: ftl
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | },
|
---|
| 96 | 'ftl-interpolation': {
|
---|
| 97 | pattern: /^\$\{[\s\S]*\}$/,
|
---|
| 98 | inside: {
|
---|
| 99 | punctuation: /^\$\{|\}$/,
|
---|
| 100 | content: {
|
---|
| 101 | pattern: /\s*\S[\s\S]*/,
|
---|
| 102 | alias: 'ftl',
|
---|
| 103 | inside: ftl
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | Prism.hooks.add('before-tokenize', function (env) {
|
---|
| 109 | // eslint-disable-next-line regexp/no-useless-lazy
|
---|
| 110 | var pattern = RegExp(
|
---|
| 111 | /<#--[\s\S]*?-->|<\/?[#@][a-zA-Z](?:<expr>)*?>|\$\{(?:<expr>)*?\}/.source.replace(
|
---|
| 112 | /<expr>/g,
|
---|
| 113 | function () {
|
---|
| 114 | return FTL_EXPR
|
---|
| 115 | }
|
---|
| 116 | ),
|
---|
| 117 | 'gi'
|
---|
| 118 | )
|
---|
| 119 | Prism.languages['markup-templating'].buildPlaceholders(
|
---|
| 120 | env,
|
---|
| 121 | 'ftl',
|
---|
| 122 | pattern
|
---|
| 123 | )
|
---|
| 124 | })
|
---|
| 125 | Prism.hooks.add('after-tokenize', function (env) {
|
---|
| 126 | Prism.languages['markup-templating'].tokenizePlaceholders(env, 'ftl')
|
---|
| 127 | })
|
---|
| 128 | })(Prism)
|
---|
| 129 | }
|
---|