This commit is contained in:
Josh Thomas 2025-01-04 23:21:11 -06:00
parent de296ceff4
commit 0b8ca4061c
14 changed files with 321 additions and 370 deletions

View file

@ -36,7 +36,7 @@ impl Ast {
pub enum Node {
Text(String),
Comment(String),
Tag(TagNode),
Block(BlockNode),
Variable {
bits: Vec<String>,
filters: Vec<DjangoFilter>,
@ -44,8 +44,8 @@ pub enum Node {
}
#[derive(Clone, Debug, Serialize)]
pub enum TagNode {
Block {
pub enum BlockNode {
Standard {
name: String,
bits: Vec<String>,
children: Vec<Node>,

View file

@ -1,4 +1,4 @@
use crate::ast::{Ast, AstError, DjangoFilter, DjangoNode, Node, TagNode};
use crate::ast::{Ast, AstError, BlockNode, DjangoFilter, Node};
use crate::tagspecs::TagSpec;
use crate::tokens::{Token, TokenStream, TokenType};
use thiserror::Error;
@ -151,13 +151,13 @@ impl Parser {
if spec.closing.as_deref() == Some(&tag) {
// If we have a current branch, add it to children
if let Some((name, bits, branch_children)) = current_branch {
children.push(Node::Tag(TagNode::Branch {
children.push(Node::Block(BlockNode::Branch {
name,
bits,
children: branch_children,
}));
}
children.push(Node::Tag(TagNode::Closing {
children.push(Node::Block(BlockNode::Closing {
name: tag,
bits: vec![],
}));
@ -169,7 +169,7 @@ impl Parser {
if let Some(branch) = branches.iter().find(|i| i.name == tag) {
// If we have a current branch, add it to children
if let Some((name, bits, branch_children)) = current_branch {
children.push(Node::Tag(TagNode::Branch {
children.push(Node::Block(BlockNode::Branch {
name,
bits,
children: branch_children,
@ -194,7 +194,7 @@ impl Parser {
}
}
// If we get here, it's an unexpected tag
let node = Node::Tag(TagNode::Block {
let node = Node::Block(BlockNode::Standard {
name: tag_name.clone(),
bits: bits.clone(),
children: children.clone(),
@ -208,7 +208,7 @@ impl Parser {
}
}
let node = Node::Tag(TagNode::Block {
let node = Node::Block(BlockNode::Standard {
name: tag_name.clone(),
bits,
children,

View file

@ -4,6 +4,5 @@ expression: ast
---
nodes:
- Text: "<!-- HTML comment -->"
- Django:
Comment: Django comment
- Comment: Django comment
errors: []

View file

@ -3,9 +3,8 @@ source: crates/djls-template-ast/src/parser.rs
expression: ast
---
nodes:
- Django:
Tag:
Block:
- Block:
Standard:
name: if
bits:
- if
@ -14,8 +13,7 @@ nodes:
- "0"
children:
- Text: Positive
- Django:
Tag:
- Block:
Branch:
name: elif
bits:
@ -24,15 +22,13 @@ nodes:
- "0"
children:
- Text: Negative
- Django:
Tag:
- Block:
Branch:
name: else
bits: []
children:
- Text: Zero
- Django:
Tag:
- Block:
Closing:
name: endif
bits: []

View file

@ -3,9 +3,8 @@ source: crates/djls-template-ast/src/parser.rs
expression: ast
---
nodes:
- Django:
Tag:
Block:
- Block:
Standard:
name: for
bits:
- for
@ -13,20 +12,17 @@ nodes:
- in
- items
children:
- Django:
Variable:
- Variable:
bits:
- item
filters: []
- Django:
Tag:
- Block:
Branch:
name: empty
bits: []
children:
- Text: No items
- Django:
Tag:
- Block:
Closing:
name: endfor
bits: []

View file

@ -3,17 +3,15 @@ source: crates/djls-template-ast/src/parser.rs
expression: ast
---
nodes:
- Django:
Tag:
Block:
- Block:
Standard:
name: if
bits:
- if
- user.is_authenticated
children:
- Text: Welcome
- Django:
Tag:
- Block:
Closing:
name: endif
bits: []

View file

@ -3,8 +3,7 @@ source: crates/djls-template-ast/src/parser.rs
expression: ast
---
nodes:
- Django:
Variable:
- Variable:
bits:
- user
- name

View file

@ -3,8 +3,7 @@ source: crates/djls-template-ast/src/parser.rs
expression: ast
---
nodes:
- Django:
Variable:
- Variable:
bits:
- value
filters:

View file

@ -4,17 +4,15 @@ expression: ast
---
nodes:
- Text: "Welcome, "
- Django:
Tag:
Block:
- Block:
Standard:
name: if
bits:
- if
- user.is_authenticated
children:
- Text: "\n "
- Django:
Variable:
- Variable:
bits:
- user
- name
@ -25,9 +23,8 @@ nodes:
arguments:
- "'Guest'"
- Text: "\n "
- Django:
Tag:
Block:
- Block:
Standard:
name: for
bits:
- for
@ -36,31 +33,27 @@ nodes:
- user.groups
children:
- Text: "\n "
- Django:
Tag:
Block:
- Block:
Standard:
name: if
bits:
- if
- forloop.first
children:
- Text: (
- Django:
Tag:
- Block:
Closing:
name: endif
bits: []
- Text: "\n "
- Django:
Variable:
- Variable:
bits:
- group
- name
filters: []
- Text: "\n "
- Django:
Tag:
Block:
- Block:
Standard:
name: if
bits:
- if
@ -68,49 +61,42 @@ nodes:
- forloop.last
children:
- Text: ", "
- Django:
Tag:
- Block:
Closing:
name: endif
bits: []
- Text: "\n "
- Django:
Tag:
Block:
- Block:
Standard:
name: if
bits:
- if
- forloop.last
children:
- Text: )
- Django:
Tag:
- Block:
Closing:
name: endif
bits: []
- Text: "\n "
- Django:
Tag:
- Block:
Branch:
name: empty
bits: []
children:
- Text: "\n (no groups)\n "
- Django:
Tag:
- Block:
Closing:
name: endfor
bits: []
- Text: "\n"
- Django:
Tag:
- Block:
Branch:
name: else
bits: []
children:
- Text: "\n Guest\n"
- Django:
Tag:
- Block:
Closing:
name: endif
bits: []

View file

@ -3,9 +3,8 @@ source: crates/djls-template-ast/src/parser.rs
expression: ast
---
nodes:
- Django:
Tag:
Block:
- Block:
Standard:
name: for
bits:
- for
@ -13,27 +12,23 @@ nodes:
- in
- items
children:
- Django:
Tag:
Block:
- Block:
Standard:
name: if
bits:
- if
- item.active
children:
- Django:
Variable:
- Variable:
bits:
- item
- name
filters: []
- Django:
Tag:
- Block:
Closing:
name: endif
bits: []
- Django:
Tag:
- Block:
Closing:
name: endfor
bits: []

View file

@ -4,31 +4,26 @@ expression: ast
---
nodes:
- Text: "<div class=\"container\">\n <h1>Header</h1>\n "
- Django:
Tag:
Block:
- Block:
Standard:
name: if
bits:
- if
- user.is_authenticated
children:
- Text: "\n "
- Django:
Comment: This if is unclosed which does matter
- Comment: This if is unclosed which does matter
- Text: "\n <p>Welcome "
- Django:
Variable:
- Variable:
bits:
- user
- name
filters: []
- Text: "</p>\n <div>\n "
- Django:
Comment: "This div is unclosed which doesn't matter"
- Comment: "This div is unclosed which doesn't matter"
- Text: "\n "
- Django:
Tag:
Block:
- Block:
Standard:
name: for
bits:
- for
@ -37,14 +32,12 @@ nodes:
- items
children:
- Text: "\n <span>"
- Django:
Variable:
- Variable:
bits:
- item
filters: []
- Text: "</span>\n "
- Django:
Tag:
- Block:
Closing:
name: endfor
bits: []

View file

@ -3,9 +3,8 @@ source: crates/djls-template-ast/src/parser.rs
expression: ast
---
nodes:
- Django:
Tag:
Block:
- Block:
Standard:
name: for
bits:
- for
@ -13,8 +12,7 @@ nodes:
- in
- items
children:
- Django:
Variable:
- Variable:
bits:
- item
- name

View file

@ -3,9 +3,8 @@ source: crates/djls-template-ast/src/parser.rs
expression: ast
---
nodes:
- Django:
Tag:
Block:
- Block:
Standard:
name: if
bits:
- if

View file

@ -4,20 +4,17 @@ expression: ast
---
nodes:
- Text: "<!DOCTYPE html>\n<html>\n <head>\n <style type=\"text/css\">\n /* Style header */\n .header { color: blue; }\n </style>\n <script type=\"text/javascript\">\n // Init app\n const app = {\n /* Config */\n debug: true\n };\n </script>\n </head>\n <body>\n <!-- Header section -->\n <div class=\"header\" id=\"main\" data-value=\"123\" disabled>\n "
- Django:
Tag:
Block:
- Block:
Standard:
name: if
bits:
- if
- user.is_authenticated
children:
- Text: "\n "
- Django:
Comment: Welcome message
- Comment: Welcome message
- Text: "\n <h1>Welcome, "
- Django:
Variable:
- Variable:
bits:
- user
- name
@ -28,30 +25,26 @@ nodes:
arguments:
- "'Guest'"
- Text: "!</h1>\n "
- Django:
Tag:
Block:
- Block:
Standard:
name: if
bits:
- if
- user.is_staff
children:
- Text: "\n <span>Admin</span>\n "
- Django:
Tag:
- Block:
Branch:
name: else
bits: []
children:
- Text: "\n <span>User</span>\n "
- Django:
Tag:
- Block:
Closing:
name: endif
bits: []
- Text: "\n "
- Django:
Tag:
- Block:
Closing:
name: endif
bits: []