| 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
| |||||||||||||||||||||||||||||
| 22 | 7 | ▶ | $_->is_inline and $_->name ne 'Note' ? $_->string : \undef | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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"
MC/DC100%1/1
$id ||= "section"
$id
Truth table100%2/2
A
A= $id
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # add counter on repeated identifiers | ||||||||||||||||||||||||||||
| 46 | 12 | ▶ | if ($ids and $ids->{$id}++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ids and $ids->{$id}++
MC/DC100%2/2
$ids and $ids->{$id}++
$ids
$ids->{$id}++
Truth table100%3/3
A and B
A= $idsB= $ids->{$id}++
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$id =~ /^\p{InPandocHeaderIdentifier}+$/ and $id =~ /^\p{Letter}/
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}/
| |||||||||||||||||||||||||||||
| 68 | 1 | ▶ | if ($id =~ /^(.+)-(\d+)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 1 | $id = $1; | |||||||||||||||||||||||||||
| 70 | 1 | ▶ | $ids->{$id} = $2 unless defined $ids->{$id} and $ids->{$id} > $2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ids->{$id} and $ids->{$id} > $2
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||