mirror of
https://github.com/semihalev/twig.git
synced 2026-03-14 13:55:46 +01:00
Clean up formatting and remove debug code
- Fix whitespace indentation in do tag implementation - Remove debug prints from range function - Delete temporary update_tokenizer file - Fix newline at end of file 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
3754dd3f72
commit
703b6eed01
5 changed files with 22 additions and 145 deletions
55
node.go
55
node.go
|
|
@ -287,61 +287,6 @@ func (n *ForNode) Render(w io.Writer, ctx *RenderContext) error {
|
|||
}
|
||||
}
|
||||
|
||||
// Special handling for FunctionNode with name "range" directly in for loop
|
||||
if funcNode, ok := n.sequence.(*FunctionNode); ok && funcNode.name == "range" {
|
||||
// Add debug output to see what's happening
|
||||
fmt.Printf("Found range function in for loop with %d args\n", len(funcNode.args))
|
||||
|
||||
// Get the engine's function registry to call range function directly
|
||||
// This handles the case of using range() directly in for loop
|
||||
if ctx.engine != nil && ctx.engine.environment != nil {
|
||||
fmt.Println("Engine and environment exist")
|
||||
|
||||
for i, ext := range ctx.engine.environment.extensions {
|
||||
fmt.Printf("Checking extension %d: %s\n", i, ext.GetName())
|
||||
if functions := ext.GetFunctions(); functions != nil {
|
||||
fmt.Printf("Extension has %d functions\n", len(functions))
|
||||
for name := range functions {
|
||||
fmt.Printf(" - Function: %s\n", name)
|
||||
}
|
||||
|
||||
if rangeFunc, exists := functions["range"]; exists {
|
||||
fmt.Println("Found range function!")
|
||||
// Evaluate all arguments
|
||||
var args []interface{}
|
||||
for i, argNode := range funcNode.args {
|
||||
fmt.Printf("Evaluating arg %d\n", i)
|
||||
arg, err := ctx.EvaluateExpression(argNode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Arg %d = %v (type: %T)\n", i, arg, arg)
|
||||
args = append(args, arg)
|
||||
}
|
||||
|
||||
// Call the range function directly
|
||||
fmt.Printf("Calling range function with %d args\n", len(args))
|
||||
result, err := rangeFunc(args...)
|
||||
if err != nil {
|
||||
fmt.Printf("Error from range function: %v\n", err)
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Range result: %v (type: %T)\n", result, result)
|
||||
|
||||
// Use the result as our sequence
|
||||
seq := result
|
||||
// Continue with normal for loop processing
|
||||
return n.renderForLoop(w, ctx, seq)
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println("Couldn't find range function in extensions")
|
||||
} else {
|
||||
fmt.Println("Engine or environment is nil")
|
||||
}
|
||||
}
|
||||
|
||||
// Special handling for FilterNode to improve rendering in for loops
|
||||
if filterNode, ok := n.sequence.(*FilterNode); ok {
|
||||
if IsDebugEnabled() {
|
||||
|
|
|
|||
13
parser.go
13
parser.go
|
|
@ -1978,14 +1978,14 @@ func (p *Parser) parseDo(parser *Parser) (Node, error) {
|
|||
// Check for special case: assignment expressions
|
||||
// These need to be handled specially since they're not normal expressions
|
||||
if parser.tokenIndex < len(parser.tokens) &&
|
||||
parser.tokens[parser.tokenIndex].Type == TOKEN_NAME {
|
||||
parser.tokens[parser.tokenIndex].Type == TOKEN_NAME {
|
||||
|
||||
varName := parser.tokens[parser.tokenIndex].Value
|
||||
parser.tokenIndex++
|
||||
|
||||
if parser.tokenIndex < len(parser.tokens) &&
|
||||
parser.tokens[parser.tokenIndex].Type == TOKEN_OPERATOR &&
|
||||
parser.tokens[parser.tokenIndex].Value == "=" {
|
||||
parser.tokens[parser.tokenIndex].Type == TOKEN_OPERATOR &&
|
||||
parser.tokens[parser.tokenIndex].Value == "=" {
|
||||
|
||||
// Skip the equals sign
|
||||
parser.tokenIndex++
|
||||
|
|
@ -2208,13 +2208,6 @@ func (p *Parser) parseMacro(parser *Parser) (Node, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
// Debug output for default value
|
||||
if literalNode, ok := defaultExpr.(*LiteralNode); ok {
|
||||
fmt.Printf("DEBUG: Default value for %s: %v (type %T)\n", paramName, literalNode.value, literalNode.value)
|
||||
} else {
|
||||
fmt.Printf("DEBUG: Default value for %s: %T\n", paramName, defaultExpr)
|
||||
}
|
||||
|
||||
defaults[paramName] = defaultExpr
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,61 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Read the html_preserving_tokenizer.go file
|
||||
filepath := "html_preserving_tokenizer.go"
|
||||
content, err := ioutil.ReadFile(filepath)
|
||||
if err != nil {
|
||||
fmt.Printf("Error reading file: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Make a backup of the original file
|
||||
err = ioutil.WriteFile(filepath+".bak", content, 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("Error creating backup: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Define patterns to replace
|
||||
pattern1 := regexp.MustCompile(`tokens = append\(tokens, Token{Type: TOKEN_([A-Z_]+), Value: ([^,]+), Line: line}\)`)
|
||||
pattern2 := regexp.MustCompile(`\*tokens = append\(\*tokens, Token{Type: TOKEN_([A-Z_]+), Value: ([^,]+), Line: line}\)`)
|
||||
pattern3 := regexp.MustCompile(`\*tokens = append\(\*tokens, Token{\s+Type:\s+TOKEN_([A-Z_]+),\s+Value:\s+([^,]+),\s+Line:\s+line,\s+}\)`)
|
||||
|
||||
// Replace patterns
|
||||
result := pattern1.ReplaceAllString(string(content), `tokens = append(tokens, createToken(TOKEN_$1, $2, line))`)
|
||||
result = pattern2.ReplaceAllString(result, `*tokens = append(*tokens, createToken(TOKEN_$1, $2, line))`)
|
||||
result = pattern3.ReplaceAllString(result, `*tokens = append(*tokens, createToken(TOKEN_$1, $2, line))`)
|
||||
|
||||
// Write the updated content back to the file
|
||||
err = ioutil.WriteFile(filepath, []byte(result), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("Error writing file: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Updated all token creation sites successfully!")
|
||||
|
||||
// Count the number of replacements
|
||||
originalTokens := countTokenCreations(string(content))
|
||||
updatedTokens := countTokenCreations(result)
|
||||
|
||||
fmt.Printf("Original token creations: %d\n", originalTokens)
|
||||
fmt.Printf("Remaining token creations: %d\n", updatedTokens)
|
||||
fmt.Printf("Replaced %d token creation sites\n", originalTokens - updatedTokens)
|
||||
}
|
||||
|
||||
func countTokenCreations(content string) int {
|
||||
pattern1 := regexp.MustCompile(`Token{Type: TOKEN_`)
|
||||
pattern2 := regexp.MustCompile(`Token{\s+Type:\s+TOKEN_`)
|
||||
|
||||
count1 := len(pattern1.FindAllStringIndex(content, -1))
|
||||
count2 := len(pattern2.FindAllStringIndex(content, -1))
|
||||
|
||||
return count1 + count2
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue