-
Notifications
You must be signed in to change notification settings - Fork 3
/
SmartyLink-v0i01
283 lines (197 loc) · 7.55 KB
/
SmartyLink-v0i01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# Blosxom Plugin: SmartyLink
# Author(s): Seo Seokjin <[email protected]>
# 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.
# <nolink> TEXT BlOCK </nolink> 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) -> <a href="$url"> $url </a>
# if $url= http://where/to/imagefile.(png,gif, or jpg) -> <img src="$url">
$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;
#<nolink> tag block : no html link conversion
s/<nolink>(.*?)<\/nolink>/&StoreRaw($1)/gise;
# store A href url
s/<a(\s[^<>]+?)>(.*?)<\/a>/&StoreHref($1, $2)/gise;
# store IMG src url
s/<img(\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/>/>/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 "<a" . &StoreRaw($anchor) . ">$text</a>";
}
sub StoreImg {
my ($src) = @_;
return "<img" . &StoreRaw($src) . ">";
}
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:[email protected] is not an image
if ($useImage && &ImageAllowed($url)) {
return "<img src=\"$url\">";
}
return "<a href=\"$url\" target=\"_new\">$name</a>";
}
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:[email protected] is not an image.
Images are showed only if $UseImage is set true.
if ($UseImage && &ImageAllowed($url)) {
return "<img src=\"$url\">";
}
return "<a href=\"$url\">$name</a>";
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 <[email protected]>
=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