summaryrefslogtreecommitdiff
path: root/pandoc-filter-iri
blob: 94be2e457fcef1459f8f21343d0925c04ce88a5f (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use Pandoc::Filter;
  5. use Pandoc::Elements;
  6. use URI;
  7. use URI::Find;
  8. pandoc_filter(
  9. \&link_normalize,
  10. );
  11. sub link_normalize {
  12. my $self = shift;
  13. return [ Link(
  14. $self->content,
  15. [ pp_uri($self->target->[0]) => '' ]
  16. )]
  17. if ( $self->name eq 'Link' );
  18. return [ Str pp_string($self->content) ]
  19. if ( $self->name eq 'Str' );
  20. return;
  21. }
  22. sub pp_uri {
  23. return URI->new(shift)->canonical->as_string;
  24. }
  25. sub pp_iri {
  26. return URI->new(shift)->canonical->as_iri;
  27. }
  28. sub pp_string {
  29. my $string = shift;
  30. my $finder = URI::Find->new( \&pp_iri );
  31. $finder->find(\$string);
  32. return $string;
  33. }