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
|
##
## wml::imp::generic - Generic Import Of External Resources
## Copyright (c) 1997-2001 Ralf S. Engelschall, All Rights Reserved.
##
# The <preserve>/<restore> tags with multiple arguments require WML 2.0.3
#use wml::mod::version
<require 2.0.3 />
#use wml::std::tags
#use wml::fmt::verbatim
#use wml::fmt::text
#use wml::fmt::pod
#use wml::fmt::sdf
<protect pass=2>
<:
#
# determine a way to retrieve the data
#
sub existsprg {
my ($prg) = @_;
my ($found, $p);
$found = 0;
foreach $p (split(/:/, $ENV{'PATH'})) {
$found = 1 if (-x "$p/$prg");
}
return $found;
}
eval 'use LWP::Simple;';
if (not $@) {
eval 'sub remote_fetch { my($url) = @_; return LWP::Simple::get($url); }'
}
elsif (&existsprg("lynx")) {
eval 'sub remote_fetch { my($url) = @_; return `lynx -source $url`; }'
}
elsif (&existsprg("wget")) {
eval 'sub remote_fetch { my($url) = @_; return `cd $(WML_TMPDIR) && mkdir wget.$$ && cd wget.$$ && wget -q -o/dev/null $url && cat * && cd $(WML_TMPDIR) && rm -f wget.$$`; }'
}
elsif (&existsprg("fetch")) {
eval 'sub remote_fetch { my($url) = @_; return `fetch $url`; }'
}
else {
eval 'sub remote_fetch { my($url) = @_;
return "Failed to retrieve $url" .
"None of {LWP::Simple::get,Lynx,Wget,Fetch} were found"; }'
}
sub local_fetch {
my ($file) = @_;
my ($buf);
local (*FP);
open(FP, "<$file") || die;
local ($/) = undef;
$buf = <FP>;
close(FP);
return $buf;
}
:>
</protect>
<define-tag import>
<preserve src format eperlfilter />
<set-var %attributes />
<perl>
{
my $format = '<get-var format />';
my $src = '<get-var src />';
my $eperlfilter = '<get-var eperlfilter />';
if ($format eq '') {
$format = 'verbatim';
$format = 'pod' if ($src =~ m|\.pod$|);
$format = 'sdf' if ($src =~ m|\.sdf$|);
$format = 'text' if ($src =~ m|\.txt$|);
}
my $buf;
if ($src =~ m|^http://| or $src =~ m|^ftp://|) {
$buf = &remote_fetch($src);
}
else {
$buf = &local_fetch($src);
}
my $tmpfile = "$(WML_TMPDIR)/wml.import.$$.tmp";
local (*TMP);
open(TMP, ">$tmpfile");
print TMP $buf;
close(TMP);
if ($format eq 'verbatim') {
$buf = &wml_fmt_verbatim({ FILE => $tmpfile });
}
elsif ($format eq 'pod') {
$buf = &wml_fmt_pod({ FILE => $tmpfile,
KEEPINDEX => 1,
EPERLFILTER => $eperlfilter });
}
elsif ($format eq 'sdf') {
$buf = &wml_fmt_pod({ FILE => $tmpfile });
}
elsif ($format eq 'text') {
$buf = &wml_fmt_text({ FILE => $tmpfile });
}
unlink($tmpfile);
<perl:print: $buf />
}
</perl>
<restore src format eperlfilter />
</define-tag>
##EOF##
__END__
=head1 NAME
wml::imp::generic - Generic Import Of External Resources
=head1 SYNOPSIS
#use wml::imp::generic
<import src="url" [format="..."]>
=head1 DESCRIPTION
This tag imports an external resource via URL or filename. The
C<E<lt>importE<gt>> tag gets replaced by the contents of the external resource
formatted via the C<verbatim>, C<pod>, C<sdf> or C<text> formatting filters.
Remote retrival only works if at least one of the following programs can be
found:
- Perl function: LWP::Simple::get()
- Program: lynx
- Program: wget
- Program: fetch
=head1 AUTHOR
Ralf S. Engelschall
rse@engelschall.com
www.engelschall.com
=head1 REQUIRES
Internal: P1, P2, P3
External: LWP::Simple::get (P5M) | lynx (PATH) | wget (PATH) | fetch (PATH)
=head1 SEE ALSO
LWP::Simple(3), lynx(1), wget(1), fetch(1)
=cut
|