1 |
|
---|
2 | package Text::Tabs;
|
---|
3 |
|
---|
4 | require Exporter;
|
---|
5 |
|
---|
6 | @ISA = (Exporter);
|
---|
7 | @EXPORT = qw(expand unexpand $tabstop);
|
---|
8 |
|
---|
9 | use vars qw($VERSION $tabstop $debug);
|
---|
10 | $VERSION = 2005.0824;
|
---|
11 |
|
---|
12 | use strict;
|
---|
13 |
|
---|
14 | BEGIN {
|
---|
15 | $tabstop = 8;
|
---|
16 | $debug = 0;
|
---|
17 | }
|
---|
18 |
|
---|
19 | sub expand {
|
---|
20 | my @l;
|
---|
21 | my $pad;
|
---|
22 | for ( @_ ) {
|
---|
23 | my $s = '';
|
---|
24 | for (split(/^/m, $_, -1)) {
|
---|
25 | my $offs = 0;
|
---|
26 | s{\t}{
|
---|
27 | $pad = $tabstop - (pos() + $offs) % $tabstop;
|
---|
28 | $offs += $pad - 1;
|
---|
29 | " " x $pad;
|
---|
30 | }eg;
|
---|
31 | $s .= $_;
|
---|
32 | }
|
---|
33 | push(@l, $s);
|
---|
34 | }
|
---|
35 | return @l if wantarray;
|
---|
36 | return $l[0];
|
---|
37 | }
|
---|
38 |
|
---|
39 | sub unexpand
|
---|
40 | {
|
---|
41 | my (@l) = @_;
|
---|
42 | my @e;
|
---|
43 | my $x;
|
---|
44 | my $line;
|
---|
45 | my @lines;
|
---|
46 | my $lastbit;
|
---|
47 | for $x (@l) {
|
---|
48 | @lines = split("\n", $x, -1);
|
---|
49 | for $line (@lines) {
|
---|
50 | $line = expand($line);
|
---|
51 | @e = split(/(.{$tabstop})/,$line,-1);
|
---|
52 | $lastbit = pop(@e);
|
---|
53 | $lastbit = '' unless defined $lastbit;
|
---|
54 | $lastbit = "\t"
|
---|
55 | if $lastbit eq " "x$tabstop;
|
---|
56 | for $_ (@e) {
|
---|
57 | if ($debug) {
|
---|
58 | my $x = $_;
|
---|
59 | $x =~ s/\t/^I\t/gs;
|
---|
60 | print "sub on '$x'\n";
|
---|
61 | }
|
---|
62 | s/ +$/\t/;
|
---|
63 | }
|
---|
64 | $line = join('',@e, $lastbit);
|
---|
65 | }
|
---|
66 | $x = join("\n", @lines);
|
---|
67 | }
|
---|
68 | return @l if wantarray;
|
---|
69 | return $l[0];
|
---|
70 | }
|
---|
71 |
|
---|
72 | 1;
|
---|
73 | __END__
|
---|
74 |
|
---|
75 | sub expand
|
---|
76 | {
|
---|
77 | my (@l) = @_;
|
---|
78 | for $_ (@l) {
|
---|
79 | 1 while s/(^|\n)([^\t\n]*)(\t+)/
|
---|
80 | $1. $2 . (" " x
|
---|
81 | ($tabstop * length($3)
|
---|
82 | - (length($2) % $tabstop)))
|
---|
83 | /sex;
|
---|
84 | }
|
---|
85 | return @l if wantarray;
|
---|
86 | return $l[0];
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | =head1 NAME
|
---|
91 |
|
---|
92 | Text::Tabs -- expand and unexpand tabs per the unix expand(1) and unexpand(1)
|
---|
93 |
|
---|
94 | =head1 SYNOPSIS
|
---|
95 |
|
---|
96 | use Text::Tabs;
|
---|
97 |
|
---|
98 | $tabstop = 4;
|
---|
99 | @lines_without_tabs = expand(@lines_with_tabs);
|
---|
100 | @lines_with_tabs = unexpand(@lines_without_tabs);
|
---|
101 |
|
---|
102 | =head1 DESCRIPTION
|
---|
103 |
|
---|
104 | Text::Tabs does about what the unix utilities expand(1) and unexpand(1)
|
---|
105 | do. Given a line with tabs in it, expand will replace the tabs with
|
---|
106 | the appropriate number of spaces. Given a line with or without tabs in
|
---|
107 | it, unexpand will add tabs when it can save bytes by doing so. Invisible
|
---|
108 | compression with plain ascii!
|
---|
109 |
|
---|
110 | =head1 BUGS
|
---|
111 |
|
---|
112 | expand doesn't handle newlines very quickly -- do not feed it an
|
---|
113 | entire document in one string. Instead feed it an array of lines.
|
---|
114 |
|
---|
115 | =head1 LICENSE
|
---|
116 |
|
---|
117 | Copyright (C) 1996-2002,2005 David Muir Sharnoff.
|
---|
118 | Copyright (C) 2005 Aristotle Pagaltzis
|
---|
119 | This module may be modified, used, copied, and redistributed at your own risk.
|
---|
120 | Publicly redistributed modified versions must use a different name.
|
---|
121 |
|
---|