summaryrefslogtreecommitdiff
path: root/pandoc-iri
blob: 0f3fb0cc6e1c4a53f207ee554fbe66e554145887 (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::Escape;
  8. use URI::Find;
  9. pandoc_filter(
  10. \&link_normalize,
  11. );
  12. sub link_normalize {
  13. my $self = shift;
  14. return [ Link(
  15. $self->content,
  16. [ pp_uri($self->target->[0]) => '' ]
  17. )]
  18. if ( $self->name eq 'Link' );
  19. return [ Str pp_string($self->content) ]
  20. if ( $self->name eq 'Str' );
  21. return;
  22. }
  23. sub pp_uri {
  24. return URI->new(uri_unescape(shift))->canonical->as_string;
  25. }
  26. sub pp_iri {
  27. return URI->new(uri_unescape(shift))->canonical->as_iri;
  28. }
  29. sub pp_string {
  30. my $string = shift;
  31. my $finder = URI::Find->new( \&pp_iri );
  32. $finder->find(\$string);
  33. return $string;
  34. }