| 1 | package Text::Outdent; | ||||||||||
| 2 | 5 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 5 | $VERSION = 0.01; | |||||||||
| 5 | 5 | @EXPORT_OK = qw/ | |||||||||
| 6 | outdent | ||||||||||
| 7 | outdent_all | ||||||||||
| 8 | outdent_quote | ||||||||||
| 9 | expand_leading_tabs | ||||||||||
| 10 | /; | ||||||||||
| 11 | 5 | $EXPORT_TAGS{ALL} = \@EXPORT_OK; | |||||||||
| 12 | |||||||||||
| 13 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 5 | ▶ | use base 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | { | ||||||||||
| 17 | 5 | my $indent_re = qr/^([^\S\n]*(?=\S))/m; | |||||||||
| 18 | 3 | ▶ | sub find_indention { | ||||||||
|
Subroutine
find_indention: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||
| 19 | 3 | my ($str) = @_; | |||||||||
| 20 | |||||||||||
| 21 | 3 | ▶ | return '' if $str !~ /\S/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | |||||||||||
| 23 | 3 | $str =~ /$indent_re/g; | |||||||||
| 24 | 3 | my $i = $1; | |||||||||
| 25 | 3 | ▶ | return '' if not length $i; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | |||||||||||
| 27 | 3 | ▶ | while ($str =~ /$indent_re/g) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 12 | my $i2 = $1; | |||||||||
| 29 | 12 | ($i) = "$i\n$i2" =~ /^(.*).*\n\1/; | |||||||||
| 30 | 12 | ▶ | return '' if not length $i2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | } | ||||||||||
| 32 | 3 | return $i; | |||||||||
| 33 | } | ||||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | { | ||||||||||
| 37 | 5 | my $indent_re = qr/^(?!$)([^\S\n]*)/m; | |||||||||
| 38 | 4 | ▶ | sub find_indention_all { | ||||||||
|
Subroutine
find_indention_all: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||
| 39 | 4 | my ($str) = @_; | |||||||||
| 40 | |||||||||||
| 41 | 4 | $str =~ /$indent_re/g; | |||||||||
| 42 | 4 | my $i = $1; | |||||||||
| 43 | 4 | ▶ | return '' if not length $i; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | |||||||||||
| 45 | 4 | ▶ | while ($str =~ /$indent_re/g) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 25 | my $i2 = $1; | |||||||||
| 47 | 25 | ($i) = "$i\n$i2" =~ /^(.*).*\n\1/; | |||||||||
| 48 | 25 | ▶ | return '' if not length $i2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | } | ||||||||||
| 50 | 4 | return $i; | |||||||||
| 51 | } | ||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 5 | ▶ | sub outdent_all { | ||||||||
|
Subroutine
outdent_all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 55 | 4 | my ($str) = @_; | |||||||||
| 56 | |||||||||||
| 57 | 4 | my $indent = find_indention_all($str); | |||||||||
| 58 | 4 | $str =~ s/^$indent//gm; | |||||||||
| 59 | |||||||||||
| 60 | 4 | return $str; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 3 | ▶ | sub outdent { | ||||||||
|
Subroutine
outdent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 64 | 3 | my ($str) = @_; | |||||||||
| 65 | |||||||||||
| 66 | 3 | my $indent = find_indention($str); | |||||||||
| 67 | 3 | $str =~ s/^$indent(?=.*?\S)//gm; | |||||||||
| 68 | |||||||||||
| 69 | 3 | return $str; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | # Removes any leading whitespaces uptil and including the first whitespace. | ||||||||||
| 73 | # Removes any trailing whitespaces if they come directly after a newline. | ||||||||||
| 74 | # (In this paragraph "whitespace" excludes newline.) | ||||||||||
| 75 | 10 | ▶ | sub heredocify { | ||||||||
|
Subroutine
heredocify: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 76 | 10 | my ($str) = @_; | |||||||||
| 77 | |||||||||||
| 78 | 10 | $str =~ s/^[^\S\n]*\n//; | |||||||||
| 79 | 10 | $str =~ s/^[^\S\n]+\z//m; | |||||||||
| 80 | |||||||||||
| 81 | 10 | return $str; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 0 | ▶ | sub outdent_quote { outdent(heredocify($_[0])) } | ||||||||
|
Subroutine
outdent_quote: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 85 | |||||||||||
| 86 | 1 | ▶ | sub expand_leading_tabs { | ||||||||
|
Subroutine
expand_leading_tabs: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 87 | 1 | my ($tabstop, $str) = @_; | |||||||||
| 88 | |||||||||||
| 89 | 1 | ▶ | 1 while $str =~ s{ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | ^ ([^\S\t\n]*?) (\t+) | ||||||||||
| 91 | }{ | ||||||||||
| 92 | 8 | $1 . ' ' x ($tabstop * length($2) - length($1) % $tabstop) | |||||||||
| 93 | }gemx; | ||||||||||
| 94 | |||||||||||
| 95 | 1 | return $str; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | 5 | 1; | |||||||||
| 99 | |||||||||||
| 100 | __END__ | ||||||||||