add elif statement

This commit is contained in:
davidodenwald 2022-12-06 18:18:15 +01:00
parent 6980ee646d
commit 5ffcddf657
5 changed files with 7 additions and 5 deletions

View file

@ -41,6 +41,7 @@ export type Keyword =
| "endfor"
| "if"
| "else"
| "elif"
| "endif"
| "macro"
| "endmacro"
@ -59,6 +60,7 @@ export type Keyword =
export const nonClosingStatements = [
"else",
"elif",
"include",
"import",
"from",

View file

@ -10,7 +10,7 @@ import {
} from "./jinja";
const regex =
/(?<pre>(?<newline>\n)?(\s*?))(?<node>{{\s*(?<expression>'([^']|\\')*'|"([^"]|\\")*"|[\S\s]*?)\s*}}|{%(?<startDelimiter>[-+]?)\s*(?<statement>(?<keyword>for|endfor|if|else|endif|macro|endmacro|call|endcall|filter|endfilter|set|endset|include|import|from|extends|block|endblock)('([^']|\\')*'|"([^"]|\\")*"|[\S\s])*?)\s*(?<endDelimiter>[-+]?)%}|(?<comment>{#[\S\s]*?#})|(?<scriptBlock><(script)((?!<)[\s\S])*>((?!<\/script)[\s\S])*?{{[\s\S]*?<\/(script)>)|(?<styleBlock><(style)((?!<)[\s\S])*>((?!<\/style)[\s\S])*?{{[\s\S]*?<\/(style)>)|(?<ignoreBlock><!-- prettier-ignore-start -->[\s\S]*<!-- prettier-ignore-end -->))/;
/(?<pre>(?<newline>\n)?(\s*?))(?<node>{{\s*(?<expression>'([^']|\\')*'|"([^"]|\\")*"|[\S\s]*?)\s*}}|{%(?<startDelimiter>[-+]?)\s*(?<statement>(?<keyword>for|endfor|if|else|elif|endif|macro|endmacro|call|endcall|filter|endfilter|set|endset|include|import|from|extends|block|endblock)('([^']|\\')*'|"([^"]|\\")*"|[\S\s])*?)\s*(?<endDelimiter>[-+]?)%}|(?<comment>{#[\S\s]*?#})|(?<scriptBlock><(script)((?!<)[\s\S])*>((?!<\/script)[\s\S])*?{{[\s\S]*?<\/(script)>)|(?<styleBlock><(style)((?!<)[\s\S])*>((?!<\/style)[\s\S])*?{{[\s\S]*?<\/(style)>)|(?<ignoreBlock><!-- prettier-ignore-start -->[\s\S]*<!-- prettier-ignore-end -->))/;
export const parse: Parser<Node>["parse"] = (text) => {
const statementStack: Statement[] = [];

View file

@ -42,7 +42,7 @@ const printStatement = (node: Statement): builders.Doc => {
{ shouldBreak: node.ownLine }
);
return node.keyword === "else"
return ["else", "elif"].includes(node.keyword)
? [builders.dedent(builders.hardline), statemnt, builders.hardline]
: statemnt;
};
@ -147,7 +147,7 @@ const splitAtElse = (node: Node): string[] => {
const elseNodes = Object.values(node.nodes).filter(
(n) =>
n.type === "statement" &&
(n as Statement).keyword === "else" &&
["else", "elif"].includes((n as Statement).keyword) &&
node.content.search(n.id) !== NOT_FOUND
);
if (!elseNodes.length) {

View file

@ -1,7 +1,7 @@
<body>
{% if href in ['layout.html', 'index.html, 'about.html', 'user.html'] %}
<h1>{{ title }}</h1>
{% else if href == "random.html" %}
{% elif href == "random.html" %}
<h2>{{ title }}</h2>
{% else %}
<h3>{{ title }}</h3>

View file

@ -1,7 +1,7 @@
<body>
{% if href in ['layout.html', 'index.html, 'about.html', 'user.html'] %}
<h1>{{title}}</h1>
{%else if href == "random.html" %}
{%elif href == "random.html" %}
<h2>{{title}}</h2>
{%else%}
<h3>{{title}}</h3>