Muster/Hook/HeadFoot.pm

filter: Statementstmt 20.2% 14 / 69 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 50.0% 4 / 8 Podpod 100% 2 / 2 totaltotal 16.9% 20 / 118 CC 17 SCAR 44.9
CC
17
Cov
14%
process
156.0
_find_affix_page
42.0
register
2.0
CRAP 88.8
« Muster/Hook/Table.pm ↑ Summary Muster/Hook/FieldSubst.pm »
1 package Muster::Hook::HeadFoot;
2 1 $Muster::Hook::HeadFoot::VERSION = '0.96';
3 1 ▶use Mojo::Base 'Muster::Hook';
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 =encoding utf8
10  
11 =head1 NAME
12  
13 Muster::Hook::HeadFoot - Muster header and footer hook.
14  
15 =head1 VERSION
16  
17 version 0.96
18  
19 =head1 DESCRIPTION
20  
21 L<Muster::Hook::HeadFoot> includes header and footer pages inside pages.
22  
23 =head1 METHODS
24  
25 =head2 register
26  
27 Do some intialization.
28 And register.
29  
30 =cut
31 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
Pod documented
32 0 my $self = shift;
33 0 my $hookmaster = shift;
34 0 my $conf = shift;
35  
36 0 $self->{metadb} = $hookmaster->{metadb};
37  
38 0 ▶ $hookmaster->add_hook('headfoot' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 0 my %args = @_;
40  
41 0 return $self->process(%args);
42 },
43 0 );
44 0 return $self;
45 } # register
46  
47 =head2 process
48  
49 Process header/footer inclusion.
50  
51 =cut
52 0 ▶sub process {
Subroutine process: not called CC=12 SCAR=50.5
Pod documented
53 0 my $self = shift;
54 0 my %args = @_;
55  
56 0 my $leaf = $args{leaf};
57 0 my $phase = $args{phase};
58  
59 # Don't do header/footer inclusion in the scanning phase;
60 # it would be too confusing.
61 0 ▶ if ($phase eq $Muster::Hooks::PHASE_SCAN)
Branch: 1 branch
BranchTrueFalseTotal
if ($phase eq $Muster::Hooks::PHASE_SCAN) 0 0 0
62 {
63 0 return $leaf;
64 }
65  
66 0 my $pagename = $leaf->pagename;
67  
68 # Do a naive solution first: simply include the raw text of the other pages
69 # into this page, IFF they are of the same filetype.
70 # This ought to work in most cases, if I'm sticking to Markdown for the default page type.
71 # If this is a binary file, use the "html_from" value if it exists.
72 0 my %affix = ();
73 0 $affix{header}->{pagename} = $self->_find_affix_page(current_page=>$pagename,
74 affix_page=>'_Header');
75 0 $affix{footer}->{pagename} = $self->_find_affix_page(current_page=>$pagename,
76 affix_page=>'_Footer');
77  
78 my $this_filetype = ($leaf->is_binary
79 ? (defined $leaf->meta->{html_from} and $leaf->meta->{html_from}
80 0 ▶ ? $leaf->meta->{html_from} : $leaf->filetype)
Branch: 2 branches
BranchTrueFalseTotal
$leaf->is_binary ? : 0 0 0
$leaf->meta->{'html_from'} ? : 0 0 0
Condition0%0/3
defined $leaf->meta->{"html_from"} and $leaf->meta->{"html_from"} ? $leaf->meta->{"html_from"} : $leaf->filetype
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
81 : $leaf->filetype);
82 0 foreach my $type (keys %affix)
83 {
84 0 my $page = $affix{$type}->{pagename};
85 0 ▶ if ($page)
Branch: 1 branch
BranchTrueFalseTotal
if ($page) 0 0 0
86 {
87 0 my $info = $self->{metadb}->page_or_file_info($page);
88 0 ▶ if ($info and $info->{filetype} eq $this_filetype)
Branch: 1 branch
BranchTrueFalseTotal
if ($info and $info->{"filetype"} eq $this_filetype) 0 0 0
Condition0%0/3
$info and $info->{"filetype"} eq $this_filetype
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
89 {
90 my $new_leaf = Muster::LeafFile->new(
91 pagename=>$info->{pagename},
92 pagesrcname=>$info->{pagesrcname},
93 parent_page=>$info->{parent_page},
94 filename=>$info->{filename},
95 filetype=>$info->{filetype},
96 is_binary=>$info->{is_binary},
97 extension=>$info->{extension},
98 bald_name=>$info->{bald_name},
99 hairy_name=>$info->{hairy_name},
100 title=>$info->{title},
101 date=>$info->{date},
102 0 meta=>$info,
103 );
104 0 $new_leaf = $new_leaf->reclassify();
105 0 ▶ if (!$new_leaf)
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_leaf) 0 0 0
106 {
107 0 croak "ERROR: leaf did not reclassify\n";
108 }
109 0 $affix{$type}->{contents} = $new_leaf->raw;
110 }
111 }
112 }
113  
114 # Now include the header and footer if there is one
115 0 ▶ if ($affix{header}->{contents})
Branch: 1 branch
BranchTrueFalseTotal
if ($affix{"header"}{"contents"}) 0 0 0
116 {
117 0 $leaf->{cooked} = $affix{header}->{contents} . $leaf->{cooked};
118 }
119 0 ▶ if ($affix{footer}->{contents})
Branch: 1 branch
BranchTrueFalseTotal
if ($affix{"footer"}{"contents"}) 0 0 0
120 {
121 0 $leaf->{cooked} = $leaf->{cooked} . $affix{footer}->{contents};
122 }
123  
124 0 return $leaf;
125 } # process
126  
127 =head2 _find_affix_page
128  
129 Find the desired Header/Footer page which
130 applies to the given page.
131 First search in the same level as the page, then in its
132 parent level, and so on.
133  
134 my $spage = $self->_find_affix_page(current_page=>$page,affix_page=>'_Header');
135  
136 =cut
137 0 ▶sub _find_affix_page {
Subroutine _find_affix_page: not called CC=6 SCAR=37.4
138 0 my $self = shift;
139 0 my %args = @_;
140  
141 0 my $current_page = $args{current_page};
142 0 my $affix_page = $args{affix_page};
143 0 my $cp_info = $self->{metadb}->page_or_file_info($current_page);
144  
145 # find a "local" affix-page first, which has priority
146 # This will have an extra '_' at the front of it.
147 # This can only be in the same folder as the current page.
148 0 my $local_sp = $cp_info->{parent_page} . '/_' . $affix_page;
149 0 ▶ if ($self->{metadb}->page_exists($local_sp))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"metadb"}->page_exists($local_sp)) 0 0 0
150 {
151 0 return $local_sp;
152 }
153  
154 # Next priority is a grand-local page.
155 # This is in the directory *above* this page (grandparent)
156 # and has two extra '_' at the front of it.
157 0 my $grandlocal_sp = $cp_info->{grandparent_page} . '/__' . $affix_page;
158 0 ▶ if ($self->{metadb}->page_exists($grandlocal_sp))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"metadb"}->page_exists($grandlocal_sp)) 0 0 0
159 {
160 0 return $grandlocal_sp;
161 }
162  
163 0 my @bits = split('/', $current_page);
164 0 my $found_page = '';
165 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
scalar @bits and not $found_page and do { do { my $cwd = join("/", @bits); my $q = "SELECT page FROM pagefiles WHERE bald_name = '${affix_page}' AND parent_page IN (SELECT parent_page FROM pagefiles WHERE page = '${cwd}');"; my $pages = $self->{"metadb"}->query($q); if ($pages) { $found_page = $pages->[0]; } ; pop @bits } } 0 0 0
Condition0%0/3
scalar @bits and not $found_page
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @bits and not $found_page
scalar @bits not $found_page
Truth table0%0/3
A and not(B)
A= scalar @bitsB= not $found_page
ABresult
0X0
100
111
166 0 my $cwd = join('/', @bits);
167 0 my $q = "SELECT page FROM pagefiles WHERE bald_name = '$affix_page' AND parent_page IN (SELECT parent_page FROM pagefiles WHERE page = '$cwd');";
168 0 my $pages = $self->{metadb}->query($q);
169 0 ▶ if ($pages)
Branch: 1 branch
BranchTrueFalseTotal
if ($pages) 0 0 0
170 {
171 0 $found_page = $pages->[0];
172 }
173 0 pop @bits;
174 } while (scalar @bits and !$found_page);
175  
176 0 return $found_page;
177 } # _find_affix_page
178  
179 1 1;
« Muster/Hook/Table.pm ↑ Summary Muster/Hook/FieldSubst.pm »