make inline statements possible
This commit is contained in:
parent
84543ebba2
commit
6086008c32
5 changed files with 25 additions and 5 deletions
|
@ -7,6 +7,7 @@ export interface Node {
|
|||
id: string;
|
||||
type: "root" | "expression" | "statement" | "block" | "ignore";
|
||||
content: string;
|
||||
ownLine: boolean;
|
||||
originalText: string;
|
||||
index: number;
|
||||
length: number;
|
||||
|
@ -15,7 +16,6 @@ export interface Node {
|
|||
|
||||
export interface Expression extends Node {
|
||||
type: "expression";
|
||||
ownLine: boolean;
|
||||
}
|
||||
|
||||
export type Delimiter = "" | "-" | "+";
|
||||
|
@ -35,7 +35,6 @@ export interface Block extends Node {
|
|||
|
||||
export interface IgnoreBlock extends Node {
|
||||
type: "ignore";
|
||||
ownLine: boolean;
|
||||
}
|
||||
|
||||
export type Keyword =
|
||||
|
|
|
@ -21,6 +21,7 @@ export const parse: Parser<Node>["parse"] = (text) => {
|
|||
id: "0",
|
||||
type: "root" as const,
|
||||
content: text,
|
||||
ownLine: false,
|
||||
originalText: text,
|
||||
index: 0,
|
||||
length: 0,
|
||||
|
@ -101,6 +102,7 @@ export const parse: Parser<Node>["parse"] = (text) => {
|
|||
id: placeholder,
|
||||
type: "statement",
|
||||
content: statement,
|
||||
ownLine: newline,
|
||||
originalText: matchText,
|
||||
index: match.index + i + pre.length,
|
||||
length: matchText.length,
|
||||
|
@ -116,6 +118,7 @@ export const parse: Parser<Node>["parse"] = (text) => {
|
|||
id: generatePlaceholder(),
|
||||
type: "statement" as const,
|
||||
content: statement,
|
||||
ownLine: newline,
|
||||
originalText: matchText,
|
||||
index: match.index + i + pre.length,
|
||||
length: matchText.length,
|
||||
|
@ -154,6 +157,7 @@ export const parse: Parser<Node>["parse"] = (text) => {
|
|||
id: generatePlaceholder(),
|
||||
type: "statement" as const,
|
||||
content: statement,
|
||||
ownLine: newline,
|
||||
originalText: matchText,
|
||||
index: match.index + i + pre.length,
|
||||
length: matchText.length,
|
||||
|
@ -175,6 +179,7 @@ export const parse: Parser<Node>["parse"] = (text) => {
|
|||
start: start,
|
||||
end: end,
|
||||
content,
|
||||
ownLine: newline,
|
||||
originalText: matchText,
|
||||
index: start.index,
|
||||
length: end.index + end.length - start.index,
|
||||
|
|
|
@ -47,7 +47,7 @@ const printStatement = (node: Statement): builders.Doc => {
|
|||
node.endDelimiter,
|
||||
"%}",
|
||||
],
|
||||
{ shouldBreak: true }
|
||||
{ shouldBreak: node.ownLine }
|
||||
);
|
||||
|
||||
return node.keyword === "else"
|
||||
|
@ -130,13 +130,23 @@ export const embed: Printer<Node>["embed"] = (
|
|||
});
|
||||
|
||||
if (node.type === "block") {
|
||||
if (node.content.includes("\n")) {
|
||||
return builders.group([
|
||||
path.call(print, "nodes", (node as Block).start.id),
|
||||
builders.indent([builders.softline, utils.stripTrailingHardline(mapped)]),
|
||||
builders.indent([
|
||||
builders.softline,
|
||||
utils.stripTrailingHardline(mapped),
|
||||
]),
|
||||
builders.hardline,
|
||||
path.call(print, "nodes", (node as Block).end.id),
|
||||
]);
|
||||
}
|
||||
return builders.group([
|
||||
path.call(print, "nodes", (node as Block).start.id),
|
||||
utils.stripTrailingHardline(mapped),
|
||||
path.call(print, "nodes", (node as Block).end.id),
|
||||
]);
|
||||
}
|
||||
return [...mapped, builders.hardline];
|
||||
};
|
||||
|
||||
|
|
3
test/cases/statement_inline/expected.html
Normal file
3
test/cases/statement_inline/expected.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
<title>{% block title %}{% endblock %}</title>
|
||||
|
||||
<div>class="{% if class %}{{ class }}{% endif %}"</div>
|
3
test/cases/statement_inline/input.html
Normal file
3
test/cases/statement_inline/input.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
<title>{%block title%} {%endblock%}</title>
|
||||
|
||||
<div>class="{%if class%}{{class}}{%endif%}"</div>
|
Loading…
Reference in a new issue