File: demo_ellipsis.pl

package info (click to toggle)
libperl6-form-perl 0.090-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 584 kB
  • sloc: perl: 3,080; makefile: 8
file content (68 lines) | stat: -rw-r--r-- 1,843 bytes parent folder | download | duplicates (4)
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
use 5.010;
use warnings;

use Perl6::Form;

sub break_with_ellipsis {
    my ($breaker) = @_;

    # This generates the replacement line-breaking subroutine...
    return sub {
        my ($str_ref, $width, $ws) = @_;

        # Remember where we started this broken line...
        my $start_pos = pos $$str_ref;

        # Break it with the original breaker....
        my ($nextline, $more) = $breaker->($str_ref, $width, $ws);

        # If not at end-of-string...
        if ($more) {
            # If there's no room for the ellipsis...
            if (length $nextline > $width-3) {

                # Reset line to be (re)broken...
                pos $$str_ref = $start_pos;

                # Rebreak it with room for ellipsis...
                ($nextline, $more) = $breaker->($str_ref, $width-3, $ws);
            }

            # Add the ellipsis...
            $nextline .= '...';
        }

        return ($nextline, $more);
    }
}

use Perl6::Form {
    # Specify a new kind of field...
    field => [
        # Match fields with trailing ellipses...
        qr/ [{] [^}]* \.\.\. [}] /x

        # And do this with them...
        => sub {
                my ($match, $opts) = @_;

                # Temporarily wrap breaking algorithm with ellipsis appender...
                $opts->{break} = break_with_ellipsis( $opts->{break} );

                # Rewrite format (removing ellipses from it)...
                my $fieldtype = substr($match,1,-4);
                return '{' . $fieldtype . substr($fieldtype,-1) x 3 . '}';
            }
    ]
};


# Test it...
my $text = 'Now is the winter of our discontent made glorious summer by this son of York';

print form
    '|---------------------------|',
    ' {<<<<<<<<<<<<<<<<<<<<<<...}',
    $text,
    ' {VVVVVVVVVVVVVVVVVVVVVVVVV}',
    '|---------------------------|';