line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $File: //member/autrijus/Lingua-ZH-Wrap/Wrap.pm $ $Author: autrijus $ |
2
|
|
|
|
|
|
|
# $Revision: #3 $ $Change: 4527 $ $DateTime: 2003/03/03 03:17:20 $ |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package Lingua::ZH::Wrap; |
5
|
|
|
|
|
|
|
$Lingua::ZH::Wrap::VERSION = '0.03'; |
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
1522
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
90
|
|
8
|
2
|
|
|
2
|
|
11
|
use vars qw($VERSION @ISA @EXPORT $columns $overflow); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
191
|
|
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
13
|
use Exporter; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
1365
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Lingua::ZH::Wrap - Wrap Chinese text |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 VERSION |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
This document describes version 0.03 of Lingua::ZH::Wrap, released |
19
|
|
|
|
|
|
|
July 25, 2004. |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 SYNOPSIS |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head2 Example 1 |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
use Lingua::ZH::Wrap; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
$initial_tab = "\t"; # Tab before first line |
28
|
|
|
|
|
|
|
$subsequent_tab = ""; # All other lines flush left |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
print wrap( $initial_tab, $subsequent_tab, @lines ); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head2 Example 2 |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
use Lingua::ZH::Wrap qw(wrap $columns $overflow); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
$columns = 75; # Change columns |
37
|
|
|
|
|
|
|
$overflow = 1; # Chinese char may occupy 76th col |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
print wrap( '', '', @lines ); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 DESCRIPTION |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
C is a very simple paragraph formatter. |
44
|
|
|
|
|
|
|
It formats a single paragraph at a time by breaking lines at Chinese |
45
|
|
|
|
|
|
|
character boundries. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
Indentation is controlled for the first line (C<$initial_tab>) and |
48
|
|
|
|
|
|
|
all subsequent lines (C<$subsequent_tab>) independently. Please note: |
49
|
|
|
|
|
|
|
C<$initial_tab> and C<$subsequent_tab> are the literal strings that will |
50
|
|
|
|
|
|
|
be used: it is unlikely you would want to pass in a number. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 OVERRIDES |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
C has a number of variables that control its |
55
|
|
|
|
|
|
|
behavior. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
Lines are wrapped at C<$Lingua::ZH::Wrap::columns> columns; if a Chinese |
58
|
|
|
|
|
|
|
character just extends columns by one byte, it will be wrapped into the |
59
|
|
|
|
|
|
|
next line, unless C<$Lingua::ZH::Wrap::overflow> is set to a true value. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 CAVEATS |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
The algorithm doesn't care about breaking non-Chinese words. Also, |
64
|
|
|
|
|
|
|
if you pass in strings encoded unicode, it will currently first decode |
65
|
|
|
|
|
|
|
into C, do the conversion, then convert back. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Patches are, of course, very welcome; in particular, I'd like to use |
68
|
|
|
|
|
|
|
L to avoid beginning-of-line punctuations, as well |
69
|
|
|
|
|
|
|
as employing other semantic-sensitive formatting techniques. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=cut |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
74
|
|
|
|
|
|
|
@EXPORT = qw(wrap $overflow $columns); |
75
|
|
|
|
|
|
|
$columns = 72; |
76
|
|
|
|
|
|
|
$overflow = 0; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
require Encode if $] >= 5.008; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub wrap { |
81
|
8
|
100
|
66
|
8
|
0
|
1330
|
if ($] >= 5.008 and Encode::is_utf8($_[2])) { |
82
|
12
|
100
|
|
|
|
50
|
return Encode::decode(big5 => _wrap(map { |
83
|
4
|
|
|
|
|
15
|
Encode::is_utf8($_) ? Encode::encode(big5 => $_) : $_ |
84
|
|
|
|
|
|
|
} @_)); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
4
|
|
|
|
|
10
|
return _wrap(@_); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub _wrap { |
91
|
8
|
|
|
8
|
|
6200
|
my ($init, $subs) = (shift, shift); |
92
|
|
|
|
|
|
|
|
93
|
8
|
50
|
|
|
|
27
|
return join("\n", map(_wrap($init, $subs, $_), @_)) if @_ > 1; |
94
|
|
|
|
|
|
|
|
95
|
8
|
|
|
|
|
14
|
my $str = shift; |
96
|
8
|
50
|
|
|
|
26
|
return join("\n", map(_wrap($init, $subs, $_), split("\n", $str))) |
97
|
|
|
|
|
|
|
if (index($str, "\n") > -1); # Handles single-line only |
98
|
|
|
|
|
|
|
|
99
|
8
|
|
|
|
|
16
|
my ($fin, $pos) = ($columns - 1, 0); |
100
|
|
|
|
|
|
|
|
101
|
8
|
|
|
|
|
25
|
$str = "$init$str"; |
102
|
|
|
|
|
|
|
|
103
|
8
|
|
|
|
|
10
|
do { |
104
|
48
|
|
|
|
|
199
|
$str =~ m/\G.{0,$fin}[\x00-\x7f]/go; |
105
|
48
|
|
66
|
|
|
214
|
$pos += $columns + (((pos($str) ||= $pos) + $pos + $columns) % 2) |
106
|
|
|
|
|
|
|
* (!!$overflow * 2 - 1); |
107
|
48
|
100
|
|
|
|
154
|
return $str if $pos >= length($str); |
108
|
40
|
|
|
|
|
175
|
substr($str, $pos, 0, "\n$subs"); |
109
|
|
|
|
|
|
|
} while (pos($str) = ++$pos); |
110
|
|
|
|
|
|
|
|
111
|
0
|
|
|
|
|
|
return $str; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
1; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 SEE ALSO |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
L |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 AUTHORS |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Autrijus Tang Eautrijus@autrijus.orgE |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 COPYRIGHT |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Copyright 2003, 2004 by Autrijus Tang Eautrijus@autrijus.orgE. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
129
|
|
|
|
|
|
|
under the same terms as Perl itself. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
See L |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=cut |