1 | 'use strict'
|
---|
2 | var refractorMarkupTemplating = require('./markup-templating.js')
|
---|
3 | module.exports = smarty
|
---|
4 | smarty.displayName = 'smarty'
|
---|
5 | smarty.aliases = []
|
---|
6 | function smarty(Prism) {
|
---|
7 | Prism.register(refractorMarkupTemplating)
|
---|
8 | ;(function (Prism) {
|
---|
9 | Prism.languages.smarty = {
|
---|
10 | comment: {
|
---|
11 | pattern: /^\{\*[\s\S]*?\*\}/,
|
---|
12 | greedy: true
|
---|
13 | },
|
---|
14 | 'embedded-php': {
|
---|
15 | pattern: /^\{php\}[\s\S]*?\{\/php\}/,
|
---|
16 | greedy: true,
|
---|
17 | inside: {
|
---|
18 | smarty: {
|
---|
19 | pattern: /^\{php\}|\{\/php\}$/,
|
---|
20 | inside: null // see below
|
---|
21 | },
|
---|
22 | php: {
|
---|
23 | pattern: /[\s\S]+/,
|
---|
24 | alias: 'language-php',
|
---|
25 | inside: Prism.languages.php
|
---|
26 | }
|
---|
27 | }
|
---|
28 | },
|
---|
29 | string: [
|
---|
30 | {
|
---|
31 | pattern: /"(?:\\.|[^"\\\r\n])*"/,
|
---|
32 | greedy: true,
|
---|
33 | inside: {
|
---|
34 | interpolation: {
|
---|
35 | pattern: /\{[^{}]*\}|`[^`]*`/,
|
---|
36 | inside: {
|
---|
37 | 'interpolation-punctuation': {
|
---|
38 | pattern: /^[{`]|[`}]$/,
|
---|
39 | alias: 'punctuation'
|
---|
40 | },
|
---|
41 | expression: {
|
---|
42 | pattern: /[\s\S]+/,
|
---|
43 | inside: null // see below
|
---|
44 | }
|
---|
45 | }
|
---|
46 | },
|
---|
47 | variable: /\$\w+/
|
---|
48 | }
|
---|
49 | },
|
---|
50 | {
|
---|
51 | pattern: /'(?:\\.|[^'\\\r\n])*'/,
|
---|
52 | greedy: true
|
---|
53 | }
|
---|
54 | ],
|
---|
55 | keyword: {
|
---|
56 | pattern: /(^\{\/?)[a-z_]\w*\b(?!\()/i,
|
---|
57 | lookbehind: true,
|
---|
58 | greedy: true
|
---|
59 | },
|
---|
60 | delimiter: {
|
---|
61 | pattern: /^\{\/?|\}$/,
|
---|
62 | greedy: true,
|
---|
63 | alias: 'punctuation'
|
---|
64 | },
|
---|
65 | number: /\b0x[\dA-Fa-f]+|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:[Ee][-+]?\d+)?/,
|
---|
66 | variable: [
|
---|
67 | /\$(?!\d)\w+/,
|
---|
68 | /#(?!\d)\w+#/,
|
---|
69 | {
|
---|
70 | pattern: /(\.|->|\w\s*=)(?!\d)\w+\b(?!\()/,
|
---|
71 | lookbehind: true
|
---|
72 | },
|
---|
73 | {
|
---|
74 | pattern: /(\[)(?!\d)\w+(?=\])/,
|
---|
75 | lookbehind: true
|
---|
76 | }
|
---|
77 | ],
|
---|
78 | function: {
|
---|
79 | pattern: /(\|\s*)@?[a-z_]\w*|\b[a-z_]\w*(?=\()/i,
|
---|
80 | lookbehind: true
|
---|
81 | },
|
---|
82 | 'attr-name': /\b[a-z_]\w*(?=\s*=)/i,
|
---|
83 | boolean: /\b(?:false|no|off|on|true|yes)\b/,
|
---|
84 | punctuation: /[\[\](){}.,:`]|->/,
|
---|
85 | operator: [
|
---|
86 | /[+\-*\/%]|==?=?|[!<>]=?|&&|\|\|?/,
|
---|
87 | /\bis\s+(?:not\s+)?(?:div|even|odd)(?:\s+by)?\b/,
|
---|
88 | /\b(?:and|eq|gt?e|gt|lt?e|lt|mod|neq?|not|or)\b/
|
---|
89 | ]
|
---|
90 | }
|
---|
91 | Prism.languages.smarty['embedded-php'].inside.smarty.inside =
|
---|
92 | Prism.languages.smarty
|
---|
93 | Prism.languages.smarty.string[0].inside.interpolation.inside.expression.inside =
|
---|
94 | Prism.languages.smarty
|
---|
95 | var string = /"(?:\\.|[^"\\\r\n])*"|'(?:\\.|[^'\\\r\n])*'/
|
---|
96 | var smartyPattern = RegExp(
|
---|
97 | // comments
|
---|
98 | /\{\*[\s\S]*?\*\}/.source +
|
---|
99 | '|' + // php tags
|
---|
100 | /\{php\}[\s\S]*?\{\/php\}/.source +
|
---|
101 | '|' + // smarty blocks
|
---|
102 | /\{(?:[^{}"']|<str>|\{(?:[^{}"']|<str>|\{(?:[^{}"']|<str>)*\})*\})*\}/.source.replace(
|
---|
103 | /<str>/g,
|
---|
104 | function () {
|
---|
105 | return string.source
|
---|
106 | }
|
---|
107 | ),
|
---|
108 | 'g'
|
---|
109 | ) // Tokenize all inline Smarty expressions
|
---|
110 | Prism.hooks.add('before-tokenize', function (env) {
|
---|
111 | var smartyLiteralStart = '{literal}'
|
---|
112 | var smartyLiteralEnd = '{/literal}'
|
---|
113 | var smartyLiteralMode = false
|
---|
114 | Prism.languages['markup-templating'].buildPlaceholders(
|
---|
115 | env,
|
---|
116 | 'smarty',
|
---|
117 | smartyPattern,
|
---|
118 | function (match) {
|
---|
119 | // Smarty tags inside {literal} block are ignored
|
---|
120 | if (match === smartyLiteralEnd) {
|
---|
121 | smartyLiteralMode = false
|
---|
122 | }
|
---|
123 | if (!smartyLiteralMode) {
|
---|
124 | if (match === smartyLiteralStart) {
|
---|
125 | smartyLiteralMode = true
|
---|
126 | }
|
---|
127 | return true
|
---|
128 | }
|
---|
129 | return false
|
---|
130 | }
|
---|
131 | )
|
---|
132 | }) // Re-insert the tokens after tokenizing
|
---|
133 | Prism.hooks.add('after-tokenize', function (env) {
|
---|
134 | Prism.languages['markup-templating'].tokenizePlaceholders(env, 'smarty')
|
---|
135 | })
|
---|
136 | })(Prism)
|
---|
137 | }
|
---|