post_page — custom subroutine to run before image paths substitution on a page
This pragma defines a Sub
or GlobalSub
to run
before image paths substitution on a page. A
reference to the page contents
is passed to the subroutine.
Example: Treat URLs relative to current directory
It is possible to have URL links treated
as being relative to the current directory.
The following is needed in catalog.cfg
:
Pragma post_page=relative_urls ### Take hrefs like <A HREF="about.html"> and make relative to current ### directory Sub <<EOR sub relative_urls { my $page = shift; my @dirs = split "/", $Tag->var('MV_PAGE', 1); pop @dirs; my $basedir = join "/", @dirs; $basedir ||= ''; $basedir .= '/' if $basedir; my $sub = sub { my ($entire, $pre, $url) = @_; return $entire if $url =~ /^\w+:/; my($page, $form) = split /\?/, $url, 2; my $u = $Tag->area({ href => "$basedir$page", form => $form, }); return qq{$pre"$u"}; }; $$page =~ s{( ( <a \s+ (?:[^>]+?\s+)? href \s*=\s* ) (["']) ([^\s"'>]+) \3 )} { $sub->($1,$2,$4) }gsiex; return; } EOR
The old documentation system improperly describes the phase at which the post_page
pragma takes effect (it states it happens after Variable
processing and before tags are interpolated).
Interchange 5.9.0 (1/1 contexts shown):
Source: lib/Vend/Interpolate.pm
Line 538 (context shows lines 528-542 in substitute_image():530)
} sub substitute_image { my ($text) = @_; ## Allow no substitution of downloads return if $::Pragma->{download}; ## If post_page routine processor returns true, return. Otherwise, ## continue image rewrite if($::Pragma->{post_page}) { Vend::Dispatch::run_macro($::Pragma->{post_page}, $text) and return; }