| 1 | #============================================================= -*-Perl-*- | ||||||||||
| 2 | # | ||||||||||
| 3 | # Template::Plugin::Autoformat | ||||||||||
| 4 | # | ||||||||||
| 5 | # DESCRIPTION | ||||||||||
| 6 | # Plugin interface to Damian Conway's Text::Autoformat module. | ||||||||||
| 7 | # | ||||||||||
| 8 | # AUTHORS | ||||||||||
| 9 | # Robert McArthur <mcarthur@dstc.edu.au> | ||||||||||
| 10 | # - original plugin code | ||||||||||
| 11 | # | ||||||||||
| 12 | # Andy Wardley <abw@wardley.org> | ||||||||||
| 13 | # - added FILTER registration, support for forms and some additional | ||||||||||
| 14 | # documentation | ||||||||||
| 15 | # | ||||||||||
| 16 | # COPYRIGHT | ||||||||||
| 17 | # Copyright (C) 2000-2008 Robert McArthur, Andy Wardley. | ||||||||||
| 18 | # All Rights Reserved. | ||||||||||
| 19 | # | ||||||||||
| 20 | # This module is free software; you can redistribute it and/or | ||||||||||
| 21 | # modify it under the same terms as Perl itself. | ||||||||||
| 22 | # | ||||||||||
| 23 | #============================================================================ | ||||||||||
| 24 | |||||||||||
| 25 | package Template::Plugin::Autoformat; | ||||||||||
| 26 | |||||||||||
| 27 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 2 | ▶ | use base 'Template::Plugin'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 2 | ▶ | use Text::Autoformat; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | |||||||||||
| 32 | 2 | our $VERSION = '2.79'; | |||||||||
| 33 | |||||||||||
| 34 | 23 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 35 | 23 | my ( $class, $context, $options ) = @_; | |||||||||
| 36 | 23 | my $filter_factory; | |||||||||
| 37 | my $plugin; | ||||||||||
| 38 | |||||||||||
| 39 | 23 | ▶ | if ($options) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | |||||||||||
| 41 | # create a closure to generate filters with additional options | ||||||||||
| 42 | 4 | ▶ | $filter_factory = sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 43 | 4 | my $context = shift; | |||||||||
| 44 | 4 | ▶ | my $filtopt = ref $_[-1] eq 'HASH' ? pop : {}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 4 | @$filtopt{ keys %$options } = values %$options; | |||||||||
| 46 | 4 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 4 | _tt_autoformat( @_, $filtopt ); | |||||||||
| 48 | 4 | }; | |||||||||
| 49 | 12 | }; | |||||||||
| 50 | |||||||||||
| 51 | # and a closure to represent the plugin | ||||||||||
| 52 | 16 | ▶ | $plugin = sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 53 | 16 | ▶ | my $plugopt = ref $_[-1] eq 'HASH' ? pop : {}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 16 | @$plugopt{ keys %$options } = values %$options; | |||||||||
| 55 | 16 | _tt_autoformat( @_, $plugopt ); | |||||||||
| 56 | 12 | }; | |||||||||
| 57 | } | ||||||||||
| 58 | else { | ||||||||||
| 59 | # simple filter factory closure (no legacy options from constructor) | ||||||||||
| 60 | 5 | ▶ | $filter_factory = sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 61 | 5 | my $context = shift; | |||||||||
| 62 | 5 | ▶ | my $filtopt = ref $_[-1] eq 'HASH' ? pop : {}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 7 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 64 | 7 | _tt_autoformat( @_, $filtopt ); | |||||||||
| 65 | 5 | }; | |||||||||
| 66 | 11 | }; | |||||||||
| 67 | |||||||||||
| 68 | # plugin without options can be static | ||||||||||
| 69 | 11 | $plugin = \&_tt_autoformat; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | # now define the filter and return the plugin | ||||||||||
| 73 | 23 | $context->define_filter( 'Autoformat', [ $filter_factory => 1 ] ); | |||||||||
| 74 | 23 | return $plugin; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 33 | ▶ | sub _tt_autoformat { | ||||||||
|
Subroutine
_tt_autoformat: called CC=3 SCAR=11.0
| |||||||||||
| 78 | 33 | ▶ | my $options = ref $_[-1] eq 'HASH' ? pop : {}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 33 | my $form = $options->{form}; | |||||||||
| 80 | 33 | ▶ | my $out | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | = $form | ||||||||||
| 82 | ? Text::Autoformat::form( $options, $form, @_ ) | ||||||||||
| 83 | : Text::Autoformat::autoformat( join( '', @_ ), $options ); | ||||||||||
| 84 | 33 | return $out; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 2 | 1; | |||||||||
| 88 | |||||||||||
| 89 | __END__ | ||||||||||