Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 256 lines (166 sloc) 5.412 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
#!/usr/bin/perl
use Mason::Tidy::App;
use strict;
use warnings;

Mason::Tidy::App->run();

1;

__END__

=head1 NAME

masontidy - Tidy HTML::Mason / Mason components

=head1 SYNOPSIS

Tidy component, write to standard output:

% masontidy -m [1|2] file.mc

Tidy component(s) in place:

% masontidy -m [1|2] -r file1.mc [file2.mc ...]

Tidy standard input, write to standard output:

% masontidy -m [1|2] -p|--pipe < file.mc

=head1 DESCRIPTION

masontidy tidies L<Mason 1|HTML::Mason> and L<Mason 2|Mason> components, using
L<perltidy|perltidy> to format the Perl code that can be embedded in various
places in the component. masontidy does not (yet) attempt to tidy the HTML or
other non-Perl content in a component.

For example, this:

<body>
%if($contents||$allow_empty) {
<ul>
%foreach my $line (@lines) {
%chomp($line);
<li>
<%2+(3-4)*6%>
</li>
<li><% foo($.bar,$.baz, $.bleah)%></li>
%}
</ul>
%}
</body>
<%init>
my @articles = @{Blog::Article::Manager->get_articles(sort_by=>"create_time",limit=>5)};
</%init>

becomes this:

<body>
% if ( $contents || $allow_empty ) {
<ul>
% foreach my $line (@lines) {
% chomp($line);
<li>
<% 2 + ( 3 - 4 ) * 6 %>
</li>
<li><% foo( $.bar, $.baz, $.bleah) %></li>
% }
</ul>
%}
</body>

<%init>
my @articles =
@{ Blog::Article::Manager->get_articles
( sort_by => "create_time", limit => 5 ) };
</%init>

=head2 What gets tidied

=over

=item *

B<%-lines and C<< <%perl> >> blocks>. These are indented relative to each other
regardless of intervening non-Perl content, e.g. note the indentation of the
C<foreach> and C<chomp> lines above.

=item *

B<Other code blocks>. C<< <%init> >>, C<< <%once> >>, C<< <%class> >>, and C<<
<%filter> >> blocks are tidied in isolation from one another.

=item *

B<Perl expressions> inside C<< <% %> >> and C<< <& &> >> tags.

=back

=head1 INVOKING

There are three ways of invoking C<masontidy>:

=over

=item *

Specify a single file; the result will be written to standard output.

=item *

Specify one or more files with the -r/--replace flag; each file will be tidied
in place.

=item *

Specify -p/--pipe; content from standard input will be tidied and written to
standard output.

=back

For more advanced options, consider using C<masontidy> with L<tidyall|tidyall>;
it will let you write to files with a separate extension, backup files before
overwriting, etc.

=head1 COMMAND-LINE OPTIONS

You can specify default options in the C<MASONTIDY_OPT> environment variable,
e.g.

MASONTIDY_OPT="-m 2"

=over

=item -m, --mason-version

Mason major version - 1 or 2. Required. Put this in C<MASONTIDY_OPT> if you
only ever use one version on your system.

=item -r, --replace

Modify file(s) in place instead of sending to standard output.

=item --indent-perl-block

Number of spaces to initially indent all lines inside C<< <%perl> >> blocks.
The default is 2, so as to align with %-lines:

% my $foo = get_foo();
<%perl>
if ($foo) {
$bar = 6;
}
</%perl>

With --indent-perl-block 0:

% my $foo = get_foo();
<%perl>
if ($foo) {
$bar = 6;
}
</%perl>

Note that this is independent from perltidy's indentation settings.

=item --indent-block

Number of spaces to initially indent all lines inside code blocks other than
C<< <%perl> >> (C<< <%init> >>, C<< <%class> >>, C<< <%once> >>, and C<<
<%filter> >>). The default is 0:

<%init>
if ($foo) {
$bar = 6;
}
</%init>

With --indent-block 2:

<%init>
if ($foo) {
$bar = 6;
}
</%init>

=item --perltidy-argv

C<perltidy> arguments to use everywhere. e.g.

--perltidy-argv="-noll -l=78"

or

--perltidy-argv " -noll -l=78"

=item --perltidy-line-argv

Additional C<perltidy> arguments to use for Perl lines. "-fnl -fbl" will always
be used, to preserve existing newlines.

If this is set to "NONE", then lines will not be tidied.

=item --perltidy-block-argv

Additional C<perltidy> arguments to use for code blocks.

If this is set to "NONE", then blocks will not be tidied.

=item --perltidy-tag-argv

Additional C<perltidy> arguments to use for C<< <% %> >> and C<< <& &> >> tags.
"-fnl -fbl" will always be used, to preserve existing newlines.

If this is set to "NONE", then tags will not be tidied.

=item -h, --help

Print help message

=back

=head1 ERRORS

Will throw a fatal error if a file cannot be tidied, such as when perltidy
encounters bad Perl syntax. However, C<masontidy> is not intended to be, and
should not be considered, a validator; it will remain silent on many syntax
errors.

=head1 LIBRARY API

You can use the L<Mason::Tidy|Mason::Tidy> API from inside another Perl
script/library instead of calling out to this script.

=head1 CAVEATS / KNOWN BUGS

=over

=item *

C<< <%perl> >> and C<< </%perl> >> tags must be on their own line, or else
their inner content will not be tidied.

=item *

C<< <% %> >> tags that span multiple lines are ignored.

=item *

The line structure of C<< %-lines >>, C<< <% %> >> tags, C<< <& &> >> and C<<
<%perl> >> blocks will not be altered; i.e. multiple lines will not be merged
and single lines will not be split, regardless of their length.

=back

=cut
Something went wrong with that request. Please try again.