diff --git a/src/jinja.ts b/src/jinja.ts index d74c8c8..342f957 100644 --- a/src/jinja.ts +++ b/src/jinja.ts @@ -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", diff --git a/src/parser.ts b/src/parser.ts index e16984b..88303cd 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -10,7 +10,7 @@ import { } from "./jinja"; const regex = - /(?
(?\n)?(\s*?))(? {{\s*(? '([^']|\\')*'|"([^"]|\\")*"|[\S\s]*?)\s*}}|{%(? [-+]?)\s*(? (? for|endfor|if|else|endif|macro|endmacro|call|endcall|filter|endfilter|set|endset|include|import|from|extends|block|endblock)('([^']|\\')*'|"([^"]|\\")*"|[\S\s])*?)\s*(? [-+]?)%}|(? {#[\S\s]*?#})|(? <(script)((?!<)[\s\S])*>((?!<\/script)[\s\S])*?{{[\s\S]*?<\/(script)>)|(? <(style)((?!<)[\s\S])*>((?!<\/style)[\s\S])*?{{[\s\S]*?<\/(style)>)|(? [\s\S]*))/; + /(? (?\n)?(\s*?))(? {{\s*(? '([^']|\\')*'|"([^"]|\\")*"|[\S\s]*?)\s*}}|{%(? [-+]?)\s*(? (? for|endfor|if|else|elif|endif|macro|endmacro|call|endcall|filter|endfilter|set|endset|include|import|from|extends|block|endblock)('([^']|\\')*'|"([^"]|\\")*"|[\S\s])*?)\s*(? [-+]?)%}|(? {#[\S\s]*?#})|(? <(script)((?!<)[\s\S])*>((?!<\/script)[\s\S])*?{{[\s\S]*?<\/(script)>)|(? <(style)((?!<)[\s\S])*>((?!<\/style)[\s\S])*?{{[\s\S]*?<\/(style)>)|(? [\s\S]*))/; export const parse: Parser ["parse"] = (text) => { const statementStack: Statement[] = []; diff --git a/src/printer.ts b/src/printer.ts index 5ee235d..594e4b4 100644 --- a/src/printer.ts +++ b/src/printer.ts @@ -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) { diff --git a/test/cases/statement_if_else/expected.html b/test/cases/statement_if_else/expected.html index 04a412d..a919a86 100644 --- a/test/cases/statement_if_else/expected.html +++ b/test/cases/statement_if_else/expected.html @@ -1,7 +1,7 @@ {% if href in ['layout.html', 'index.html, 'about.html', 'user.html'] %} {{ title }}
- {% else if href == "random.html" %} + {% elif href == "random.html" %}{{ title }}
{% else %}{{ title }}
diff --git a/test/cases/statement_if_else/input.html b/test/cases/statement_if_else/input.html index cfa1ca9..d2d7927 100644 --- a/test/cases/statement_if_else/input.html +++ b/test/cases/statement_if_else/input.html @@ -1,7 +1,7 @@ {% if href in ['layout.html', 'index.html, 'about.html', 'user.html'] %}{{title}}
- {%else if href == "random.html" %} + {%elif href == "random.html" %}{{title}}
{%else%}{{title}}