From 6b168a3b264058ff90ee8b085015cd6fd31ab555 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Sun, 5 Dec 2021 14:46:06 +0100 Subject: [PATCH] Latte: Use standard token names and combined delimiter tokens (#3226) --- components/prism-latte.js | 22 +- components/prism-latte.min.js | 2 +- tests/languages/latte/delimiter_feature.test | 78 ++----- tests/languages/latte/html_feature.test | 203 ++++++++++++++++--- tests/languages/latte/php_feature.test | 105 ++++++++-- 5 files changed, 286 insertions(+), 124 deletions(-) diff --git a/components/prism-latte.js b/components/prism-latte.js index 2826551f8b..dd877a2323 100644 --- a/components/prism-latte.js +++ b/components/prism-latte.js @@ -1,21 +1,15 @@ (function (Prism) { Prism.languages.latte = { 'comment': /^\{\*[\s\S]*/, - 'ld': { - pattern: /^\{(?:[=_]|\/?(?!\d|\w+\()\w+)?/, - inside: { - 'punctuation': /^\{\/?/, - 'tag': { - pattern: /.+/, - alias: 'important' - } - } + 'latte-tag': { + // https://latte.nette.org/en/tags + pattern: /(^\{(?:\/(?=[a-z]))?)(?:[=_]|[a-z]\w*\b(?!\())/i, + lookbehind: true, + alias: 'important' }, - 'rd': { - pattern: /\}$/, - inside: { - 'punctuation': /.+/ - } + 'delimiter': { + pattern: /^\{\/?|\}$/, + alias: 'punctuation' }, 'php': { pattern: /\S(?:[\s\S]*\S)?/, diff --git a/components/prism-latte.min.js b/components/prism-latte.min.js index 2a7e020eb1..5f19298508 100644 --- a/components/prism-latte.min.js +++ b/components/prism-latte.min.js @@ -1 +1 @@ -!function(t){t.languages.latte={comment:/^\{\*[\s\S]*/,ld:{pattern:/^\{(?:[=_]|\/?(?!\d|\w+\()\w+)?/,inside:{punctuation:/^\{\/?/,tag:{pattern:/.+/,alias:"important"}}},rd:{pattern:/\}$/,inside:{punctuation:/.+/}},php:{pattern:/\S(?:[\s\S]*\S)?/,alias:"language-php",inside:t.languages.php}};var e=t.languages.extend("markup",{});t.languages.insertBefore("inside","attr-value",{"n-attr":{pattern:/n:[\w-]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+))?/,inside:{"attr-name":{pattern:/^[^\s=]+/,alias:"important"},"attr-value":{pattern:/=[\s\S]+/,inside:{punctuation:[/^=/,{pattern:/^(\s*)["']|["']$/,lookbehind:!0}],php:{pattern:/\S(?:[\s\S]*\S)?/,inside:t.languages.php}}}}}},e.tag),t.hooks.add("before-tokenize",function(a){if("latte"===a.language){t.languages["markup-templating"].buildPlaceholders(a,"latte",/\{\*[\s\S]*?\*\}|\{[^'"\s{}*](?:[^"'/{}]|\/(?![*/])|("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|\/\*(?:[^*]|\*(?!\/))*\*\/)*\}/g),a.grammar=e}}),t.hooks.add("after-tokenize",function(a){t.languages["markup-templating"].tokenizePlaceholders(a,"latte")})}(Prism); \ No newline at end of file +!function(t){t.languages.latte={comment:/^\{\*[\s\S]*/,"latte-tag":{pattern:/(^\{(?:\/(?=[a-z]))?)(?:[=_]|[a-z]\w*\b(?!\())/i,lookbehind:!0,alias:"important"},delimiter:{pattern:/^\{\/?|\}$/,alias:"punctuation"},php:{pattern:/\S(?:[\s\S]*\S)?/,alias:"language-php",inside:t.languages.php}};var e=t.languages.extend("markup",{});t.languages.insertBefore("inside","attr-value",{"n-attr":{pattern:/n:[\w-]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+))?/,inside:{"attr-name":{pattern:/^[^\s=]+/,alias:"important"},"attr-value":{pattern:/=[\s\S]+/,inside:{punctuation:[/^=/,{pattern:/^(\s*)["']|["']$/,lookbehind:!0}],php:{pattern:/\S(?:[\s\S]*\S)?/,inside:t.languages.php}}}}}},e.tag),t.hooks.add("before-tokenize",function(a){if("latte"===a.language){t.languages["markup-templating"].buildPlaceholders(a,"latte",/\{\*[\s\S]*?\*\}|\{[^'"\s{}*](?:[^"'/{}]|\/(?![*/])|("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|\/\*(?:[^*]|\*(?!\/))*\*\/)*\}/g),a.grammar=e}}),t.hooks.add("after-tokenize",function(a){t.languages["markup-templating"].tokenizePlaceholders(a,"latte")})}(Prism); \ No newline at end of file diff --git a/tests/languages/latte/delimiter_feature.test b/tests/languages/latte/delimiter_feature.test index 4116230304..6c7ddc5e64 100644 --- a/tests/languages/latte/delimiter_feature.test +++ b/tests/languages/latte/delimiter_feature.test @@ -15,100 +15,62 @@ line} [ ["latte", [ - ["ld", [ - ["punctuation", "{"], - ["tag", "aa"] - ]], - ["rd", [ - ["punctuation", "}"] - ]] + ["delimiter", "{"], + ["latte-tag", "aa"], + ["delimiter", "}"] ]], ["latte", [ - ["ld", [ - ["punctuation", "{/"], - ["tag", "aa"] - ]], - ["rd", [ - ["punctuation", "}"] - ]] + ["delimiter", "{/"], + ["latte-tag", "aa"], + ["delimiter", "}"] ]], ["latte", [ - ["ld", [ - ["punctuation", "{"] - ]], - ["php", [ - ["operator", "/"] - ]], - ["rd", [ - ["punctuation", "}"] - ]] + ["delimiter", "{/"], + ["delimiter", "}"] ]], "\r\n{", ["latte", [ - ["ld", [ - ["punctuation", "{"], - ["tag", "aa"] - ]], - ["rd", [ - ["punctuation", "}"] - ]] + ["delimiter", "{"], + ["latte-tag", "aa"], + ["delimiter", "}"] ]], "}\r\n", ["latte", [ - ["ld", [ - ["punctuation", "{"] - ]], + ["delimiter", "{"], ["php", [ ["number", "10"] ]], - ["rd", [ - ["punctuation", "}"] - ]] + ["delimiter", "}"] ]], ["latte", [ - ["ld", [ - ["punctuation", "{"], - ["tag", "="] - ]], + ["delimiter", "{"], + ["latte-tag", "="], ["php", [ ["number", "10"] ]], - ["rd", [ - ["punctuation", "}"] - ]] + ["delimiter", "}"] ]], ["latte", [ - ["ld", [ - ["punctuation", "{"] - ]], + ["delimiter", "{"], ["php", [ ["function", ["test"]], ["punctuation", "("], ["punctuation", ")"] ]], - ["rd", [ - ["punctuation", "}"] - ]] + ["delimiter", "}"] ]], "\r\n{'no'}\r\n{\"no\"}\r\n{ no }\r\n", ["latte", [ - ["ld", [ - ["punctuation", "{"], - ["tag", "multi"] - ]], - - ["php", ["line"]], - ["rd", [ - ["punctuation", "}"] - ]] + ["delimiter", "{"], ["latte-tag", "multi"], + ["php", ["line"]], ["delimiter", "}"] ]] ] diff --git a/tests/languages/latte/html_feature.test b/tests/languages/latte/html_feature.test index d6534973bb..02296ff518 100644 --- a/tests/languages/latte/html_feature.test +++ b/tests/languages/latte/html_feature.test @@ -12,42 +12,179 @@ ---------------------------------------------------- [ - ["tag", [["tag", [["punctuation", "<"], "a"]], - ["attr-name", ["href"]], ["attr-value", [["punctuation", "="], ["punctuation", "\""], - ["latte", [["ld", [["punctuation", "{"], ["tag", "link"]]], - ["php", ["Post", ["punctuation", ":"], ["class-name", "show"], ["variable", "$post"], ["operator", "->"], ["property", "id"]]], - ["rd", [["punctuation", "}"]]]]], - ["punctuation", "\""]]], ["punctuation", ">"]]], - ["latte", [["ld", [["punctuation", "{"]]], ["php", [["variable", "$post"], ["operator", "->"], ["property", "title"]]], ["rd", [["punctuation", "}"]]]]], - ["tag", [["tag", [["punctuation", ""]]], + ["tag", [ + ["tag", [ + ["punctuation", "<"], + "a" + ]], + ["attr-name", ["href"]], + ["attr-value", [ + ["punctuation", "="], + ["punctuation", "\""], + ["latte", [ + ["delimiter", "{"], + ["latte-tag", "link"], + ["php", [ + "Post", + ["punctuation", ":"], + ["class-name", "show"], + ["variable", "$post"], + ["operator", "->"], + ["property", "id"] + ]], + ["delimiter", "}"] + ]], + ["punctuation", "\""] + ]], + ["punctuation", ">"] + ]], + ["latte", [ + ["delimiter", "{"], + ["php", [ + ["variable", "$post"], + ["operator", "->"], + ["property", "title"] + ]], + ["delimiter", "}"] + ]], + ["tag", [ + ["tag", [ + ["punctuation", ""] + ]], - ["tag", [["tag", [["punctuation", "<"], "a"]], - ["attr-name", - [["latte", [["ld", [["punctuation", "{"], ["tag", "if"]]], ["php", [["variable", "$post"], ["operator", "->"], ["property", "id"]]], ["rd", [["punctuation", "}"]]]]], "title"] - ], - ["attr-value", [["punctuation", "="], ["punctuation", "\""], "ahoj", ["punctuation", "\""]]], - ["attr-name", [["latte", [["ld", [["punctuation", "{/"], ["tag", "if"]]], ["rd", [["punctuation", "}"]]]]]]], - ["punctuation", ">"]]], - ["latte", [["ld", [["punctuation", "{"]]], ["php", [["variable", "$post"], ["operator", "->"], ["property", "title"]]], ["rd", [["punctuation", "}"]]]]], - ["tag", [["tag", [["punctuation", ""]]], + ["tag", [ + ["tag", [ + ["punctuation", "<"], + "a" + ]], + ["attr-name", [ + ["latte", [ + ["delimiter", "{"], + ["latte-tag", "if"], + ["php", [ + ["variable", "$post"], + ["operator", "->"], + ["property", "id"] + ]], + ["delimiter", "}"] + ]], + "title" + ]], + ["attr-value", [ + ["punctuation", "="], + ["punctuation", "\""], + "ahoj", + ["punctuation", "\""] + ]], + ["attr-name", [ + ["latte", [ + ["delimiter", "{/"], + ["latte-tag", "if"], + ["delimiter", "}"] + ]] + ]], + ["punctuation", ">"] + ]], + ["latte", [ + ["delimiter", "{"], + ["php", [ + ["variable", "$post"], + ["operator", "->"], + ["property", "title"] + ]], + ["delimiter", "}"] + ]], + ["tag", [ + ["tag", [ + ["punctuation", ""] + ]], - ["latte", [["ld", [["punctuation", "{"], ["tag", "tag"]]], ["php", [["variable", "$a"], ["operator", "="], ["constant", "true"], ["operator", "?"], ["number", "10"], ["operator", "*"], ["number", "5"]]], ["rd", [["punctuation", "}"]]]]], - - ["tag", [["tag", [["punctuation", "<"], "div"]], - ["n-attr", [["attr-name", "n:attr"], ["attr-value", [["punctuation", "="], ["punctuation", "\""], ["php", [["variable", "$a"], ["operator", "="], ["constant", "true"], ["operator", "?"], ["number", "10"], ["operator", "*"], ["number", "5"]]], ["punctuation", "\""]]]]], - ["punctuation", ">"]]], - - ["latte", [["ld", [["punctuation", "{"], ["tag", "php"]]], ["php", [["string", "''"]]], ["rd", [["punctuation", "}"]]]]], - - ["latte", [["ld", [["punctuation", "{"], ["tag", "php"]]], ["php", [["string", ["\"\""]]]], ["rd", [["punctuation", "}"]]]]], - - ["latte", [["ld", [["punctuation", "{"], ["tag", "php"]]], ["php", [["string", ["\"ba\\\"r\""]]]], ["rd", [["punctuation", "}"]]]]], - - ["latte", [["ld", [["punctuation", "{"], ["tag", "php"]]], ["php", [["string", "'ba\\'z'"]]], ["rd", [["punctuation", "}"]]]]], - - ["latte", [["ld", [["punctuation", "{"], ["tag", "php"]]], ["php", [["comment", "/* \" */"]]], ["rd", [["punctuation", "}"]]]]] + ["latte", [ + ["delimiter", "{"], + ["latte-tag", "tag"], + ["php", [ + ["variable", "$a"], + ["operator", "="], + ["constant", "true"], + ["operator", "?"], + ["number", "10"], + ["operator", "*"], + ["number", "5"] + ]], + ["delimiter", "}"] + ]], + ["tag", [ + ["tag", [ + ["punctuation", "<"], + "div" + ]], + ["n-attr", [ + ["attr-name", "n:attr"], + ["attr-value", [ + ["punctuation", "="], + ["punctuation", "\""], + ["php", [ + ["variable", "$a"], + ["operator", "="], + ["constant", "true"], + ["operator", "?"], + ["number", "10"], + ["operator", "*"], + ["number", "5"] + ]], + ["punctuation", "\""] + ]] + ]], + ["punctuation", ">"] + ]], + ["latte", [ + ["delimiter", "{"], + ["latte-tag", "php"], + ["php", [ + ["string", "''"] + ]], + ["delimiter", "}"] + ]], + ["latte", [ + ["delimiter", "{"], + ["latte-tag", "php"], + ["php", [ + ["string", ["\"\""]] + ]], + ["delimiter", "}"] + ]], + ["latte", [ + ["delimiter", "{"], + ["latte-tag", "php"], + ["php", [ + ["string", ["\"ba\\\"r\""]] + ]], + ["delimiter", "}"] + ]], + ["latte", [ + ["delimiter", "{"], + ["latte-tag", "php"], + ["php", [ + ["string", "'ba\\'z'"] + ]], + ["delimiter", "}"] + ]], + ["latte", [ + ["delimiter", "{"], + ["latte-tag", "php"], + ["php", [ + ["comment", "/* \" */"] + ]], + ["delimiter", "}"] + ]] ] ---------------------------------------------------- -Checks for operators. \ No newline at end of file +Checks for operators. diff --git a/tests/languages/latte/php_feature.test b/tests/languages/latte/php_feature.test index 6d23c0ec1e..7455f66db4 100644 --- a/tests/languages/latte/php_feature.test +++ b/tests/languages/latte/php_feature.test @@ -10,25 +10,94 @@ ---------------------------------------------------- [ - ["latte", [["ld", [["punctuation", "{"], ["tag", "tag"]]], ["php", [["variable", "$a"], ["operator", "="], ["constant", "true"], ["operator", "?"], ["number", "10"], ["operator", "*"], ["number", "5"]]], ["rd", [["punctuation", "}"]]]]], - - ["tag", [["tag", [["punctuation", "<"], "div"]], ["n-attr", [["attr-name", "n:attr"], - ["attr-value", [["punctuation", "="], ["punctuation", "\""], ["php", [["variable", "$a"], ["operator", "="], ["constant", "true"], ["operator", "?"], ["number", "10"], ["operator", "*"], ["number", "5"]]], ["punctuation", "\""]]]]], - ["punctuation", ">"]]], - - ["latte", [["ld", [["punctuation", "{"], ["tag", "php"]]], ["php", [["string", "''"]]], ["rd", [["punctuation", "}"]]]]], - - ["latte", [["ld", [["punctuation", "{"], ["tag", "php"]]], ["php", [["string", ["\"\""]]]], ["rd", [["punctuation", "}"]]]]], - - ["latte", [["ld", [["punctuation", "{"], ["tag", "php"]]], ["php", [["string", ["\"ba\\\"r\""]]]], ["rd", [["punctuation", "}"]]]]], - - ["latte", [["ld", [["punctuation", "{"], ["tag", "php"]]], ["php", [["string", "'ba\\'z'"]]], ["rd", [["punctuation", "}"]]]]], - - ["latte", [["ld", [["punctuation", "{"], ["tag", "php"]]], ["php", [["comment", "/* \" */"]]], ["rd", [["punctuation", "}"]]]]], - - ["latte", [["ld", [["punctuation", "{"], ["tag", "php"]]], ["php", [["comment", "/* } */"]]], ["rd", [["punctuation", "}"]]]]] + ["latte", [ + ["delimiter", "{"], + ["latte-tag", "tag"], + ["php", [ + ["variable", "$a"], + ["operator", "="], + ["constant", "true"], + ["operator", "?"], + ["number", "10"], + ["operator", "*"], + ["number", "5"] + ]], + ["delimiter", "}"] + ]], + ["tag", [ + ["tag", [ + ["punctuation", "<"], + "div" + ]], + ["n-attr", [ + ["attr-name", "n:attr"], + ["attr-value", [ + ["punctuation", "="], + ["punctuation", "\""], + ["php", [ + ["variable", "$a"], + ["operator", "="], + ["constant", "true"], + ["operator", "?"], + ["number", "10"], + ["operator", "*"], + ["number", "5"] + ]], + ["punctuation", "\""] + ]] + ]], + ["punctuation", ">"] + ]], + ["latte", [ + ["delimiter", "{"], + ["latte-tag", "php"], + ["php", [ + ["string", "''"] + ]], + ["delimiter", "}"] + ]], + ["latte", [ + ["delimiter", "{"], + ["latte-tag", "php"], + ["php", [ + ["string", ["\"\""]] + ]], + ["delimiter", "}"] + ]], + ["latte", [ + ["delimiter", "{"], + ["latte-tag", "php"], + ["php", [ + ["string", ["\"ba\\\"r\""]] + ]], + ["delimiter", "}"] + ]], + ["latte", [ + ["delimiter", "{"], + ["latte-tag", "php"], + ["php", [ + ["string", "'ba\\'z'"] + ]], + ["delimiter", "}"] + ]], + ["latte", [ + ["delimiter", "{"], + ["latte-tag", "php"], + ["php", [ + ["comment", "/* \" */"] + ]], + ["delimiter", "}"] + ]], + ["latte", [ + ["delimiter", "{"], + ["latte-tag", "php"], + ["php", [ + ["comment", "/* } */"] + ]], + ["delimiter", "}"] + ]] ] ---------------------------------------------------- -Checks for operators. \ No newline at end of file +Checks for operators.