Big tidy up - now runs perltidy for enbedded perl - still not complete though
This commit is contained in:
parent
f12699e804
commit
2570c004e8
144
README.md
144
README.md
@ -4,55 +4,125 @@ This Python program formats Mojolicious template files to make their structure e
|
||||
|
||||
## Features
|
||||
|
||||
- Proper indentation of HTML tags
|
||||
- Formatting of Mojolicious command lines (% lines)
|
||||
- Proper handling of Perl code blocks
|
||||
- Special handling for Mojolicious-specific syntax (form_for, content_for, etc.)
|
||||
- Ensures space after % when required
|
||||
- Customizable indentation size
|
||||
- Smart handling of non-indenting tags (br, hr, img, input, etc.)
|
||||
- Special handling for lines with multiple closing tags
|
||||
- **HTML Tag Indentation**: Properly indents HTML tags and nested elements
|
||||
- **Mojolicious Command Formatting**: Formats Mojolicious command lines (% lines) with proper spacing
|
||||
- **Perl Code Block Formatting**: Intelligently handles Perl code blocks and their nesting
|
||||
- **Special Syntax Handling**: Special handling for Mojolicious-specific syntax (form_for, content_for blocks)
|
||||
- **Embedded Perl Formatting**: Uses perltidy to format embedded Perl code blocks
|
||||
- **Smart Tag Handling**: Special handling for self-closing tags, minimal indentation for span/p tags
|
||||
- **Customizable Indentation**: Configurable indentation size (default: 4 spaces)
|
||||
- **Perltidy Output Files**: Option to save original and formatted Perl code to separate files
|
||||
|
||||
## Installation
|
||||
|
||||
No installation is required. The formatter is a standalone Python script that can be run directly.
|
||||
|
||||
### Requirements
|
||||
|
||||
- Python 3.6 or higher
|
||||
- perltidy (for Perl code formatting)
|
||||
|
||||
To install perltidy:
|
||||
|
||||
```bash
|
||||
sudo apt-get install perltidy
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```bash
|
||||
# Basic usage
|
||||
./mojo_formatter_final.py input_file.mojo > output_file.mojo
|
||||
|
||||
# Read from stdin and write to stdout
|
||||
cat input_file.mojo | ./mojo_formatter_final.py > output_file.mojo
|
||||
|
||||
# Specify custom indentation size (default is 4 spaces)
|
||||
./mojo_formatter_final.py --indent 8 input_file.mojo > output_file.mojo
|
||||
|
||||
# Show help
|
||||
./mojo_formatter_final.py --help
|
||||
./mojo_formatter_final_fixed8.py input_file.mojo > output_file.mojo
|
||||
```
|
||||
|
||||
## Examples
|
||||
### With Custom Indentation
|
||||
|
||||
The formatter can handle various Mojolicious template constructs, including:
|
||||
```bash
|
||||
./mojo_formatter_final_fixed8.py --indent 2 input_file.mojo > output_file.mojo
|
||||
```
|
||||
|
||||
- HTML tags
|
||||
- Mojolicious command lines (% lines)
|
||||
- Perl code blocks
|
||||
- Form blocks
|
||||
- Content blocks
|
||||
- Embedded Perl expressions
|
||||
- Special HTML elements like `<br>`, `<hr>`, `<img>`, etc.
|
||||
### With Perltidy Output Files
|
||||
|
||||
## Requirements
|
||||
```bash
|
||||
./mojo_formatter_final_fixed8.py --perltidy-output-dir=/path/to/output/dir input_file.mojo > output_file.mojo
|
||||
```
|
||||
|
||||
- Python 3.x
|
||||
- No external dependencies required
|
||||
### With Debug Logging
|
||||
|
||||
```bash
|
||||
./mojo_formatter_final_fixed8.py --debug input_file.mojo > output_file.mojo
|
||||
```
|
||||
|
||||
## How It Works
|
||||
|
||||
The formatter uses regular expressions to identify different elements in the Mojolicious template:
|
||||
The formatter processes Mojolicious template files in several passes:
|
||||
|
||||
1. Mojolicious command lines (starting with %)
|
||||
2. HTML tags
|
||||
3. Perl code blocks
|
||||
4. Special Mojolicious constructs (form_for, content_for)
|
||||
1. **Embedded Perl Processing**: Extracts and formats embedded Perl code using perltidy
|
||||
2. **Line-by-Line Processing**: Processes each line based on its type (HTML or Mojolicious command)
|
||||
3. **Post-Processing**: Handles special cases like multiple closing tags on a single line
|
||||
4. **Duplicate Tag Cleanup**: Normalizes and removes duplicate closing tags
|
||||
|
||||
It then applies appropriate indentation based on the nesting level of these elements, with special handling for non-indenting tags and multiple closing tags.
|
||||
## Special Features
|
||||
|
||||
### Smart HTML Tag Handling
|
||||
|
||||
- Non-indenting tags like `<br>`, `<hr>`, `<img>`, etc. don't cause indentation changes
|
||||
- Minimal indentation for `<span>` and `<p>` tags (half the normal indentation)
|
||||
- Special handling for lines with multiple closing tags
|
||||
|
||||
### Embedded Perl Formatting
|
||||
|
||||
The formatter uses perltidy to format embedded Perl code blocks (enclosed in `<%` and `%>` tags). This ensures that your Perl code follows consistent formatting rules.
|
||||
|
||||
### Perltidy Output Files
|
||||
|
||||
When using the `--perltidy-output-dir` option, the formatter saves both the original and formatted Perl code for each embedded Perl block to separate files:
|
||||
|
||||
- `perl_block_N_original.pl`: The original Perl code before formatting
|
||||
- `perl_block_N_formatted.pl`: The formatted Perl code after perltidy processing
|
||||
|
||||
## Example
|
||||
|
||||
### Input
|
||||
|
||||
```perl
|
||||
<div>
|
||||
<%
|
||||
# This is a test of pure Perl code with minimal indentation
|
||||
if ($status) {
|
||||
$c->desktopBackupRecordStatus($backup_rec, 'pre-backup', $status);
|
||||
return ($c->l('bac_OPERATION_STATUS_REPORT').$c->l('bac_ERR_PRE_BACKUP'));
|
||||
}
|
||||
|
||||
my $clvl = $c->stash('compressionlevel');
|
||||
my $cmd = "/bin/tar --create --file=- --directory / @{$c->stash('exclude')} "
|
||||
. "@{$c->stash('directories')} | /usr/bin/gzip $clvl ";
|
||||
%>
|
||||
</div>
|
||||
```
|
||||
|
||||
### Output
|
||||
|
||||
```perl
|
||||
<div>
|
||||
<%
|
||||
# This is a test of pure Perl code with minimal indentation
|
||||
if ($status) {
|
||||
$c->desktopBackupRecordStatus($backup_rec, 'pre-backup', $status);
|
||||
return ($c->l('bac_OPERATION_STATUS_REPORT') . $c->l('bac_ERR_PRE_BACKUP'));
|
||||
}
|
||||
my $clvl = $c->stash('compressionlevel');
|
||||
my $cmd = "/bin/tar --create --file=- --directory / @{$c->stash('exclude')} "
|
||||
. "@{$c->stash('directories')} | /usr/bin/gzip $clvl ";
|
||||
%>
|
||||
</div>
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
If you encounter issues with perltidy, the formatter will fall back to a simple indentation-based formatter for Perl code. Enable debug logging with the `--debug` flag to see detailed information about the formatting process.
|
||||
|
||||
## License
|
||||
|
||||
This software is provided as-is, without any warranties or conditions of any kind.
|
182
manus request
Normal file
182
manus request
Normal file
@ -0,0 +1,182 @@
|
||||
I am wanting you to create a python3 programm to take as input a Mojolicious Template file and output the same contents, but formatted in a way to make it structure easily understandable by a human.
|
||||
This involves identifying all html tags, mojolicious command and helper commands and also all perl constructs and indenting them in a such a way that subservient html and other statements are indented like an html prettifier, taking into account that the file may also include perl program statements which also should be properly formatted and indented to show the structure like perltidy. All "%" in the first non space character position in the line should be followed by a space unless followed by a specific mojolicious character such as "=".
|
||||
you can use any python3 libraries that you see fit.
|
||||
here is an example of a mojolicious template file which needs re-structuring:
|
||||
```
|
||||
% layout 'default', title => "Sme server 2 - yum_install";
|
||||
% content_for 'module' => begin
|
||||
<div id='module' class='module yuminstall-panel'>
|
||||
% if (config->{debug} == 1) {
|
||||
<p>
|
||||
%= dumper $c->current_route
|
||||
%= dumper $yum_datas
|
||||
</p>
|
||||
% }
|
||||
|
||||
<h1><%= $title%></h1>
|
||||
|
||||
% if ( $notif ) {
|
||||
<br><div class=sme-error>
|
||||
%= $notif
|
||||
</div>
|
||||
%}
|
||||
<br>
|
||||
|
||||
%= form_for 'yumd' => (method => 'POST') => begin
|
||||
|
||||
%=l 'yum_HEADER_AVAILABLE_SOFTWARE'
|
||||
<br><br>
|
||||
% if ($c->non_empty('available','group')) {
|
||||
%=l 'yum_DESC_AVAILABLE_GROUPS'
|
||||
<p><span class=label>
|
||||
%=l 'yum_LABEL_AVAILABLE_GROUPS'
|
||||
</span><span class=data>
|
||||
% param 'SelectedGroups' => $c->get_names2('updates','group') unless param 'SelectedGroups';
|
||||
%= select_field 'SelectedGroups' => $c->get_options2('available', 'group'), class => 'input', multiple => "1"
|
||||
</span></p>
|
||||
%}
|
||||
<br>
|
||||
% if ($c->non_empty('available','packages')) {
|
||||
%=l 'yum_DESC_AVAILABLE_PACKAGES'
|
||||
<p><span class=label>
|
||||
%=l 'yum_LABEL_AVAILABLE_PACKAGES'
|
||||
</span><span class=data>
|
||||
% param 'SelectedPackages' => $c->get_names2('updates','package') unless param 'SelectedPackages';
|
||||
%= select_field 'SelectedPackages' => $c->get_options2('available', 'package'), class => 'input', multiple => "1"
|
||||
</span></p>
|
||||
%}
|
||||
<!-- $c->print_skip_header() -->
|
||||
%= hidden_field 'trt' => 'INST'
|
||||
<br><br>
|
||||
<!-- <div class='center'>-->
|
||||
%= submit_button $c->l('yum_INSTALL_SOFTWARE'), class => 'action'
|
||||
<!--</div>-->
|
||||
% end
|
||||
</div>
|
||||
% end
|
||||
|
||||
```
|
||||
and here is the same example restructured:
|
||||
```
|
||||
% layout 'default', title => "Sme server 2 - yum_install";
|
||||
% content_for 'module' => begin
|
||||
<div id='module' class='module yuminstall-panel'>
|
||||
% if (config->{debug} == 1) {
|
||||
<p>
|
||||
%= dumper $c->current_route
|
||||
%= dumper $yum_datas
|
||||
</p>
|
||||
% }
|
||||
<h1><%= $title%></h1>
|
||||
% if ( $notif ) {<br>
|
||||
<div class=sme-error>
|
||||
%= $notif
|
||||
</div>
|
||||
%}
|
||||
<br>
|
||||
%= form_for 'yumd' => (method => 'POST') => begin
|
||||
%=l 'yum_HEADER_AVAILABLE_SOFTWARE'
|
||||
<br><br>
|
||||
% if ($c->non_empty('available','group')) {
|
||||
%=l 'yum_DESC_AVAILABLE_GROUPS'
|
||||
<p><span class=label>
|
||||
%=l 'yum_LABEL_AVAILABLE_GROUPS'
|
||||
</span><span class=data>
|
||||
% param 'SelectedGroups' => $c->get_names2('updates','group') unless param 'SelectedGroups';
|
||||
%= select_field 'SelectedGroups' => $c->get_options2('available', 'group'), class => 'input', multiple => "1"
|
||||
</span></p>
|
||||
%}
|
||||
<br>
|
||||
% if ($c->non_empty('available','packages')) {
|
||||
%=l 'yum_DESC_AVAILABLE_PACKAGES'
|
||||
<p><span class=label>
|
||||
%=l 'yum_LABEL_AVAILABLE_PACKAGES'
|
||||
</span><span class=data>
|
||||
% param 'SelectedPackages' => $c->get_names2('updates','package') unless param 'SelectedPackages';
|
||||
%= select_field 'SelectedPackages' => $c->get_options2('available', 'package'), class => 'input', multiple => "1"
|
||||
</span></p>
|
||||
%}
|
||||
<!-- $c->print_skip_header() -->
|
||||
%= hidden_field 'trt' => 'INST'
|
||||
<br><br>
|
||||
<!-- <div class='center'>-->
|
||||
%= submit_button $c->l('yum_INSTALL_SOFTWARE'), class => 'action'
|
||||
<!--</div>-->
|
||||
% end
|
||||
</div>
|
||||
% end
|
||||
```
|
||||
Give me the whole program with the following second example unformatted template file ready to run as a test
|
||||
|
||||
```
|
||||
<div id='dom_upd'>
|
||||
% my $btn = l('ADD');
|
||||
%= form_for '/domains2' => (method => 'POST') => begin
|
||||
<p>
|
||||
<h2>
|
||||
% if ( $dom_datas->{trt} eq "ADD" ) {
|
||||
%=l 'dom_CREATE_TITLE'
|
||||
% } else {
|
||||
%=l 'dom_MODIFY_TITLE'
|
||||
% $btn = l('MODIFY');
|
||||
% }
|
||||
</h2>
|
||||
</p>
|
||||
<p><br>
|
||||
<span class=label>
|
||||
%=l 'DOMAIN_NAME', class => 'label'
|
||||
</span><span class=data>
|
||||
% if ( $dom_datas->{trt} eq "ADD" ) {
|
||||
% param 'Domain' => $dom_datas->{domain} unless param 'Domain';
|
||||
%= text_field 'Domain', class => 'input'
|
||||
% } else {
|
||||
%= hidden_field 'Domain' => $dom_datas->{domain}
|
||||
%= $dom_datas->{domain}, class => 'data'
|
||||
% }
|
||||
</span>
|
||||
</p>
|
||||
<p><br>
|
||||
<span class=label>
|
||||
%=l 'DESCRIPTION_BRIEF', class => 'label'
|
||||
</span><span class=data>
|
||||
% param 'Description' => $dom_datas->{description} unless param 'Description';
|
||||
%= text_field 'Description', class => 'input'
|
||||
</span>
|
||||
</p>
|
||||
<p><br>
|
||||
%=l 'dom_CONTENT_FIELD_DESCRIPTION'
|
||||
<br>
|
||||
<span class=label>
|
||||
%= $c->l('dom_CONTENT', '');
|
||||
</span><span class=data>
|
||||
% param 'Content' => $dom_datas->{content} unless param 'Content';
|
||||
%= select_field 'Content', $c->content_options_list(), class => 'input'
|
||||
</span>
|
||||
</p>
|
||||
<p><br>
|
||||
%=l 'dom_DESC_NAMESERVERS'
|
||||
<br>
|
||||
<span class=label>
|
||||
%=l 'dom_LABEL_NAMESERVERS', class => 'label'
|
||||
</span><span class=data>
|
||||
% param 'Nameservers' => $dom_datas->{nameservers} unless param 'Nameservers';
|
||||
%= select_field 'Nameservers', $c->nameserver_options_list(), class => 'input'
|
||||
</span>
|
||||
</p>
|
||||
<p><br>
|
||||
%= submit_button "$btn", class => 'action'
|
||||
</p>
|
||||
%= hidden_field 'trt' => $dom_datas->{trt}
|
||||
%end
|
||||
</div>
|
||||
```
|
||||
|
||||
Further thoughts:
|
||||
|
||||
Some files have this iosrt of structure are <%
|
||||
...perl...
|
||||
%>
|
||||
The perl needs to be formatted accordingly.
|
||||
|
||||
and Delete all blank lines and put all final tags (where the content is indented) on a line on its own.
|
||||
Example is</p> </span> </dev> and others
|
@ -5,20 +5,74 @@ Mojolicious Template Formatter
|
||||
This program formats Mojolicious template files to make their structure
|
||||
easily understandable by humans. It properly indents HTML tags, Mojolicious
|
||||
commands, helper commands, and Perl constructs.
|
||||
|
||||
Uses perltidy for formatting embedded Perl code and can output perltidy results
|
||||
to a separate file for inspection.
|
||||
"""
|
||||
|
||||
import re
|
||||
import sys
|
||||
import argparse
|
||||
import subprocess
|
||||
import tempfile
|
||||
import os
|
||||
import logging
|
||||
import uuid
|
||||
import platform
|
||||
|
||||
|
||||
# Version information
|
||||
VERSION = "1.0"
|
||||
PROGRAM_NAME = "Mojolicious Template Formatter"
|
||||
|
||||
# Configure logging
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
handlers=[
|
||||
logging.StreamHandler(sys.stderr)
|
||||
]
|
||||
)
|
||||
logger = logging.getLogger('mojo_formatter')
|
||||
|
||||
|
||||
def get_python_version():
|
||||
"""Get the current Python version."""
|
||||
return f"{platform.python_version()}"
|
||||
|
||||
|
||||
def get_perltidy_version():
|
||||
"""Get the installed perltidy version."""
|
||||
try:
|
||||
# Run the perltidy command
|
||||
result = subprocess.run(['perltidy', '-v'], capture_output=True, text=True)
|
||||
if result.returncode == 0:
|
||||
# Extract version from stdout
|
||||
version_match = re.search(r'This is perltidy, (v[\d\.]+)', result.stdout)
|
||||
if version_match:
|
||||
return version_match.group(1)
|
||||
return "Unknown version"
|
||||
else:
|
||||
return "Not available"
|
||||
except Exception:
|
||||
return "Not installed"
|
||||
|
||||
def log_system_info():
|
||||
"""Log system information including program version and dependencies."""
|
||||
python_version = get_python_version()
|
||||
perltidy_version = get_perltidy_version()
|
||||
|
||||
logger.info(f"{PROGRAM_NAME} v{VERSION}")
|
||||
logger.info(f"Running with Python {python_version}")
|
||||
logger.info(f"Perltidy {perltidy_version}")
|
||||
|
||||
class MojoTemplateFormatter:
|
||||
"""
|
||||
A formatter for Mojolicious template files that makes their structure
|
||||
easily understandable by humans.
|
||||
"""
|
||||
|
||||
def __init__(self, indent_size=4):
|
||||
def __init__(self, indent_size=4, perltidy_output_dir=None):
|
||||
"""Initialize the formatter with default settings."""
|
||||
self.indent_size = indent_size
|
||||
self.current_indent = 0
|
||||
@ -27,6 +81,9 @@ class MojoTemplateFormatter:
|
||||
self.html_tag_stack = []
|
||||
self.in_form_block = False
|
||||
self.in_content_block = False
|
||||
self.remove_blank_lines = True
|
||||
self.perltidy_output_dir = perltidy_output_dir
|
||||
self.perltidy_block_count = 0
|
||||
|
||||
# Patterns for Mojolicious syntax
|
||||
self.mojo_command_pattern = re.compile(r'^(\s*)(%\s*.*?)$')
|
||||
@ -36,6 +93,10 @@ class MojoTemplateFormatter:
|
||||
self.form_block_start_pattern = re.compile(r'%=\s*form_for\b.*?=>\s*begin\b')
|
||||
self.perl_block_end_pattern = re.compile(r'%\s*}')
|
||||
self.perl_end_pattern = re.compile(r'%\s*end\b')
|
||||
|
||||
# Embedded Perl patterns
|
||||
self.embedded_perl_start_pattern = re.compile(r'<%')
|
||||
self.embedded_perl_end_pattern = re.compile(r'%>')
|
||||
self.mojo_expression_pattern = re.compile(r'<%=?=?\s*(.*?)\s*%>')
|
||||
self.mojo_code_pattern = re.compile(r'<%\s*(.*?)\s*%>')
|
||||
self.mojo_comment_pattern = re.compile(r'<%#\s*(.*?)\s*%>')
|
||||
@ -45,6 +106,9 @@ class MojoTemplateFormatter:
|
||||
self.html_close_tag_pattern = re.compile(r'</([a-zA-Z][a-zA-Z0-9]*)>')
|
||||
self.html_self_closing_tag_pattern = re.compile(r'<([a-zA-Z][a-zA-Z0-9]*)[^>]*/>')
|
||||
|
||||
# Pattern for multiple closing tags on a line
|
||||
self.multiple_closing_tags_pattern = re.compile(r'(</[^>]+>)(\s*)(</[^>]+>)')
|
||||
|
||||
# List of tags that shouldn't cause indentation changes
|
||||
self.non_indenting_tags = ['br', 'hr', 'img', 'input', 'link', 'meta']
|
||||
|
||||
@ -73,6 +137,12 @@ class MojoTemplateFormatter:
|
||||
Returns:
|
||||
str: The formatted content.
|
||||
"""
|
||||
logger.info("Starting formatting process")
|
||||
|
||||
# First pass: process embedded Perl blocks
|
||||
logger.info("Processing embedded Perl blocks")
|
||||
content = self._preprocess_embedded_perl(content)
|
||||
|
||||
lines = content.splitlines()
|
||||
self.output_lines = []
|
||||
self.current_indent = 0
|
||||
@ -81,14 +151,16 @@ class MojoTemplateFormatter:
|
||||
self.in_form_block = False
|
||||
self.in_content_block = False
|
||||
|
||||
logger.info("Processing lines for HTML and Mojolicious commands")
|
||||
i = 0
|
||||
while i < len(lines):
|
||||
line = lines[i]
|
||||
i += 1
|
||||
|
||||
# Skip empty lines
|
||||
# Skip empty lines if remove_blank_lines is enabled
|
||||
if not line.strip():
|
||||
self.output_lines.append('')
|
||||
if not self.remove_blank_lines:
|
||||
self.output_lines.append('')
|
||||
continue
|
||||
|
||||
# Process the line based on its type
|
||||
@ -96,9 +168,284 @@ class MojoTemplateFormatter:
|
||||
self._process_mojo_command_line(line)
|
||||
else:
|
||||
self._process_html_line(line)
|
||||
|
||||
|
||||
# Second pass: handle closing tags on separate lines
|
||||
logger.info("Post-processing closing tags")
|
||||
self._postprocess_closing_tags()
|
||||
|
||||
logger.info("Formatting complete")
|
||||
return '\n'.join(self.output_lines)
|
||||
|
||||
def _preprocess_embedded_perl(self, content):
|
||||
"""
|
||||
Preprocess embedded Perl blocks to format the Perl code inside using perltidy.
|
||||
|
||||
Args:
|
||||
content (str): The content to preprocess.
|
||||
|
||||
Returns:
|
||||
str: The preprocessed content.
|
||||
"""
|
||||
# Find all embedded Perl blocks
|
||||
pattern = re.compile(r'<%\s*(.*?)\s*%>', re.DOTALL)
|
||||
|
||||
def format_perl_code(match):
|
||||
perl_code = match.group(1)
|
||||
if not perl_code.strip():
|
||||
logger.debug("Empty Perl block found")
|
||||
return f"<%\n%>"
|
||||
|
||||
# Format the Perl code by adding indentation
|
||||
lines = perl_code.splitlines()
|
||||
if len(lines) <= 1:
|
||||
# For single-line Perl, just clean up spacing
|
||||
logger.debug("Single-line Perl block found")
|
||||
return f"<% {perl_code.strip()} %>"
|
||||
|
||||
# For multi-line Perl, use perltidy
|
||||
self.perltidy_block_count += 1
|
||||
block_id = self.perltidy_block_count
|
||||
logger.info(f"Found multi-line Perl block #{block_id} with {len(lines)} lines")
|
||||
logger.debug(f"Original Perl code (block #{block_id}):\n{perl_code}")
|
||||
|
||||
formatted_perl = self._run_perltidy(perl_code, block_id)
|
||||
|
||||
# If perltidy fails, fall back to our simple formatter
|
||||
if formatted_perl is None:
|
||||
logger.warning(f"Perltidy failed for block #{block_id}, falling back to simple formatter")
|
||||
formatted_lines = []
|
||||
current_indent = self.indent_size
|
||||
|
||||
for line in lines:
|
||||
if not line.strip():
|
||||
continue # Skip empty lines
|
||||
|
||||
stripped = line.lstrip()
|
||||
|
||||
# Check if this line decreases indentation (closing brace at start)
|
||||
if stripped.startswith('}') or stripped.startswith(');'):
|
||||
current_indent = max(self.indent_size, current_indent - self.indent_size)
|
||||
|
||||
# Add the line with proper indentation
|
||||
if stripped.startswith('#'):
|
||||
# For comments, use the current indentation
|
||||
formatted_lines.append(' ' * current_indent + stripped)
|
||||
else:
|
||||
formatted_lines.append(' ' * current_indent + stripped)
|
||||
|
||||
# Check if this line increases indentation for the next line
|
||||
if (stripped.endswith('{') or
|
||||
stripped.endswith('({') or
|
||||
stripped.endswith('sub {') or
|
||||
stripped.endswith('= {') or
|
||||
stripped.endswith('=> {') or
|
||||
(stripped.endswith('(') and not stripped.startswith(')'))):
|
||||
current_indent += self.indent_size
|
||||
|
||||
# Special case for closing parentheses that decrease indentation
|
||||
if stripped.endswith(');') and not stripped.startswith('('):
|
||||
current_indent = max(self.indent_size, current_indent - self.indent_size)
|
||||
|
||||
# Join the formatted lines with newlines
|
||||
formatted_perl = '\n'.join(formatted_lines)
|
||||
else:
|
||||
logger.info(f"Perltidy successfully formatted block #{block_id}")
|
||||
logger.debug(f"Perltidy formatted code (block #{block_id}):\n{formatted_perl}")
|
||||
|
||||
# Note: No space between % and > in the closing tag
|
||||
# IMPORTANT: Preserve the exact perltidy formatting
|
||||
return f"<%\n{formatted_perl}\n%>"
|
||||
|
||||
# Replace all embedded Perl blocks with formatted versions
|
||||
logger.info("Searching for embedded Perl blocks")
|
||||
result = pattern.sub(format_perl_code, content)
|
||||
logger.info(f"Embedded Perl block processing complete, found {self.perltidy_block_count} blocks")
|
||||
return result
|
||||
|
||||
def _run_perltidy(self, perl_code, block_id):
|
||||
"""
|
||||
Run perltidy on the given Perl code.
|
||||
|
||||
Args:
|
||||
perl_code (str): The Perl code to format.
|
||||
block_id (int): Identifier for this Perl block.
|
||||
|
||||
Returns:
|
||||
str: The formatted Perl code, or None if perltidy fails.
|
||||
"""
|
||||
try:
|
||||
logger.info(f"Running perltidy on Perl block #{block_id}")
|
||||
|
||||
# Create temporary files for input and output
|
||||
with tempfile.NamedTemporaryFile(mode='w+', delete=False) as input_file:
|
||||
input_file.write(perl_code)
|
||||
input_file_path = input_file.name
|
||||
logger.debug(f"Created temporary input file for block #{block_id}: {input_file_path}")
|
||||
|
||||
output_file_path = input_file_path + '.tidy'
|
||||
|
||||
# Run perltidy with our desired options
|
||||
cmd = [
|
||||
'perltidy',
|
||||
'-i=' + str(self.indent_size), # Set indentation size
|
||||
'-ci=' + str(self.indent_size), # Set continuation indentation
|
||||
'-l=120', # Line length
|
||||
'-pt=2', # Parenthesis tightness
|
||||
'-bt=2', # Brace tightness
|
||||
'-sbt=2', # Square bracket tightness
|
||||
'-ce', # Cuddled else
|
||||
'-nbl', # No blank lines before comments
|
||||
'-nsfs', # No space for semicolon
|
||||
input_file_path, # Input file
|
||||
'-o', output_file_path # Output file
|
||||
]
|
||||
|
||||
logger.debug(f"Executing perltidy command for block #{block_id}: {' '.join(cmd)}")
|
||||
|
||||
# Execute perltidy
|
||||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||
|
||||
# Check if perltidy succeeded
|
||||
if result.returncode != 0:
|
||||
logger.error(f"Perltidy failed for block #{block_id} with return code {result.returncode}")
|
||||
logger.error(f"Stderr: {result.stderr}")
|
||||
return None
|
||||
|
||||
# Read the formatted code
|
||||
if os.path.exists(output_file_path):
|
||||
with open(output_file_path, 'r') as output_file:
|
||||
formatted_code = output_file.read()
|
||||
logger.info(f"Perltidy output file size for block #{block_id}: {len(formatted_code)} bytes")
|
||||
|
||||
# If requested, save the perltidy output to a separate file
|
||||
if self.perltidy_output_dir:
|
||||
self._save_perltidy_output(perl_code, formatted_code, block_id)
|
||||
else:
|
||||
logger.error(f"Perltidy output file not found for block #{block_id}: {output_file_path}")
|
||||
return None
|
||||
|
||||
# Clean up temporary files
|
||||
logger.debug(f"Cleaning up temporary files for block #{block_id}")
|
||||
os.unlink(input_file_path)
|
||||
if os.path.exists(output_file_path):
|
||||
os.unlink(output_file_path)
|
||||
|
||||
return formatted_code.strip()
|
||||
|
||||
except Exception as e:
|
||||
logger.exception(f"Error running perltidy for block #{block_id}: {e}")
|
||||
return None
|
||||
|
||||
def _save_perltidy_output(self, original_code, formatted_code, block_id):
|
||||
"""
|
||||
Save the original and formatted Perl code to separate files for inspection.
|
||||
|
||||
Args:
|
||||
original_code (str): The original Perl code.
|
||||
formatted_code (str): The formatted Perl code.
|
||||
block_id (int): Identifier for this Perl block.
|
||||
"""
|
||||
try:
|
||||
# Create the output directory if it doesn't exist
|
||||
os.makedirs(self.perltidy_output_dir, exist_ok=True)
|
||||
|
||||
# Create filenames for the original and formatted code
|
||||
original_file = os.path.join(self.perltidy_output_dir, f"perl_block_{block_id}_original.pl")
|
||||
formatted_file = os.path.join(self.perltidy_output_dir, f"perl_block_{block_id}_formatted.pl")
|
||||
|
||||
# Write the original code to a file
|
||||
with open(original_file, 'w') as f:
|
||||
f.write(original_code)
|
||||
|
||||
# Write the formatted code to a file
|
||||
with open(formatted_file, 'w') as f:
|
||||
f.write(formatted_code)
|
||||
|
||||
logger.info(f"Saved perltidy input/output for block #{block_id} to {original_file} and {formatted_file}")
|
||||
|
||||
except Exception as e:
|
||||
logger.exception(f"Error saving perltidy output for block #{block_id}: {e}")
|
||||
|
||||
def _postprocess_closing_tags(self):
|
||||
"""
|
||||
Postprocess the output lines to put closing tags on separate lines.
|
||||
"""
|
||||
logger.info("Post-processing closing tags")
|
||||
result_lines = []
|
||||
i = 0
|
||||
|
||||
# Track if we're inside an embedded Perl block
|
||||
in_perl_block = False
|
||||
|
||||
while i < len(self.output_lines):
|
||||
line = self.output_lines[i]
|
||||
|
||||
# Check if we're entering an embedded Perl block
|
||||
if line.strip() == '<%':
|
||||
in_perl_block = True
|
||||
result_lines.append(line)
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# Check if we're exiting an embedded Perl block
|
||||
if line.strip() == '%>':
|
||||
in_perl_block = False
|
||||
result_lines.append(line)
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# If we're inside an embedded Perl block, don't modify the line
|
||||
if in_perl_block:
|
||||
result_lines.append(line)
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# Check for multiple closing tags
|
||||
if self.multiple_closing_tags_pattern.search(line):
|
||||
logger.debug(f"Found multiple closing tags in line: {line}")
|
||||
# Split the line at each closing tag
|
||||
parts = []
|
||||
current = line
|
||||
|
||||
while self.multiple_closing_tags_pattern.search(current):
|
||||
match = self.multiple_closing_tags_pattern.search(current)
|
||||
first_tag = match.group(1)
|
||||
whitespace = match.group(2)
|
||||
second_tag = match.group(3)
|
||||
|
||||
# Split at the second tag
|
||||
before_second = current[:match.start(3)]
|
||||
after_second = current[match.end(3):]
|
||||
|
||||
# Add the part before the second tag
|
||||
parts.append(before_second)
|
||||
|
||||
# Update current to be the second tag and everything after
|
||||
current = second_tag + after_second
|
||||
|
||||
# Add the last part
|
||||
if current:
|
||||
parts.append(current)
|
||||
|
||||
# Add all parts as separate lines
|
||||
base_indent = len(line) - len(line.lstrip())
|
||||
for j, part in enumerate(parts):
|
||||
# For closing tags, reduce indentation
|
||||
if j > 0 and part.strip().startswith('</'):
|
||||
indent = max(0, base_indent - self.indent_size)
|
||||
else:
|
||||
indent = base_indent
|
||||
|
||||
result_lines.append(' ' * indent + part.strip())
|
||||
logger.debug(f"Split line part {j+1}: {' ' * indent + part.strip()}")
|
||||
else:
|
||||
result_lines.append(line)
|
||||
|
||||
i += 1
|
||||
|
||||
self.output_lines = result_lines
|
||||
logger.info(f"Post-processing complete, {len(result_lines)} lines in output")
|
||||
|
||||
def _is_mojo_command_line(self, line):
|
||||
"""
|
||||
Check if the line is a Mojolicious command line (starts with %).
|
||||
@ -120,6 +467,7 @@ class MojoTemplateFormatter:
|
||||
line (str): The Mojolicious command line to process.
|
||||
"""
|
||||
stripped = line.lstrip()
|
||||
logger.debug(f"Processing Mojo command line: {stripped}")
|
||||
|
||||
# Ensure space after % if not followed by specific characters
|
||||
if stripped.startswith('%') and len(stripped) > 1 and stripped[1] not in ['=', '#', '%']:
|
||||
@ -128,6 +476,7 @@ class MojoTemplateFormatter:
|
||||
|
||||
# Check for content block start
|
||||
if self.content_block_start_pattern.search(stripped):
|
||||
logger.debug("Found content block start")
|
||||
indent = ' ' * self.current_indent
|
||||
formatted_line = indent + stripped
|
||||
self.output_lines.append(formatted_line)
|
||||
@ -137,6 +486,7 @@ class MojoTemplateFormatter:
|
||||
|
||||
# Check for form block start
|
||||
if self.form_block_start_pattern.search(stripped):
|
||||
logger.debug("Found form block start")
|
||||
indent = ' ' * self.current_indent
|
||||
formatted_line = indent + stripped
|
||||
self.output_lines.append(formatted_line)
|
||||
@ -146,6 +496,7 @@ class MojoTemplateFormatter:
|
||||
|
||||
# Handle Perl block opening
|
||||
if self.perl_block_start_pattern.search(stripped):
|
||||
logger.debug("Found Perl block start")
|
||||
indent = ' ' * self.current_indent
|
||||
formatted_line = indent + stripped
|
||||
self.output_lines.append(formatted_line)
|
||||
@ -157,6 +508,7 @@ class MojoTemplateFormatter:
|
||||
|
||||
# Handle Perl block closing with }
|
||||
if self.perl_block_end_pattern.search(stripped):
|
||||
logger.debug("Found Perl block end with }")
|
||||
if self.perl_block_stack:
|
||||
# Pop the indentation level from the stack
|
||||
self.current_indent = self.perl_block_stack.pop()
|
||||
@ -174,6 +526,7 @@ class MojoTemplateFormatter:
|
||||
|
||||
# Handle Perl block closing with end
|
||||
if self.perl_end_pattern.search(stripped):
|
||||
logger.debug("Found Perl block end with 'end'")
|
||||
if self.in_form_block and not self.perl_block_stack:
|
||||
self.in_form_block = False
|
||||
self.current_indent = max(0, self.current_indent - self.indent_size)
|
||||
@ -202,10 +555,25 @@ class MojoTemplateFormatter:
|
||||
Args:
|
||||
line (str): The HTML line to process.
|
||||
"""
|
||||
# Special handling for embedded Perl blocks
|
||||
if line.strip().startswith('<%'):
|
||||
# For embedded Perl blocks, don't modify the indentation
|
||||
# Just add the line as is to preserve perltidy formatting
|
||||
self.output_lines.append(line)
|
||||
return
|
||||
|
||||
# Special handling for Perl block closing tag
|
||||
if line.strip() == '%>':
|
||||
# For the closing tag, don't add any space after %
|
||||
self.output_lines.append('%>')
|
||||
return
|
||||
|
||||
stripped = line.lstrip()
|
||||
logger.debug(f"Processing HTML line: {stripped[:30]}...")
|
||||
|
||||
# Special handling for lines with <br></span></p> pattern or variations
|
||||
if self.br_span_p_pattern.search(stripped) or self.br_span_space_p_pattern.search(stripped):
|
||||
logger.debug("Found <br></span></p> pattern")
|
||||
# Find the base indentation level for this paragraph
|
||||
base_indent = 0
|
||||
for i in range(len(self.html_tag_stack)):
|
||||
@ -246,6 +614,7 @@ class MojoTemplateFormatter:
|
||||
|
||||
# Special handling for lines with <br> and closing tags
|
||||
if self.br_with_close_tags_pattern.search(stripped):
|
||||
logger.debug("Found <br> with closing tags")
|
||||
# Find appropriate indentation level
|
||||
indent_level = self.current_indent
|
||||
for tag in self.html_close_tag_pattern.findall(stripped):
|
||||
@ -266,6 +635,7 @@ class MojoTemplateFormatter:
|
||||
|
||||
# Skip indentation changes for lines with only non-indenting tags
|
||||
if self._contains_only_non_indenting_tags(stripped):
|
||||
logger.debug("Found line with only non-indenting tags")
|
||||
indent = ' ' * self.current_indent
|
||||
formatted_line = indent + stripped
|
||||
self.output_lines.append(formatted_line)
|
||||
@ -273,6 +643,7 @@ class MojoTemplateFormatter:
|
||||
|
||||
# Special handling for lines with multiple closing tags
|
||||
if self.multiple_close_tags_pattern.search(stripped):
|
||||
logger.debug("Found line with multiple closing tags")
|
||||
# Count the number of closing tags
|
||||
close_count = len(self.html_close_tag_pattern.findall(stripped))
|
||||
# Reduce indentation once for the whole line
|
||||
@ -347,18 +718,25 @@ class MojoTemplateFormatter:
|
||||
return False
|
||||
|
||||
|
||||
def format_mojolicious_template(content, indent_size=4):
|
||||
def format_mojolicious_template(content, indent_size=4, remove_blank_lines=True, log_level=logging.INFO, perltidy_output_dir=None):
|
||||
"""
|
||||
Format a Mojolicious template.
|
||||
|
||||
Args:
|
||||
content (str): The content of the Mojolicious template.
|
||||
indent_size (int): Number of spaces to use for indentation.
|
||||
remove_blank_lines (bool): Whether to remove blank lines.
|
||||
log_level (int): Logging level to use.
|
||||
perltidy_output_dir (str): Directory to save perltidy input/output files.
|
||||
|
||||
Returns:
|
||||
str: The formatted content.
|
||||
"""
|
||||
formatter = MojoTemplateFormatter(indent_size)
|
||||
# Set the logging level
|
||||
logger.setLevel(log_level)
|
||||
|
||||
formatter = MojoTemplateFormatter(indent_size, perltidy_output_dir)
|
||||
formatter.remove_blank_lines = remove_blank_lines
|
||||
return formatter.format(content)
|
||||
|
||||
|
||||
@ -371,11 +749,39 @@ def main():
|
||||
default=sys.stdout, help='Output file (default: stdout)')
|
||||
parser.add_argument('--indent', type=int, default=4,
|
||||
help='Number of spaces to use for indentation (default: 4)')
|
||||
parser.add_argument('--keep-blank-lines', action='store_true',
|
||||
help='Keep blank lines in the output (default: remove blank lines)')
|
||||
parser.add_argument('--log-level', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
|
||||
default='INFO', help='Set the logging level (default: INFO)')
|
||||
parser.add_argument('--perltidy-output-dir', type=str,
|
||||
help='Directory to save perltidy input/output files for inspection')
|
||||
args = parser.parse_args()
|
||||
|
||||
# Set the log level based on the command-line argument
|
||||
log_level = getattr(logging, args.log_level)
|
||||
logger.setLevel(log_level)
|
||||
|
||||
# Log program and version information
|
||||
log_system_info()
|
||||
|
||||
logger.info(f"Starting formatter with indent={args.indent}, keep_blank_lines={args.keep_blank_lines}, log_level={args.log_level}")
|
||||
if args.perltidy_output_dir:
|
||||
logger.info(f"Perltidy output will be saved to: {args.perltidy_output_dir}")
|
||||
|
||||
content = args.input_file.read()
|
||||
formatted_content = format_mojolicious_template(content, args.indent)
|
||||
logger.info(f"Read {len(content)} bytes from input")
|
||||
|
||||
formatted_content = format_mojolicious_template(
|
||||
content,
|
||||
args.indent,
|
||||
remove_blank_lines=not args.keep_blank_lines,
|
||||
log_level=log_level,
|
||||
perltidy_output_dir=args.perltidy_output_dir
|
||||
)
|
||||
|
||||
logger.info(f"Writing {len(formatted_content)} bytes to output")
|
||||
args.output_file.write(formatted_content)
|
||||
logger.info("Formatting complete")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
Loading…
x
Reference in New Issue
Block a user