| blib/lib/Text/Livedoor/Wiki/Block.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 90 | 90 | 100.0 |
| branch | 26 | 26 | 100.0 |
| condition | 3 | 3 | 100.0 |
| subroutine | 13 | 13 | 100.0 |
| pod | 8 | 8 | 100.0 |
| total | 140 | 140 | 100.0 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package Text::Livedoor::Wiki::Block; | ||||||
| 2 | |||||||
| 3 | 10 | 10 | 56 | use warnings; | |||
| 10 | 22 | ||||||
| 10 | 319 | ||||||
| 4 | 10 | 10 | 55 | use strict; | |||
| 10 | 19 | ||||||
| 10 | 287 | ||||||
| 5 | 10 | 10 | 13498 | use UNIVERSAL::require; | |||
| 10 | 17774 | ||||||
| 10 | 107 | ||||||
| 6 | 10 | 10 | 283 | use Scalar::Util; | |||
| 10 | 23 | ||||||
| 10 | 11648 | ||||||
| 7 | |||||||
| 8 | sub new { | ||||||
| 9 | 9 | 9 | 1 | 27 | my $class = shift; | ||
| 10 | 9 | 24 | my $self = shift; | ||||
| 11 | 9 | 38 | $self = bless $self , $class; | ||||
| 12 | 9 | 67 | Scalar::Util::weaken($self->{inline}); | ||||
| 13 | 9 | 54 | $self->_load( $self->{block_plugins} ); | ||||
| 14 | 9 | 52 | return $self; | ||||
| 15 | } | ||||||
| 16 | 234 | 234 | 1 | 737 | sub blocks { shift->{blocks} } | ||
| 17 | 92 | 92 | 1 | 487 | sub trigger{ shift->{trigger} } | ||
| 18 | 83 | 83 | 1 | 430 | sub inline { shift->{inline}->parse(@_); } | ||
| 19 | 148 | 148 | 1 | 1149 | sub on_mobile{ shift->{on_mobile} } | ||
| 20 | |||||||
| 21 | #XXX only for footnote plugin for now | ||||||
| 22 | sub footer_section { | ||||||
| 23 | 90 | 90 | 1 | 190 | my $self = shift; | ||
| 24 | 90 | 199 | my $res = ''; | ||||
| 25 | 90 | 210 | my $footnotes = $Text::Livedoor::Wiki::scratchpad->{footnotes}; | ||||
| 26 | |||||||
| 27 | 90 | 100 | 534 | if( $footnotes ) { | |||
| 28 | 3 | 10 | $res =qq| | ||||
| 29 | 3 | 7 | my $cnt = 1; | ||||
| 30 | 3 | 11 | for ( @$footnotes ) { | ||||
| 31 | 4 | 25 | $res .=qq| |
||||
| 32 | 4 | 11 | $cnt++; | ||||
| 33 | } | ||||||
| 34 | 3 | 8 | $res .="
|
||||
| 35 | } | ||||||
| 36 | 90 | 393 | return $res; | ||||
| 37 | } | ||||||
| 38 | |||||||
| 39 | 992 | 992 | 1 | 2651 | sub opts { $Text::Livedoor::Wiki::opts } | ||
| 40 | sub parse { | ||||||
| 41 | 178 | 178 | 1 | 289 | my $self = shift; | ||
| 42 | 178 | 296 | my $text = shift; | ||||
| 43 | 178 | 329 | my $nested = shift; | ||||
| 44 | 178 | 412 | my $catalog_keeper = $self->opts->{catalog_keeper}; | ||||
| 45 | 178 | 872 | my $id_keeper = $self->opts->{id_keeper}; | ||||
| 46 | 178 | 637 | my $skip_catalog = $self->opts->{skip_catalog}; | ||||
| 47 | |||||||
| 48 | 178 | 1283 | my @lines = split("\n" ,$text , -1 ); | ||||
| 49 | 178 | 381 | my $html = ''; | ||||
| 50 | 178 | 374 | my @items = (); | ||||
| 51 | 178 | 304 | my $last = scalar @lines; | ||||
| 52 | 178 | 376 | my $cnt = 0; | ||||
| 53 | 178 | 278 | my $on_processing = 0; | ||||
| 54 | 178 | 226 | my $current_block ; | ||||
| 55 | |||||||
| 56 | 178 | 409 | $Text::Livedoor::Wiki::scratchpad->{core}{block_uid}++; | ||||
| 57 | LINE : | ||||||
| 58 | 178 | 342 | for ( @lines ) { | ||||
| 59 | 458 | 100 | 1354 | $Text::Livedoor::Wiki::scratchpad->{core}{current_pos}++ unless $nested; | |||
| 60 | #warn $Text::Livedoor::Wiki::scratchpad->{core}{current_pos} . ':' . $_ unless $nested; | ||||||
| 61 | 458 | 649 | $cnt++; | ||||
| 62 | 458 | 100 | 1617 | my $blocks_cache = $on_processing ? [$current_block] : $self->blocks ; | |||
| 63 | 458 | 1122 | my $id = $self->opts->{name} . '_block_' . $Text::Livedoor::Wiki::scratchpad->{core}{block_uid} ; | ||||
| 64 | 458 | 727 | for my $block ( @{$blocks_cache} ) { | ||||
| 458 | 1081 | ||||||
| 65 | 2391 | 100 | 23771 | if( my $res = $block->check($_ , { id => $id , on_next => 0 , inline => $self->{inline} } ) ) { | |||
| 66 | 373 | 578 | $current_block= $block; | ||||
| 67 | 373 | 551 | $on_processing = 1; | ||||
| 68 | 373 | 1177 | push @items, $res; | ||||
| 69 | 373 | 100 | 803 | if( $last != $cnt ) { | |||
| 70 | 287 | 100 | 1755 | unless( my $next_res = $block->check($lines[$cnt] , { id => $id , on_next => 1 , inline => $self->{inline} } ) ) { | |||
| 71 | 62 | 100 | 193 | $html =~ s/ \n$// unless $Text::Livedoor::Wiki::scratchpad->{skip_ajust_block_break}; |
|||
| 72 | 62 | 141 | $Text::Livedoor::Wiki::scratchpad->{skip_ajust_block_break} = 0; | ||||
| 73 | 62 | 100 | 201 | $html .= $self->on_mobile ? $block->mobile( $self, $self->{inline} , \@items) : $block->get( $self, $self->{inline} , \@items ); | |||
| 74 | 62 | 202 | @items = (); | ||||
| 75 | 62 | 152 | $Text::Livedoor::Wiki::scratchpad->{core}{block_uid}++; | ||||
| 76 | 62 | 149 | $on_processing = 0; | ||||
| 77 | } | ||||||
| 78 | } | ||||||
| 79 | else { | ||||||
| 80 | #$html =~ s/ \n$//; |
||||||
| 81 | 86 | 100 | 481 | $html =~ s/ \n$// unless $Text::Livedoor::Wiki::scratchpad->{skip_ajust_block_break}; |
|||
| 82 | 86 | 100 | 277 | $html .= $self->on_mobile ? $block->mobile( $self, $self->{inline} , \@items) : $block->get( $self , $self->{inline} , \@items ); | |||
| 83 | 86 | 333 | @items = (); | ||||
| 84 | 86 | 381 | $Text::Livedoor::Wiki::scratchpad->{core}{block_uid}++; | ||||
| 85 | 86 | 191 | $on_processing = 0; | ||||
| 86 | } | ||||||
| 87 | 373 | 2177 | next LINE; | ||||
| 88 | } | ||||||
| 89 | } | ||||||
| 90 | |||||||
| 91 | 83 | 431 | my $text= $self->inline($_) ; | ||||
| 92 | 83 | 100 | 253 | if( length $text ) { | |||
| 93 | 69 | 289 | $html .= $text . qq| \n|; |
||||
| 94 | } | ||||||
| 95 | else { | ||||||
| 96 | 14 | 41 | $html .= qq| \n|; |
||||
| 97 | } | ||||||
| 98 | } | ||||||
| 99 | 176 | 419 | $on_processing = 0; # in case | ||||
| 100 | |||||||
| 101 | #hoge <- remove this |
||||||
| 102 | 176 | 519 | $html =~ s/ \n$//; |
||||
| 103 | |||||||
| 104 | 176 | 100 | 100 | 1236 | if( $html && !($html =~ /\n$/) ) { | ||
| 105 | 44 | 153 | $html .= "\n"; | ||||
| 106 | } | ||||||
| 107 | |||||||
| 108 | 176 | 1122 | return $html; | ||||
| 109 | } | ||||||
| 110 | sub _load { | ||||||
| 111 | 9 | 9 | 20 | my $self = shift; | |||
| 112 | 9 | 28 | my $plugins = shift; | ||||
| 113 | 9 | 21 | my $trigger = {}; | ||||
| 114 | |||||||
| 115 | 9 | 33 | for (@$plugins ) { | ||||
| 116 | 81 | 1313 | $_->require; | ||||
| 117 | 81 | 100 | 1688 | $trigger->{$_} = $_->trigger if $_->trigger; | |||
| 118 | } | ||||||
| 119 | 9 | 137 | $self->{blocks} = $plugins; | ||||
| 120 | 9 | 30 | $self->{trigger} = $trigger; | ||||
| 121 | |||||||
| 122 | } | ||||||
| 123 | |||||||
| 124 | 1; | ||||||
| 125 | |||||||
| 126 | =head1 NAME | ||||||
| 127 | |||||||
| 128 | Text::Livedoor::Wiki::Block - Block Parser | ||||||
| 129 | |||||||
| 130 | =head1 DESCRIPTION | ||||||
| 131 | |||||||
| 132 | block parser. supporting block has block :-) | ||||||
| 133 | |||||||
| 134 | =head1 METHOD | ||||||
| 135 | |||||||
| 136 | =head2 blocks | ||||||
| 137 | |||||||
| 138 | =head2 footer_section | ||||||
| 139 | |||||||
| 140 | =head2 inline | ||||||
| 141 | |||||||
| 142 | =head2 new | ||||||
| 143 | |||||||
| 144 | =head2 on_mobile | ||||||
| 145 | |||||||
| 146 | =head2 opts | ||||||
| 147 | |||||||
| 148 | =head2 parse | ||||||
| 149 | |||||||
| 150 | =head2 trigger | ||||||
| 151 | |||||||
| 152 | =head1 AUTHOR | ||||||
| 153 | |||||||
| 154 | polocky | ||||||
| 155 | |||||||
| 156 | =cut |