blib/lib/Pandoc/Filter/HeaderIdentifiers.pm

filter: Statementstmt 100% 46 / 46 Branchbran 77.7% 14 / 18 Conditioncond 72.7% 8 / 11 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 13 / 13 Podpod 80.0% 4 / 5 totaltotal 89.0% 89 / 100 SCAR 26.5
CC
14
Cov
91%
__ANON__
6.8
header_identifier
5.0
__ANON__
3.1
CRAP 14.2
« blib/lib/Pandoc/Selector.pm ↑ Summary blib/lib/Pandoc/Filter.pm »
1 package Pandoc::Filter::HeaderIdentifiers;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.34';
7  
8 1 ▶use parent 'Pandoc::Filter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Pandoc::Elements;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our @EXPORT = qw(header_identifier InPandocHeaderIdentifier);
12  
13 ## FUNCTIONS
14  
15 sub header_identifier {
16 12 ▶ my $id = shift;
Subroutine header_identifier: called CC=5 SCAR=16.1
Pod documented
17 12 my $ids = shift;
18  
19 # stringify inline elements except footnotes
20 $id = join '', @{
21 12 ▶ pandoc_query( $id, sub {
Branch: 1 branch
BranchTrueFalseTotal
if ref $id 5 7 12
22 7 ▶ $_->is_inline and $_->name ne 'Note' ? $_->string : \undef
Subroutine __ANON__: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
$_->is_inline and $_->name ne "Note" ? $_->string : \(undef) 7 0 7
$_->name ne 'Note' ? : 6 1 7
23 })
24 5 } if ref $id;
25  
26 # Convert all alphabetic characters to lowercase.
27 12 $id = lc $id;
28  
29 # these steps not strictly documented but it is how Pandoc works
30 12 $id =~ s/\p{^InPandocHeaderIdOrWs}+//g;
31 12 $id =~ s/\p{WhiteSpace}+$//;
32  
33 # Replace all spaces and newlines with hyphens.
34 12 $id =~ s/\p{WhiteSpace}+/-/g;
35  
36 # Remove all punctuation, except underscores, hyphens, periods, and whitespace.
37 12 $id =~ s/\p{^InPandocHeaderIdentifier}//g;
38  
39 # remove everything up to the first letter
40 12 $id =~ s/^[_.0-9-]+//;
41  
42 # if nothing is left, use the identifier 'section'
43 12 ▶ $id ||= 'section';
Condition100%2/2
$id ||= "section"
l!l
93
MC/DC100%1/1
$id ||= "section"
$id
Truth table100%2/2
A
A= $id
Aresult
11
00
44  
45 # add counter on repeated identifiers
46 12 ▶ if ($ids and $ids->{$id}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ids and $ids->{$id}++) 4 8 12
Condition100%3/3
$ids and $ids->{$id}++
!ll&&!rl&&r
624
MC/DC100%2/2
$ids and $ids->{$id}++
$ids $ids->{$id}++
Truth table100%3/3
A and B
A= $idsB= $ids->{$id}++
ABresult
0X0
100
111
47 4 $id .= '-' . ($ids->{$id}-1);
48 }
49  
50 12 return $id;
51 }
52  
53 ## METHODS
54  
55 sub new {
56 1 ▶ bless { }, shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
57 }
58  
59 sub apply {
60 1 ▶ my ($self, $doc) = @_;
Subroutine apply: called CC=2 SCAR=7.1
Pod documented
61  
62 1 ▶ my $ids = @_ > 2 ? $_[2] : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ > 2 ? : 1 0 1
63  
64 # collect existing identifiers
65 $doc->walk( Header => sub {
66 5 ▶ my $id = $_->id;
Subroutine __ANON__: called CC=6 SCAR=19.1
67 5 ▶ return if $id !~ /^\p{InPandocHeaderIdentifier}+$/ or $id !~ /^\p{Letter}/;
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /^\p{InPandocHeaderIdentifier}+$/ and $id =~ /^\p{Letter}/ 4 1 5
Condition66%2/3
$id =~ /^\p{InPandocHeaderIdentifier}+$/ and $id =~ /^\p{Letter}/
!ll&&!rl&&r
401
MC/DC50%1/2
$id =~ /^\p{InPandocHeaderIdentifier}+$/ and $id =~ /^\p{Letter}/
$id =~ /^\p{InPandocHeaderIdentifier}+$/ $id =~ /^\p{Letter}/
Truth table66%2/3
A and B
A= $id =~ /^\p{InPandocHeaderIdentifier}+$/B= $id =~ /^\p{Letter}/
ABresult
0X0
100
111
68 1 ▶ if ($id =~ /^(.+)-(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /^(.+)-(\d+)$/) 1 0 1
69 1 $id = $1;
70 1 ▶ $ids->{$id} = $2 unless defined $ids->{$id} and $ids->{$id} > $2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ids->{$id} and $ids->{$id} > $2 1 0 1
Condition33%1/3
defined $ids->{$id} and $ids->{$id} > $2
!ll&&!rl&&r
100
MC/DC0%0/2
defined $ids->{$id} and $ids->{$id} > $2
defined $ids->{$id} $ids->{$id} > $2
Truth table33%1/3
A and B
A= defined $ids->{$id}B= $ids->{$id} > $2
ABresult
0X0
100
111
71 }
72 1 $ids->{$id}++;
73 1 } );
74  
75 # add missing identifiers
76 $doc->walk( Header => sub {
77 5 ▶ $_[0]->id( header_identifier( $_[0]->content, $ids ) ) if $_[0]->id eq '';
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->id eq "" 4 1 5
78 1 });
79  
80 1 $doc;
81 }
82
83 ## CHARACTER PROPERTIES
84  
85 sub InPandocHeaderIdentifier {
86 2 ▶ return "+utf8::Letter\n-utf8::Uppercase_Letter\n-utf8::Titlecase_Letter\n0030 0039\n005F\n002d 002e\n";
Subroutine InPandocHeaderIdentifier: called CC=1 SCAR=0.0
Pod documented
87 }
88  
89 sub InPandocHeaderIdOrWs {
90 1 ▶ return "+utf8::Whitespace\n+utf8::Letter\n-utf8::Uppercase_Letter\n-utf8::Titlecase_Letter\n0030 0039\n005F\n002d 002e\n";
Subroutine InPandocHeaderIdOrWs: called CC=1 SCAR=0.0
Pod undocumented
91 }
92  
93 __END__
« blib/lib/Pandoc/Selector.pm ↑ Summary blib/lib/Pandoc/Filter.pm »