File Coverage

blib/lib/Kwiki/Formatter/CaptionedImage.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Kwiki::Formatter::CaptionedImage;
2              
3 1     1   27910 use warnings;
  1         4  
  1         43  
4 1     1   7 use strict;
  1         3  
  1         46  
5 1     1   1565 use Kwiki::Plugin '-Base';
  0            
  0            
6             use mixin 'Kwiki::Installer';
7             our $VERSION = '0.01';
8              
9             const class_title => 'Captioned Image';
10             const class_id => 'captionedimage';
11             const css_file => 'captioned_image.css';
12              
13             sub register {
14             my $registry = shift;
15             $registry->add(preload => $self->class_id);
16             $registry->add(hook => 'formatter:all_blocks',
17             post => 'add_note_to_list',
18             );
19             }
20              
21             sub init {
22             super;
23             my $formatter = $self->hub->load_class('formatter');
24             $formatter->table->{capimage} = 'Kwiki::Formatter::CaptionedImage::Block';
25             }
26              
27             sub add_note_to_list {
28             return [('capimage', @{$_[-1]->returned})];
29             }
30              
31             package Kwiki::Formatter::CaptionedImage::Block;
32             use Spoon::Base -Base;
33             use base 'Spoon::Formatter::Block';
34              
35             const formatter_id => 'capimage';
36             const pattern_block =>
37             qr{^\[(?:\s*([^\|]+)\|\s*)?(?:\s*([^\]]+)\s+)?(\w+:(?://|\?)[^\]\s]+)(?:\s+([^\]]+)\s*)?\]\n}m;
38              
39             field wrapper_float => '';
40             field target => '';
41             field float => '';
42             const html_end => q{
}; 43               44             sub match { 45             return unless $self->text =~ $self->pattern_block; 46             $self->start_offset($-[0]); 47             $self->end_offset($+[0]); 48             my ($float, $text1, $target, $text2) = ($1, $2, $3, $4); 49             return unless $target =~ /(?:jpe?g|gif|png)$/i; 50               51             $self->target($self->normalize_target($target)); 52             $float = '' unless defined $float; 53             $self->wrapper_float(' style="text-align: center"') if $float eq ''; 54             $float = $float ne '' 55             ? "float: $float" 56             : "margin-left: auto; margin-right: auto"; 57             $float = " style=\"$float\" "; 58             $self->float($float); 59             $text1 = '' unless defined $text1; 60             $text2 = '' unless defined $text2; 61             my $text = $text1 . ' ' . $text2; 62             $text =~ s/^\s*(.*?)\s*$/$1/; 63             $text = $target unless $text =~ /\S/; 64             $self->text($text); 65             return 1; 66             } 67               68             sub html_start { 69             my ( $wrapper_float, $float, $target ) = 70             ( $self->wrapper_float, $self->float, $self->target ); 71             return < 72            
73            
74            
75             HTML
76             }
77              
78             sub normalize_target {
79             my ( $target ) = @_;
80             if( $target =~ /^img:\/\/(?:([^\/]+)\/)?(.+)$/i ) {
81             my $page = defined $1 ? $1 : $self->hub->pages->current_id;
82             my $file = $2;
83             $target = "plugin/attachments/$page/$file";
84             }
85             return $target;
86             }
87              
88             package Kwiki::Formatter::CaptionedImage;
89             1; # End of Kwiki::Formatter::CaptionedImage;
90              
91             __DATA__