summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2014-12-25 13:59:32 +0100
committerJonas Smedegaard <dr@jones.dk>2014-12-25 13:59:32 +0100
commit2ca1aa70f5b49aa3eaee998a4d562c5bc37066bd (patch)
treed8b645898f07c67743697e5e6f821e592a6159c4
parenta8d873cea1672a2341d2f57d4e0057056a5fc983 (diff)
Add IRI filter to beautify (i.e. canonicalize and internationalize) links (including uglifying workaround to modest URL parsing).
-rw-r--r--Makefile2
-rwxr-xr-xpandoc-filter-iri41
2 files changed, 42 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 07bd8de..a8f9131 100644
--- a/Makefile
+++ b/Makefile
@@ -10,7 +10,7 @@ re_urlencode = s|\bhttp[^\s\"]+?\K(--+)|uri_escape($$1, "-")|eg
templates = template.tex header.tex before.tex after.tex
-filters = ./pandoc-filter-sections ./pandoc-filter-bib
+filters = ./pandoc-filter-sections ./pandoc-filter-bib ./pandoc-filter-iri
export PANDOC_CITEPROC_FILE = $(stem).bib
#PANDOC_CITEPROC_FILE = $(stem).bib
diff --git a/pandoc-filter-iri b/pandoc-filter-iri
new file mode 100755
index 0000000..94be2e4
--- /dev/null
+++ b/pandoc-filter-iri
@@ -0,0 +1,41 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+
+use Pandoc::Filter;
+use Pandoc::Elements;
+
+use URI;
+use URI::Find;
+
+pandoc_filter(
+ \&link_normalize,
+);
+
+sub link_normalize {
+ my $self = shift;
+ return [ Link(
+ $self->content,
+ [ pp_uri($self->target->[0]) => '' ]
+ )]
+ if ( $self->name eq 'Link' );
+ return [ Str pp_string($self->content) ]
+ if ( $self->name eq 'Str' );
+ return;
+}
+
+sub pp_uri {
+ return URI->new(shift)->canonical->as_string;
+}
+
+sub pp_iri {
+ return URI->new(shift)->canonical->as_iri;
+}
+
+sub pp_string {
+ my $string = shift;
+ my $finder = URI::Find->new( \&pp_iri );
+ $finder->find(\$string);
+ return $string;
+}