# ResolveURLs.pl # # by Alexei Kosut # Last modified February 3, 2003 (1.0.2) # # Add global filter tag resolve_urls to expand relative URLs # in href and src attributes of HTML tags. They are always expanded # directly relative to the site URL of the weblog. use strict; use MT::Template::Context; use URI; sub resolve_uri($$) { my ($uri, $siteurl) = @_; return $uri if ($uri =~ /^\#/); # Do not resolve URI fragments return URI->new_abs($uri, $siteurl); } MT::Template::Context->add_global_filter(resolve_urls => sub { my ($s, $arg, $ctx) = @_; my $blog = $ctx->stash('blog') or return $ctx->error("Tag called without a blog in context"); my $siteurl = $blog->site_url; $siteurl .= "/" if (substr($siteurl, -1) ne "/"); # Find each URL and make it absolute $s =~ s{(<[^>]*\s(?:href|src)=)(?:\"([^\">]+)\"|'([^\'>]+)'|([a-zA-Z0-9._-]+))(/?>|\s)} {$1 . '"' . resolve_uri($2 || $3 || $4, $siteurl) . '"' . $5}gei; return $s; });