Merge pull request #4 from davidodenwald/add-elif-statement

add elif statement
This commit is contained in:
{{david|odenwald}} 2022-12-06 18:20:36 +01:00 committed by GitHub
commit 25474bdad4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 7 additions and 5 deletions

View file

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

View file

@ -10,7 +10,7 @@ import {
} from "./jinja"; } from "./jinja";
const regex = 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) => { export const parse: Parser<Node>["parse"] = (text) => {
const statementStack: Statement[] = []; const statementStack: Statement[] = [];

View file

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

View file

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

View file

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