1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
package Pandoc::Filter::HeaderIdentifiers;
use strict;
use warnings;
use 5.010;
our $VERSION = '0.34';
use parent 'Pandoc::Filter';
use Pandoc::Elements;
our @EXPORT = qw(header_identifier InPandocHeaderIdentifier);
## FUNCTIONS
sub header_identifier {
my $id = shift;
my $ids = shift;
# stringify inline elements except footnotes
$id = join '', @{
pandoc_query( $id, sub {
$_->is_inline and $_->name ne 'Note' ? $_->string : \undef
})
} if ref $id;
# Convert all alphabetic characters to lowercase.
$id = lc $id;
# these steps not strictly documented but it is how Pandoc works
$id =~ s/\p{^InPandocHeaderIdOrWs}+//g;
$id =~ s/\p{WhiteSpace}+$//;
# Replace all spaces and newlines with hyphens.
$id =~ s/\p{WhiteSpace}+/-/g;
# Remove all punctuation, except underscores, hyphens, periods, and whitespace.
$id =~ s/\p{^InPandocHeaderIdentifier}//g;
# remove everything up to the first letter
$id =~ s/^[_.0-9-]+//;
# if nothing is left, use the identifier 'section'
$id ||= 'section';
# add counter on repeated identifiers
if ($ids and $ids->{$id}++) {
$id .= '-' . ($ids->{$id}-1);
}
return $id;
}
## METHODS
sub new {
bless { }, shift;
}
sub apply {
my ($self, $doc) = @_;
my $ids = @_ > 2 ? $_[2] : {};
# collect existing identifiers
$doc->walk( Header => sub {
my $id = $_->id;
return if $id !~ /^\p{InPandocHeaderIdentifier}+$/ or $id !~ /^\p{Letter}/;
if ($id =~ /^(.+)-(\d+)$/) {
$id = $1;
$ids->{$id} = $2 unless defined $ids->{$id} and $ids->{$id} > $2;
}
$ids->{$id}++;
} );
# add missing identifiers
$doc->walk( Header => sub {
$_[0]->id( header_identifier( $_[0]->content, $ids ) ) if $_[0]->id eq '';
});
$doc;
}
## CHARACTER PROPERTIES
sub InPandocHeaderIdentifier {
return "+utf8::Letter\n-utf8::Uppercase_Letter\n-utf8::Titlecase_Letter\n0030 0039\n005F\n002d 002e\n";
}
sub InPandocHeaderIdOrWs {
return "+utf8::Whitespace\n+utf8::Letter\n-utf8::Uppercase_Letter\n-utf8::Titlecase_Letter\n0030 0039\n005F\n002d 002e\n";
}
__END__
=head1 NAME
Pandoc::Filter::HeaderIdentifiers - Add identifiers to headers
=head1 SYNOPSIS
my $id = header_identifier( $header->content ); # calculate identifier
Pandoc::Filter::HeaderIdentifiers->new->apply($doc); # add all identifiers
=head1 DESCRIPTION
This L<Pandoc::Filter> adds identifier attributes (L<id|Pandoc::Elements/id>)
to all L<Headers|Pandoc::Elements/Header> elements. It uses the same algorithm
as internally used by pandoc. The module also exports function
C<header_identifier> to calculate an identifier from a list of elements.
=head1 FUNCTIONS
=head2 header_identifier( $content [, $ids ] )
Returns an identifier for a given list of L<inlines|Pandoc::Elements/INLINE
ELEMENTS> or string (C<$content>). Optionally takes into account and updates
existing ids, given as hash reference mapping identifier to usage count
(C<$ids>).
=head1 METHODS
=head2 apply( $element [, $ids ] )
Add identifiers to all L<Header|Pandoc::Elements/Header> elements found at a
given element (typically a L<Document|Pandoc::Elements/Document>. A hash
reference of existing identifier counts (or an empty hash to get the new
counts) can be passed in addition.
=head1 CHARACTER PROPERTIES
=head2 InPandocHeaderIdentifier
Matches all Unicode lowercase letters, digits 0 to 9, underscore, hyphen, and
period. In the unlikely event that you want to check whether a given string
could have been generated as header identifier, use this:
$id =~ /^\p{InPandocHeaderIdentifier}+$/ and $id =~ /^\p{Letter}/
=head1 SEE ALSO
L<http://pandoc.org/MANUAL.html#header-identifiers>
=cut
|