| 1 | package Filter::Undent; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | push @ISA, 'Exporter'; | |||||||||
| 7 | 1 | @EXPORT = qw(undent); | |||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Filter::Simple; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | =head1 NAME | ||||||||||
| 14 | |||||||||||
| 15 | Filter::Undent - Un-indent heredoc strings automatically | ||||||||||
| 16 | |||||||||||
| 17 | =cut | ||||||||||
| 18 | |||||||||||
| 19 | 1 | our $VERSION = '1.0.3'; | |||||||||
| 20 | |||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||
| 22 | |||||||||||
| 23 | Don't you wish heredocs could align with your docs? Now they can! | ||||||||||
| 24 | |||||||||||
| 25 | use Filter::Undent; | ||||||||||
| 26 | print <<'EOF' | ||||||||||
| 27 | What is printed is magically undented to the level of the | ||||||||||
| 28 | first line of the heredoc. | ||||||||||
| 29 | |||||||||||
| 30 | Only these lines will be indented in the output, since they | ||||||||||
| 31 | are indented relative to the first line | ||||||||||
| 32 | EOF | ||||||||||
| 33 | |||||||||||
| 34 | If you want to disable the unindent of the heredocs, simply: | ||||||||||
| 35 | |||||||||||
| 36 | no Filter::Undent; | ||||||||||
| 37 | |||||||||||
| 38 | =cut | ||||||||||
| 39 | |||||||||||
| 40 | FILTER_ONLY | ||||||||||
| 41 | 6 | ▶ | quotelike => sub {s{^<<}{undent <<}gs}, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 1 | ▶ | all => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=10.5
| |||||||||||
| 43 | 1 | ▶ | return unless $Filter::Undent::DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | print STDERR join '', map {"Filter::Undent> $_\n"} split /\n/, $_; | |||||||||
| 45 | }, | ||||||||||
| 46 | 1 | ; | |||||||||
| 47 | |||||||||||
| 48 | =head1 EXPORTED FUNCTIONS | ||||||||||
| 49 | |||||||||||
| 50 | =head2 undent | ||||||||||
| 51 | |||||||||||
| 52 | This function does the actual work of unindenting. It returns the modified | ||||||||||
| 53 | version of the input string, ignoring any leading newlines, and if the first | ||||||||||
| 54 | line of the provided string is indented with space or tab characters, it will | ||||||||||
| 55 | remove the same whitespace from the beginning of all of the subsequent lines | ||||||||||
| 56 | in the output. Any lines which are outdented from the first line, or is using | ||||||||||
| 57 | a different combination of spaces or tabs will not have its leading space | ||||||||||
| 58 | removed. | ||||||||||
| 59 | |||||||||||
| 60 | =cut | ||||||||||
| 61 | |||||||||||
| 62 | 2 | ▶ | sub undent ($) { | ||||||||
|
Subroutine
undent: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 63 | 1 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 64 | 2 | ▶ | if ( $_[0] =~ m/^(\r?\n)*([ \t]+)/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 2 | my $i = $2; | |||||||||
| 66 | 2 | return join '', map { s/^\Q$i\E/$1/g; $_ } grep { $_ ne '' } | |||||||||
| 67 | split /(.*?\n)/, $_[0]; | ||||||||||
| 68 | } | ||||||||||
| 69 | 0 | return $_[0]; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | =head1 AUTHOR | ||||||||||
| 73 | |||||||||||
| 74 | Anthony Kilna, C<< <anthony at kilna.com> >> - L<http://anthony.kilna.com> | ||||||||||
| 75 | |||||||||||
| 76 | =head1 BUGS | ||||||||||
| 77 | |||||||||||
| 78 | Please report any bugs or feature requests to C<bug-filter-undent at rt.cpan.org>, or through | ||||||||||
| 79 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Filter-Undent>. I will be notified, and then you'll | ||||||||||
| 80 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 81 | |||||||||||
| 82 | =head1 SUPPORT | ||||||||||
| 83 | |||||||||||
| 84 | You can find documentation for this module with the perldoc command. | ||||||||||
| 85 | |||||||||||
| 86 | perldoc Filter::Undent | ||||||||||
| 87 | |||||||||||
| 88 | |||||||||||
| 89 | You can also look for information at: | ||||||||||
| 90 | |||||||||||
| 91 | =over 4 | ||||||||||
| 92 | |||||||||||
| 93 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||
| 94 | |||||||||||
| 95 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Filter-Undent> | ||||||||||
| 96 | |||||||||||
| 97 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 98 | |||||||||||
| 99 | L<http://annocpan.org/dist/Filter-Undent> | ||||||||||
| 100 | |||||||||||
| 101 | =item * CPAN Ratings | ||||||||||
| 102 | |||||||||||
| 103 | L<http://cpanratings.perl.org/d/Filter-Undent> | ||||||||||
| 104 | |||||||||||
| 105 | =item * Search CPAN | ||||||||||
| 106 | |||||||||||
| 107 | L<http://search.cpan.org/dist/Filter-Undent/> | ||||||||||
| 108 | |||||||||||
| 109 | =back | ||||||||||
| 110 | |||||||||||
| 111 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 112 | |||||||||||
| 113 | Copyright 2012 Kilna Companies. | ||||||||||
| 114 | |||||||||||
| 115 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 116 | under the terms of either: the GNU General Public License as published | ||||||||||
| 117 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 118 | |||||||||||
| 119 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 120 | |||||||||||
| 121 | =cut | ||||||||||
| 122 | |||||||||||
| 123 | 1 | 1; # End of Filter::Undent | |||||||||