Skip to content

Commit

Permalink
rename block type to container
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuadavidthomas committed Jan 7, 2025
1 parent 00f3fb3 commit ad293c6
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 71 deletions.
54 changes: 27 additions & 27 deletions crates/djls-template-ast/SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,18 @@ Represents Django template tags that may have nested content, assignments, and c

```rust
pub enum Block {
Block {
tag: Tag,
nodes: Vec<Node>,
closing: Option<Box<Block>>,
},
Branch {
tag: Tag,
nodes: Vec<Node>,
},
Closing {
tag: Tag,
},
Container {
tag: Tag,
nodes: Vec<Node>,
closing: Option<Box<Block>>,
},
Inclusion {
tag: Tag,
template_name: String,
Expand Down Expand Up @@ -159,24 +159,6 @@ pub struct Assignment {

#### Variants

##### `Block::Block`

Represents standard block tags that may contain child nodes and require a closing tag.

```rust
Block::Block {
tag: Tag, // The opening Tag of the block
nodes: Vec<Node>, // Nodes contained within the block
closing: Option<Box<Block>>, // Contains Block::Closing if present
}
```

Examples:

- `{% if %}...{% endif %}`
- `{% for %}...{% endfor %}`
- `{% with %}...{% endwith %}`

##### `Block::Branch`

Represents branch tags that are part of control flow structures and contain child nodes.
Expand Down Expand Up @@ -210,6 +192,24 @@ Examples:
- `{% endfor %}`
- `{% endwith %}`

##### `Block::Container`

Represents standard block tags that may contain child nodes and require a closing tag.

```rust
Block::Block {
tag: Tag, // The opening Tag of the block
nodes: Vec<Node>, // Nodes contained within the block
closing: Option<Box<Block>>, // Contains Block::Closing if present
}
```

Examples:

- `{% if %}...{% endif %}`
- `{% for %}...{% endfor %}`
- `{% with %}...{% endwith %}`

##### `Block::Inclusion`

Represents tags that include or extend templates.
Expand Down Expand Up @@ -250,7 +250,7 @@ Tag Specifications (TagSpecs) define how tags are parsed and understood. They al

```toml
[package.module.path.tag_name] # Path where tag is registered, e.g., django.template.defaulttags
type = "block" | "inclusion" | "single"
type = "container" | "inclusion" | "single"
closing = "closing_tag_name" # For block tags that require a closing tag
branches = ["branch_tag_name", ...] # For block tags that support branches

Expand All @@ -267,7 +267,7 @@ The `name` field in args should match the internal name used in Django's node im

### Tag Types

- `block`: Tags that wrap content and require a closing tag
- `container`: Tags that wrap content and require a closing tag

```django
{% if condition %}content{% endif %}
Expand Down Expand Up @@ -298,7 +298,7 @@ The `name` field in args should match the internal name used in Django's node im

```toml
[django.template.defaulttags.if]
type = "block"
type = "container"
closing = "endif"
branches = ["elif", "else"]
args = [{ name = "condition", required = true }]
Expand All @@ -316,7 +316,7 @@ args = [{ name = "template_name", required = true }]

