working folder creation
This commit is contained in:
parent
ea3e92b8b7
commit
b9a82af12f
11 changed files with 2791 additions and 1552 deletions
84
template_checker.py
Normal file
84
template_checker.py
Normal file
|
@ -0,0 +1,84 @@
|
|||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
def check_template_syntax(directory):
|
||||
"""Check all template files for common Jinja2 syntax errors"""
|
||||
template_pattern = re.compile(r'{{.*?}}|{%.*?%}|{#.*?#}')
|
||||
problem_files = []
|
||||
|
||||
for root, _, files in os.walk(directory):
|
||||
for file in files:
|
||||
if file.endswith('.html'):
|
||||
filepath = os.path.join(root, file)
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
try:
|
||||
content = f.read()
|
||||
# Check for unbalanced Jinja2 brackets
|
||||
open_curly = content.count('{{')
|
||||
close_curly = content.count('}}')
|
||||
open_block = content.count('{%')
|
||||
close_block = content.count('%}')
|
||||
open_comment = content.count('{#')
|
||||
close_comment = content.count('#}')
|
||||
|
||||
if open_curly != close_curly or open_block != close_block or open_comment != close_comment:
|
||||
problem_files.append({
|
||||
'file': filepath,
|
||||
'issues': {
|
||||
'curly': (open_curly, close_curly),
|
||||
'block': (open_block, close_block),
|
||||
'comment': (open_comment, close_comment)
|
||||
}
|
||||
})
|
||||
|
||||
# Find incomplete Jinja2 expressions
|
||||
line_num = 1
|
||||
for line in content.split('\n'):
|
||||
# Check for unclosed Jinja expressions
|
||||
line_open_curly = line.count('{{')
|
||||
line_close_curly = line.count('}}')
|
||||
if line_open_curly != line_close_curly and '{{' in line:
|
||||
problem_files.append({
|
||||
'file': filepath,
|
||||
'line': line_num,
|
||||
'content': line.strip(),
|
||||
'issue': f"Unbalanced curly braces: {line_open_curly} opening vs {line_close_curly} closing"
|
||||
})
|
||||
line_num += 1
|
||||
|
||||
except Exception as e:
|
||||
problem_files.append({
|
||||
'file': filepath,
|
||||
'error': str(e)
|
||||
})
|
||||
|
||||
return problem_files
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) > 1:
|
||||
directory = sys.argv[1]
|
||||
else:
|
||||
directory = 'app/templates'
|
||||
|
||||
problems = check_template_syntax(directory)
|
||||
|
||||
if problems:
|
||||
print(f"Found {len(problems)} potential issues:")
|
||||
for problem in problems:
|
||||
print(f"\nFile: {problem['file']}")
|
||||
if 'line' in problem:
|
||||
print(f"Line {problem['line']}: {problem['content']}")
|
||||
print(f"Issue: {problem['issue']}")
|
||||
elif 'issues' in problem:
|
||||
issues = problem['issues']
|
||||
if issues['curly'][0] != issues['curly'][1]:
|
||||
print(f"Unbalanced curly braces: {issues['curly'][0]} opening vs {issues['curly'][1]} closing")
|
||||
if issues['block'][0] != issues['block'][1]:
|
||||
print(f"Unbalanced block tags: {issues['block'][0]} opening vs {issues['block'][1]} closing")
|
||||
if issues['comment'][0] != issues['comment'][1]:
|
||||
print(f"Unbalanced comments: {issues['comment'][0]} opening vs {issues['comment'][1]} closing")
|
||||
else:
|
||||
print(f"Error: {problem['error']}")
|
||||
else:
|
||||
print("No issues found!")
|
Loading…
Add table
Add a link
Reference in a new issue