| 1 | package Muster::Hook::Include; | ||||||||||||||||||||||||||||||||
| 2 | 1 | $Muster::Hook::Include::VERSION = '0.96'; | |||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Mojo::Base 'Muster::Hook::Directives'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Muster::LeafFile; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Muster::Hooks; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | Muster::Hook::Include - Muster include-page directive | ||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | version 0.96 | ||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | L<Muster::Hook::Include> includes pages inside other pages. | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | L<Muster::Hook::Include> inherits all methods from L<Muster::Hook::Directives>. | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | =head2 register | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | Do some intialization. | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub register { | ||||||||||||||||||||||||||||||
|
Subroutine
register: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 31 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 32 | 0 | my $hookmaster = shift; | |||||||||||||||||||||||||||||||
| 33 | 0 | my $conf = shift; | |||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | 0 | $self->{metadb} = $hookmaster->{metadb}; | |||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | my $callback = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 38 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 0 | return $self->process(%args); | |||||||||||||||||||||||||||||||
| 41 | 0 | }; | |||||||||||||||||||||||||||||||
| 42 | # Don't do page inclusion in the scanning phase; | ||||||||||||||||||||||||||||||||
| 43 | # it would be too confusing. | ||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $hookmaster->add_hook('includepage' => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 45 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 0 | return $self->do_directives( | |||||||||||||||||||||||||||||||
| 48 | no_scan=>1, | ||||||||||||||||||||||||||||||||
| 49 | directive=>'includepage', | ||||||||||||||||||||||||||||||||
| 50 | call=>$callback, | ||||||||||||||||||||||||||||||||
| 51 | %args, | ||||||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||||||
| 53 | }, | ||||||||||||||||||||||||||||||||
| 54 | 0 | ); | |||||||||||||||||||||||||||||||
| 55 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 56 | } # register | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | =head2 process | ||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | Process page inclusion. | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub process { | ||||||||||||||||||||||||||||||
|
Subroutine
process: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 64 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 65 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 0 | my $leaf = $args{leaf}; | |||||||||||||||||||||||||||||||
| 68 | 0 | my $phase = $args{phase}; | |||||||||||||||||||||||||||||||
| 69 | 0 | my @p = @{$args{params}}; | |||||||||||||||||||||||||||||||
| 70 | 0 | my %params = @p; | |||||||||||||||||||||||||||||||
| 71 | 0 | my $pagename = $leaf->pagename; | |||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if (! exists $params{pagenames}) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||||||
| 75 | 0 | return "ERROR: missing pagenames parameter"; | |||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if ($phase eq $Muster::Hooks::PHASE_SCAN) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 78 | { | ||||||||||||||||||||||||||||||||
| 79 | 0 | return ""; | |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 0 | my @list = map { $self->{metadb}->bestlink($pagename, $_) } split ' ', $params{pagenames}; | |||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | # Do a naive solution first: simply include the raw text of the other pages | ||||||||||||||||||||||||||||||||
| 85 | # into this page, IFF they are of the same filetype. | ||||||||||||||||||||||||||||||||
| 86 | # This ought to work in most cases, if I'm sticking to Markdown for the default page type. | ||||||||||||||||||||||||||||||||
| 87 | # If this is a binary file, use the "html_from" value if it exists. | ||||||||||||||||||||||||||||||||
| 88 | my $this_filetype = ($leaf->is_binary | ||||||||||||||||||||||||||||||||
| 89 | ? (defined $leaf->meta->{html_from} and $leaf->meta->{html_from} | ||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | ? $leaf->meta->{html_from} : $leaf->filetype) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
defined $leaf->meta->{"html_from"} and $leaf->meta->{"html_from"} ? $leaf->meta->{"html_from"} : $leaf->filetype
MC/DC0%0/2
defined $leaf->meta->{"html_from"} and $leaf->meta->{"html_from"} ? $leaf->meta->{"html_from"} : $leaf->filetype
defined $leaf->meta->{"html_from"}
$leaf->meta->{"html_from"} ? $leaf->meta->{"html_from"} : $leaf->filetype
Truth table0%0/3
A and B
A= defined $leaf->meta->{"html_from"}B= $leaf->meta->{"html_from"} ? $leaf->meta->{"html_from"} : $leaf->filetype
| |||||||||||||||||||||||||||||||||
| 91 | : $leaf->filetype); | ||||||||||||||||||||||||||||||||
| 92 | 0 | my @in_stuff = (); | |||||||||||||||||||||||||||||||
| 93 | 0 | foreach my $page (@list) | |||||||||||||||||||||||||||||||
| 94 | { | ||||||||||||||||||||||||||||||||
| 95 | 0 | my $info = $self->{metadb}->page_or_file_info($page); | |||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if ($info and $info->{filetype} eq $this_filetype) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$info and $info->{"filetype"} eq $this_filetype
MC/DC0%0/2
$info and $info->{"filetype"} eq $this_filetype
$info
$info->{"filetype"} eq $this_filetype
Truth table0%0/3
A and B
A= $infoB= $info->{"filetype"} eq $this_filetype
| |||||||||||||||||||||||||||||||||
| 97 | { | ||||||||||||||||||||||||||||||||
| 98 | my $new_leaf = Muster::LeafFile->new( | ||||||||||||||||||||||||||||||||
| 99 | pagename=>$info->{pagename}, | ||||||||||||||||||||||||||||||||
| 100 | pagesrcname=>$info->{pagesrcname}, | ||||||||||||||||||||||||||||||||
| 101 | parent_page=>$info->{parent_page}, | ||||||||||||||||||||||||||||||||
| 102 | filename=>$info->{filename}, | ||||||||||||||||||||||||||||||||
| 103 | filetype=>$info->{filetype}, | ||||||||||||||||||||||||||||||||
| 104 | is_binary=>$info->{is_binary}, | ||||||||||||||||||||||||||||||||
| 105 | extension=>$info->{extension}, | ||||||||||||||||||||||||||||||||
| 106 | bald_name=>$info->{bald_name}, | ||||||||||||||||||||||||||||||||
| 107 | hairy_name=>$info->{hairy_name}, | ||||||||||||||||||||||||||||||||
| 108 | title=>$info->{title}, | ||||||||||||||||||||||||||||||||
| 109 | date=>$info->{date}, | ||||||||||||||||||||||||||||||||
| 110 | 0 | meta=>$info, | |||||||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||||||
| 112 | 0 | $new_leaf = $new_leaf->reclassify(); | |||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if (!$new_leaf) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 114 | { | ||||||||||||||||||||||||||||||||
| 115 | 0 | croak "ERROR: leaf did not reclassify\n"; | |||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||
| 117 | 0 | push @in_stuff, $self->include_a_page($new_leaf); | |||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||
| 120 | 0 | my $ret = join("\n", @in_stuff); | |||||||||||||||||||||||||||||||
| 121 | } # process | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | =head2 include_a_page | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | Include the content of one page into another. | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub include_a_page { | ||||||||||||||||||||||||||||||
|
Subroutine
include_a_page: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 129 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 130 | 0 | my $new_leaf = shift; | |||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | 0 | my $content = $new_leaf->cooked; | |||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | if ($content =~ /\[\[\!includepage/) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||||||
| 135 | # the included page may have an includepage directive in it! | ||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | my $cb = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 137 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | 0 | return $self->process(%args); | |||||||||||||||||||||||||||||||
| 140 | 0 | }; | |||||||||||||||||||||||||||||||
| 141 | 0 | $new_leaf = $self->do_directives( | |||||||||||||||||||||||||||||||
| 142 | no_scan=>1, | ||||||||||||||||||||||||||||||||
| 143 | directive=>'includepage', | ||||||||||||||||||||||||||||||||
| 144 | call=>$cb, | ||||||||||||||||||||||||||||||||
| 145 | leaf=>$new_leaf, | ||||||||||||||||||||||||||||||||
| 146 | phase=>$Muster::Hooks::PHASE_BUILD, | ||||||||||||||||||||||||||||||||
| 147 | ); | ||||||||||||||||||||||||||||||||
| 148 | 0 | $content = $new_leaf->cooked; | |||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||
| 150 | 0 | return $content; | |||||||||||||||||||||||||||||||
| 151 | } # include_a_page | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 1 | 1; | |||||||||||||||||||||||||||||||