```toml
[django.template.defaulttags.autoescape]
type = "block"
type = "container"
closing = "endautoescape"
args = [{ name = "setting", required = true, allowed_values = ["on", "off"] }]
```
Expand Down
20 changes: 10 additions & 10 deletions crates/djls-template-ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,18 @@ impl Node {

#[derive(Debug, Clone, Serialize)]
pub enum Block {
Block {
tag: Tag,
nodes: Vec<Node>,
closing: Option<Box<Block>>,
},
Branch {
tag: Tag,
nodes: Vec<Node>,
},
Closing {
tag: Tag,
},
Container {
tag: Tag,
nodes: Vec<Node>,
closing: Option<Box<Block>>,
},
Inclusion {
tag: Tag,
template_name: String,
Expand All @@ -169,7 +169,7 @@ pub enum Block {
impl Block {
pub fn tag(&self) -> &Tag {
match self {
Self::Block { tag, .. }
Self::Container { tag, .. }
| Self::Branch { tag, .. }
| Self::Single { tag }
| Self::Inclusion { tag, .. }
Expand All @@ -179,15 +179,15 @@ impl Block {

pub fn nodes(&self) -> Option<&Vec<Node>> {
match self {
Block::Block { nodes, .. } => Some(nodes),
Block::Container { nodes, .. } => Some(nodes),
Block::Branch { nodes, .. } => Some(nodes),
_ => None,
}
}

pub fn closing(&self) -> Option<&Block> {
match self {
Block::Block { closing, .. } => closing.as_deref(),
Block::Container { closing, .. } => closing.as_deref(),
_ => None,
}
}
Expand Down Expand Up @@ -306,7 +306,7 @@ mod tests {

#[test]
fn test_block_spans() {
let nodes = vec![Node::Block(Block::Block {
let nodes = vec![Node::Block(Block::Container {
tag: Tag {
name: "if".to_string(),
bits: vec!["user.is_authenticated".to_string()],
Expand Down Expand Up @@ -341,7 +341,7 @@ mod tests {
assert!(errors.is_empty());

let nodes = ast.nodes();
if let Node::Block(Block::Block {
if let Node::Block(Block::Container {
tag,
nodes,
closing,
Expand Down
4 changes: 2 additions & 2 deletions crates/djls-template-ast/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl Parser {

match spec {
Some(spec) => match spec.tag_type {
TagType::Block => self.parse_block_tag(tag, spec),
TagType::Container => self.parse_block_tag(tag, spec),
TagType::Single => Ok(Node::Block(Block::Single { tag })),
TagType::Inclusion => {
let template_name = tag.bits.get(1).cloned().unwrap_or_default();
Expand Down Expand Up @@ -161,7 +161,7 @@ impl Parser {
.push(ParserError::Ast(AstError::UnclosedTag(tag.name.clone())));
}

Ok(Node::Block(Block::Block {
Ok(Node::Block(Block::Container {
tag,
nodes,
closing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ expression: ast
---
nodes:
- Block:
Block:
Container:
tag:
name: if
bits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ expression: ast
---
nodes:
- Block:
Block:
Container:
tag:
name: for
bits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ expression: ast
---
nodes:
- Block:
Block:
Container:
tag:
name: if
bits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ nodes:
start: 0
length: 8
- Block:
Block:
Container:
tag:
name: if
bits:
Expand Down Expand Up @@ -43,7 +43,7 @@ nodes:
start: 47
length: 31
- Block:
Block:
Container:
tag:
name: for
bits:
Expand All @@ -60,7 +60,7 @@ nodes:
assignment: ~
nodes:
- Block:
Block:
Container:
tag:
name: if
bits:
Expand Down Expand Up @@ -101,7 +101,7 @@ nodes:
start: 171
length: 10
- Block:
Block:
Container:
tag:
name: if
bits:
Expand Down Expand Up @@ -135,7 +135,7 @@ nodes:
length: 5
assignment: ~
- Block:
Block:
Container:
tag:
name: if
bits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ expression: ast
---
nodes:
- Block:
Block:
Container:
tag:
name: for
bits:
Expand All @@ -21,7 +21,7 @@ nodes:
assignment: ~
nodes:
- Block:
Block:
Container:
tag:
name: if
bits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ nodes:
start: 28
length: 15
- Block:
Block:
Container:
tag:
name: if
bits:
Expand Down Expand Up @@ -62,7 +62,7 @@ nodes:
start: 198
length: 41
- Block:
Block:
Container:
tag:
name: for
bits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ expression: ast
---
nodes:
- Block:
Block:
Container:
tag:
name: for
bits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ expression: ast
---
nodes:
- Block:
Block:
Container:
tag:
name: if
bits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ nodes:
start: 394
length: 56
- Block:
Block:
Container:
tag:
name: if
bits:
Expand Down Expand Up @@ -143,7 +143,7 @@ nodes:
start: 598
length: 6
- Block:
Block:
Container:
tag:
name: if
bits:
Expand Down
14 changes: 7 additions & 7 deletions crates/djls-template-ast/src/tagspecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ impl TagSpec {
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum TagType {
Block,
Single,
Container,
Inclusion,
Single,
}

#[derive(Clone, Debug, Deserialize)]
Expand Down Expand Up @@ -238,7 +238,7 @@ mod tests {

let pyproject_content = r#"
[tool.djls.template.tags.mytag]
type = "block"
type = "container"
closing = "endmytag"
branches = ["mybranch"]
args = [{ name = "myarg", required = true }]
Expand All @@ -248,10 +248,10 @@ args = [{ name = "myarg", required = true }]
let specs = TagSpecs::load_all(root)?;

let if_tag = specs.get("if").expect("if tag should be present");
assert_eq!(if_tag.tag_type, TagType::Block);
assert_eq!(if_tag.tag_type, TagType::Container);

let my_tag = specs.get("mytag").expect("mytag should be present");
assert_eq!(my_tag.tag_type, TagType::Block);
assert_eq!(my_tag.tag_type, TagType::Container);
assert_eq!(my_tag.closing, Some("endmytag".to_string()));

let branches = my_tag
Expand All @@ -276,14 +276,14 @@ args = [{ name = "myarg", required = true }]

let djls_content = r#"
[mytag1]
type = "block"
type = "container"
closing = "endmytag1"
"#;
fs::write(root.join("djls.toml"), djls_content)?;

let pyproject_content = r#"
[tool.djls.template.tags]
mytag2.type = "block"
mytag2.type = "container"
mytag2.closing = "endmytag2"
"#;
fs::write(root.join("pyproject.toml"), pyproject_content)?;
Expand Down
Loading

0 comments on commit ad293c6

Please sign in to comment.