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 { pub enum Node {
Text(String), Text(String),
Comment(String), Comment(String),
Tag(TagNode), Block(BlockNode),
Variable { Variable {
bits: Vec<String>, bits: Vec<String>,
filters: Vec<DjangoFilter>, filters: Vec<DjangoFilter>,
@ -44,8 +44,8 @@ pub enum Node {
} }
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize)]
pub enum TagNode { pub enum BlockNode {
Block { Standard {
name: String, name: String,
bits: Vec<String>, bits: Vec<String>,
children: Vec<Node>, 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::tagspecs::TagSpec;
use crate::tokens::{Token, TokenStream, TokenType}; use crate::tokens::{Token, TokenStream, TokenType};
use thiserror::Error; use thiserror::Error;
@ -151,13 +151,13 @@ impl Parser {
if spec.closing.as_deref() == Some(&tag) { if spec.closing.as_deref() == Some(&tag) {
// If we have a current branch, add it to children // If we have a current branch, add it to children
if let Some((name, bits, branch_children)) = current_branch { if let Some((name, bits, branch_children)) = current_branch {
children.push(Node::Tag(TagNode::Branch { children.push(Node::Block(BlockNode::Branch {
name, name,
bits, bits,
children: branch_children, children: branch_children,
})); }));
} }
children.push(Node::Tag(TagNode::Closing { children.push(Node::Block(BlockNode::Closing {
name: tag, name: tag,
bits: vec![], bits: vec![],
})); }));
@ -169,7 +169,7 @@ impl Parser {
if let Some(branch) = branches.iter().find(|i| i.name == tag) { if let Some(branch) = branches.iter().find(|i| i.name == tag) {
// If we have a current branch, add it to children // If we have a current branch, add it to children
if let Some((name, bits, branch_children)) = current_branch { if let Some((name, bits, branch_children)) = current_branch {
children.push(Node::Tag(TagNode::Branch { children.push(Node::Block(BlockNode::Branch {
name, name,
bits, bits,
children: branch_children, children: branch_children,
@ -194,7 +194,7 @@ impl Parser {
} }
} }
// If we get here, it's an unexpected tag // 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(), name: tag_name.clone(),
bits: bits.clone(), bits: bits.clone(),
children: children.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(), name: tag_name.clone(),
bits, bits,
children, children,

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -4,20 +4,17 @@ expression: ast
--- ---
nodes: 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 " - 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: - Block:
Tag: Standard:
Block:
name: if name: if
bits: bits:
- if - if
- user.is_authenticated - user.is_authenticated
children: children:
- Text: "\n " - Text: "\n "
- Django: - Comment: Welcome message
Comment: Welcome message
- Text: "\n <h1>Welcome, " - Text: "\n <h1>Welcome, "
- Django: - Variable:
Variable:
bits: bits:
- user - user
- name - name
@ -28,30 +25,26 @@ nodes:
arguments: arguments:
- "'Guest'" - "'Guest'"
- Text: "!</h1>\n " - Text: "!</h1>\n "
- Django: - Block:
Tag: Standard:
Block:
name: if name: if
bits: bits:
- if - if
- user.is_staff - user.is_staff
children: children:
- Text: "\n <span>Admin</span>\n " - Text: "\n <span>Admin</span>\n "
- Django: - Block:
Tag:
Branch: Branch:
name: else name: else
bits: [] bits: []
children: children:
- Text: "\n <span>User</span>\n " - Text: "\n <span>User</span>\n "
- Django: - Block:
Tag:
Closing: Closing:
name: endif name: endif
bits: [] bits: []
- Text: "\n " - Text: "\n "
- Django: - Block:
Tag:
Closing: Closing:
name: endif name: endif
bits: [] bits: []