# Blosxom Plugin: SmartyLink # Author(s): Seo Seokjin # version 0.01 # Short Description: Allows http links/images, and wiki url pattern # links in Blosxom pages and converts text urls to html. # these will be made images shown or not. # TEXT BlOCK nolink tag block doesn't convert text url links to html package SmartyLink; %SaveUrl = (); %SaveNumUrl = (); $SaveUrlIndex = 0; $SaveNumUrlIndex = 0; @ImageSites = qw(); # Url prefixes of good image sites: ()=all $UseImage = 1; # image url pattern conversion is allowed # examples of url type conversion: # if $url= http://where/to/textfile.(htm or html) -> $url # if $url= http://where/to/imagefile.(png,gif, or jpg) -> $FS = "\x1e\xff\xfe\x1e"; # An unlikely sequence for any charset $AnyLetter = "[-,.()' _0-9A-Za-z\xc0-\xff]"; $QDelim = '(?:"")?'; # Optional quote delimiter (not in output) $FreeLinkPattern = "($AnyLetter+)$QDelim"; # Url-style links are delimited by one of: # 1. Whitespace (kept in output) # 2. Left or right angle-bracket (< or >) (kept in output) # 3. Right square-bracket (]) (kept in output) # 4. A single double-quote (") (kept in output) # 5. A double double-quote ("") (removed from output) $UrlProtocols = "http|https|ftp|afs|news|nntp|mid|cid|mailto|wais|" . "prospero|telnet|gopher"; $UrlPattern = "((?:(?:$UrlProtocols):[^\\]\\s\"<>]+)$QDelim)"; $ImageExtensions = "(gif|jpg|png|bmp|jpeg)"; ## following patterns will be supported (currently not yet) # $RFCPattern = "RFC\\s?(\\d+)"; # $ISBNPattern = "ISBN:?([0-9- xX]{10,})"; $AnchoredLinkPattern = $LinkPattern . '#([0-9A-Za-z\xc0-\xff]+)' . $QDelim; sub start{ return 1; } #Story.. search the story for links and images sub story { my ($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_; %SaveUrl = (); %SaveNumUrl = (); $SaveUrlIndex = 0; $SaveNumUrlIndex = 0; local $_ = $$body_ref; # tag block : no html link conversion s/(.*?)<\/nolink>/&StoreRaw($1)/gise; # store A href url s/]+?)>(.*?)<\/a>/&StoreHref($1, $2)/gise; # store IMG src url s/]+?)>/&StoreImg($1)/gise; # transform url pattern to html link tag s/\b$UrlPattern/&AutoUrl($1, $UseImage)/geo; $$body_ref = &RestoreSavedText($_); # restore stored a, img tag url } sub StoreRaw { my ($html) = @_; $SaveUrl{$SaveUrlIndex} = $html; return $FS . $SaveUrlIndex++ . $FS; } sub RestoreSavedText { my ($text) = @_; 1 while $text =~ s/$FS(\d+)$FS/$SaveUrl{$1}/ge; # Restore saved text return $text; } sub QuoteHtml { my ($html) = @_; $html =~ s/&/&/g; $html =~ s//>/g; $html =~ s/&([#a-zA-Z0-9]+);/&$1;/g; # Allow character references return $html; } sub AutoUrl { my ($name, $useImage) = @_; my ($link, $extra); ($link, $extra) = &UrlLink($name, $useImage); return $link . $extra; } sub StoreRaw { my ($html) = @_; $SaveUrl{$SaveUrlIndex} = $html; return $FS . $SaveUrlIndex++ . $FS; } sub StoreHref { my ($anchor, $text) = @_; return "$text"; } sub StoreImg { my ($src) = @_; return ""; } sub UrlLink { my ($rawname, $useImage) = @_; my ($name, $punct); ($name, $punct) = &SplitUrlPunct($rawname); return (&UrlLinkOrImage($name, $name, $useImage), $punct); } sub SplitUrlPunct { my ($url) = @_; my ($punct); if ($url =~ s/\"\"$//) { return ($url, ""); # Delete double-quote delimiters here } $punct = ""; ($punct) = ($url =~ /([^a-zA-Z0-9\/\xc0-\xff]+)$/); $url =~ s/([^a-zA-Z0-9\/\xc0-\xff]+)$//; return ($url, $punct); } sub StripUrlPunct { my ($url) = @_; my ($junk); ($url, $junk) = &SplitUrlPunct($url); return $url; } sub UrlLinkOrImage { my ($url, $name, $useImage) = @_; # Restricted image URLs so that mailto:foo@bar.gif is not an image if ($useImage && &ImageAllowed($url)) { return ""; } return "$name"; } sub ImageAllowed { my ($url) = @_; my ($site, $imagePrefixes); $imagePrefixes = 'http:|https:|ftp:'; #$imagePrefixes .= '|file:' if (!$LimitFileUrl); return 0 unless ($url =~ /^($imagePrefixes).+\.$ImageExtensions$/); return 0 if ($url =~ /"/); # No HTML-breaking quotes allowed return 1 if (@ImageSites < 1); # Most common case: () means all allowed return 0 if ($ImageSites[0] eq 'none'); # Special case: none allowed foreach $site (@ImageSites) { return 1 if ($site eq substr($url, 0, length($site))); # Match prefix } return 0; } 1 __END__ =head1 NAME Blosxom Plug-in: SmartyLink =head1 SYNOPSIS This is a plugin for Blosxom that generates html url links for URL pattern. The plugin converts the following url pattern($UrlPattern) to html link : $UrlProtocols = "http|https|ftp|afs|news|nntp|mid|cid|mailto|wais|" . "prospero|telnet|gopher"; $UrlPattern = "((?:(?:$UrlProtocols):[^\\]\\s\"<>]+)$QDelim)"; $ImageExtensions = "(gif|jpg|png|bmp|jpeg)"; if story's body matches url pattern($UrlPattern), the matching parts are transformed into html url(image or file) links. $$body_ref =~ s/\b$UrlPattern/&AutoUrl($1, $UseImage)/geo; Restricted image URLs so that mailto:foo@bar.gif is not an image. Images are showed only if $UseImage is set true. if ($UseImage && &ImageAllowed($url)) { return ""; } return "$name"; The plugin source code is an excerpt from UseMod Wiki LinkPattern. The plugin also will add other wiki url patterns in the future. =head1 VERSION v0.01 =head1 AUTHOR Seo Seokjin =head1 ALSO SEE SmartyLink: http://usemodj.com/Projects/Weblog/Blosxom/SmartyLink Blosxom: http://www.blosxom.com/ =head1 BUGS If you find a bug in this plugin, please goto the plugins website for information on contacting me. http://usemodj.com/Projects/Weblog/Blosxom/SmartyLink =head1 LICENSE =head2 SmartyLink: SmartyLink Copyright 2004, Seo Seokjin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. =head2 BLOSXOM For licensing information on Blosxom please goto http://www.blosxom.com/license.html