Skip to content

Commit

Permalink
Item15055:Publish jquery plugins as internal resources
Browse files Browse the repository at this point in the history
Item15056:  Allow publication of dynamic html
Item15161: Support simple url() function references in .css.
Commented out the $this references noted in Item14759 and Item14760. But the debub messages are now gone too. This needs to be refactored to fix.
  • Loading branch information
BramVan-Oosterhout committed Jul 28, 2022
1 parent f38ce60 commit 723d3e6
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 74 deletions.
5 changes: 3 additions & 2 deletions data/System/PublishPlugin.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%META:TOPICINFO{author="ProjectContributor" comment="" date="1638778587" format="1.1" reprev="1" version="1"}%
%META:TOPICINFO{author="BramVanOosterhout" comment="" date="1658981746" format="1.1" reprev="5" version="5"}%
---+!! <nop>%TOPIC%
*Generates a static view of a web, as HTML files on disc, or as a =PDF=, or as a =zip= or =tgz= archive file, or by uploading directly to an FTP server.*

Expand Down Expand Up @@ -554,6 +554,7 @@ apply.
---++ Change History

%TABLE{columnwidths="10em" tablewidth="100%"}%
| 3.6 (28 Jul 2022) | Foswikitask:Item15055: Publish jquery plugins as internal resource. Foswikitask:Item15056: Allow publication of dynamic html. Foswikitask:Item15161: Support simple url() function references in =.css=. |
| 3.5 (07 Dec 2021) | Foswikitask:Item15050: Bug fix. Foswikitask:Item15051: Improve saved publishing history. Foswikitask:Item15052: Allow =cover= on =publishskin=. Foswikitask:Item15053: Improve publishing resources. |
| 3.4 (30 Jan 2018) | Foswikitask:Item14611: flatdir generator, Foswikitask:Item14612: add support for handling external resources, Foswikitask:Item14613: Foswikitask:Item14613: improve format UI, Foswikitask:Item14513: add HTML to basic_publish skin Foswikitask:Item14614: support parameters passed to resource URLs |
| 3.3 (26 Jan 2018) | Foswikitask:Item14540: correct internal resource resolution Foswikitask:Item14610: re-implement defaultpage |
Expand Down Expand Up @@ -601,7 +602,7 @@ GNU General Public License for more details, published at
http://www.gnu.org/copyleft/gpl.html

%META:FORM{name="PackageForm"}%
%META:FIELD{name="Author" title="Author" value=""}%
%META:FIELD{name="Author" title="Author" value="ProjectContributor"}%
%META:FIELD{name="Version" title="Version" value="%25$VERSION%25"}%
%META:FIELD{name="Release" title="Release" value="%25$RELEASE%25"}%
%META:FIELD{name="Description" title="Description" value="%25$SHORTDESCRIPTION%25"}%
Expand Down
4 changes: 2 additions & 2 deletions lib/Foswiki/Plugins/PublishPlugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use Foswiki::Func ();
use Error qw( :try );
use Assert;

our $VERSION = '3.5';
our $RELEASE = '07 Dec 2021';
our $VERSION = '3.6';
our $RELEASE = '28 July 2022';
our $SHORTDESCRIPTION =
'Generate static output (HTML, PDF) optionally upload (FTP) the output to a publishing site.';

Expand Down
194 changes: 124 additions & 70 deletions lib/Foswiki/Plugins/PublishPlugin/Publisher.pm
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,13 @@ sub _loadParams {
$k = $spec->{renamed};
$spec = $schema{$k};
$renamed = 1;

#print STDERR "Rename $ok to $k\n";
}

next if defined $opt{$k};

ASSERT( defined $spec->{validator}, $k ) if DEBUG;

if ( !defined $v && defined $spec->{default} && !$renamed ) {

#print STDERR "Default $ok to '$spec->{default}'\n";
$v = $spec->{default};
}
elsif ( defined $v ) {
Expand Down Expand Up @@ -603,8 +599,9 @@ TEXT
$this->{archive}->getReady();

# Force static context for all published topics
# Chaned to pdf only. Item15056
Foswiki::Func::getContext()->{static} = 1
if $this->{opt}->{format} eq 'pdf';
if $this->{opt}->{format} eq 'pdf';

my $safe = $Foswiki::cfg{ScriptUrlPaths};
undef $Foswiki::cfg{ScriptUrlPaths};
Expand Down Expand Up @@ -994,27 +991,42 @@ sub _rewriteTag {
|| $new =~ /^#/ )
{

#$this->logDebug("Rewrite $new (rel to ",
# $this->{archive}->getTopicPath( $web, $topic ).')');
$this->logDebug( "Rewrite $new (rel to ",
$this->{archive}->getTopicPath( $web, $topic ) . ')' );
$new =
File::Spec->abs2rel( "/$new",
'/' . $this->{archive}->getTopicPath( $web, $topic ) . "/.." );

#$this->logDebug("as $new");
$this->logDebug("as $new");
}

#$this->logDebug("$attrs{$key} = $new");
$this->logDebug("$attrs{$key} = $new");
$attrs{$key} = $new;

return
"<$type " . join( ' ', map { "$_=\"$attrs{$_}\"" } keys %attrs ) . '>';
}

sub _rewriteStyleURL {
my ( $url, $referrer ) = @_;

# $url is the <url> used in the style url( <...>url> ) reference
# $ reference is the file path from where the reference was found

$referrer =~ m!\A([^/]+)/([^/]+)/(.*/)?[^/]+\Z!;
$url = Foswiki::Func::getPubUrlPath( $1, $2, "$3$url" );
return $url;
}

# Rewrite a URL - be it internal or external. Internal URLs that point to
# anything in pub, or to scripts, are always rewritten.
sub _processURL {
my ( $this, $url, $referrer ) = @_;

if ( $referrer =~ m/\Astyle:(.+)/ ) {
$url = _rewriteStyleURL( $url, $1 );
}

$url = URI->new($url);

# $url->scheme
Expand Down Expand Up @@ -1057,7 +1069,7 @@ sub _processURL {
sub _match {
my ( $abs, $url, $match ) = @_;

$this->logDebug("Test $url against $match");
#$this->logDebug("Test $url against $match");

# Some older parsers used to allow the scheme name to be present
# in the relative URL if it was the same as the base URL
Expand All @@ -1066,53 +1078,59 @@ sub _processURL {
if ( $match->can('scheme') && $match->scheme ) {
if ( $url->can('scheme') ) {
unless ( _matchPart( $url->scheme, $match->scheme ) ) {
$this->logDebug( "- scheme mismatch "
. $url->scheme . " and "
. $match->scheme );

#$this->logDebug( "- scheme mismatch "
# . $url->scheme . " and "
# . $match->scheme );
return undef;
}
}
else {
$this->logDebug("- no scheme on url");
#$this->logDebug("- no scheme on url");
return undef;
}
}
elsif ( $url->can('scheme') && $url->scheme ) {
$this->logDebug("- no scheme on match");

#$this->logDebug("- no scheme on match");
return undef;
}

if ( $match->can('host') && $match->host ) {
if ( $url->can('host') ) {
unless ( _matchPart( $url->host, $match->host ) ) {
$this->logDebug("- host mismatch");

#$this->logDebug("- host mismatch");
return undef;
}
}
else {
$this->logDebug("- no host on url");
#$this->logDebug("- no host on url");
return undef;
}
}
elsif ( $url->can('host') && $url->host ) {
$this->logDebug("- no host on match");

#$this->logDebug("- no host on match");
return undef;
}

if ( $match->can('port') && length( $match->port ) ) {
if ( $url->can('port') ) {
unless ( _matchPart( $url->port, $match->port ) ) {
$this->logDebug("- port mismatch");

#$this->logDebug("- port mismatch");
return undef;
}
}
else {
$this->logDebug("- no port on url");
#$this->logDebug("- no port on url");
return undef;
}
}
elsif ( $url->can('port') && length( $url->port ) ) {
$this->logDebug("- no port on match");

#$this->logDebug("- no port on match");
return undef;
}

Expand All @@ -1125,16 +1143,17 @@ sub _processURL {
|| $mpath[0] eq 'SCRIPT' )
)
{
$this->logDebug("- trim $upath[0] match $mpath[0]");
#$this->logDebug("- trim $upath[0] match $mpath[0]");
shift(@mpath);
shift(@upath);
}
if ( $mpath[0] eq 'WEB' ) {
$this->logDebug( "- matched " . join( '/', @upath ) );

#$this->logDebug( "- matched " . join( '/', @upath ) );
return \@upath;
}
else {
$this->logDebug("- no match at $mpath[0]");
#$this->logDebug("- no match at $mpath[0]");
return undef;
}
}
Expand Down Expand Up @@ -1206,7 +1225,7 @@ sub _processURL {
my $new = $url;

# Is it a pub resource?
#Item15055:
#Item15055:
# Abandoning the retrieval of pub resources with query as external
# readAttachment supports versioned attachments.
# other parameters must be handled by the pub resource
Expand All @@ -1215,20 +1234,22 @@ sub _processURL {
if ( $type eq 'pub' ) {
if ( $url->query() ) {
my %query = $url->query_form();

$attachment = $query{filename} ? delete $query{filename} : pop(@$upath);

$attachment =
$query{filename} ? delete $query{filename} : pop(@$upath);
$rev = $query{rev} ? delete $query{rev} : undef();
$url->query_form( \%query );
}
else{
else {
$attachment = pop(@$upath) if scalar(@$upath);
$rev = undef();
}
$topic = pop(@$upath) if scalar(@$upath);
$topic = pop(@$upath) if scalar(@$upath);
$web = join( '/', @$upath );
$new = $this->_processInternalResource( $web, $topic, $attachment, $rev );
$new = $new.'?'.$url->query() if $url->query();

$new =
$this->_processInternalResource( $web, $topic, $attachment, $rev );
$new = $new . '?' . $url->query() if $url->query();

}
elsif ( $type eq 'script' ) {

Expand Down Expand Up @@ -1287,52 +1308,85 @@ sub _processInternalResource {

my $data;

# See it it's an attachment
my $d1 = Foswiki::Func::webExists( $web );
my $d2 = Foswiki::Func::attachmentExists( $web, $topic, $attachment );
# if ( Foswiki::Func::attachmentExists( $web, $topic, $attachment ) ) {
if ( $d1 && $d2 ) {
$data = Foswiki::Func::readAttachment( $web, $topic, $attachment, $rev );
# See if it's an attachment
if ( Foswiki::Func::webExists($web)
&& Foswiki::Func::attachmentExists( $web, $topic, $attachment ) )
{
$data =
Foswiki::Func::readAttachment( $web, $topic, $attachment, $rev );
}
else {
# an unusual attachment;
my @webTopic = split '/', $web.'/'.$topic;
my @web = ( );
foreach my $i ( 0 .. $#webTopic) {
if (Foswiki::Func::webExists( join( '/', @web, $webTopic[$i] ) ) ) {
push @web, shift @webTopic;

my @webTopic = split '/', $web . '/' . $topic;
if ( Foswiki::Func::webExists( $webTopic[0] ) ) {

# an unusual attachment like (like jquery plugins)
# (pub/)web/topic/subdirs.../attachment

my @web = ();
foreach my $i ( 0 .. $#webTopic ) {
if (
Foswiki::Func::webExists(
join( '/', @web, $webTopic[$i] )
)
)
{
push @web, shift @webTopic;
}
else {
last;
}
}
else {
last;
$web = join '/', @web;
$topic = shift @webTopic;
$attachment = join '/', @webTopic, $attachment;

eval {
$data =
Foswiki::Func::readAttachment( $web, $topic, $attachment,
$rev );
};
unless ($data) {
$this->logError("$web\.$topic:$attachment is not readable");
return "MISSING RESOURCE $rsrc";
}
}
$web = join '/', @web;
$topic = shift @webTopic;
$attachment = join '/', @webTopic, $attachment;
print STDERR "WEB: $web\nTOPIC: $topic\nATTACHMENT: $attachment\n";
#### if ( Foswiki::Func::attachmentExists( $web, $topic, $attachment ) ) {
$data = Foswiki::Func::readAttachment( $web, $topic, $attachment, $rev );
### }
unless( $data) {
$this->logError("$web\.$topic:$attachment is not readable");
return "MISSING RESOURCE $rsrc";
}
# Not an attachment - pull on our muddy boots and puddle
# around in directories - if they exist!
if (1 == 0) {
my $pubDir = Foswiki::Func::getPubDir();
my $src = "$pubDir/$rsrc";
if ( open( my $fh, '<', $src ) ) {
local $/;
binmode($fh);
$data = <$fh>;
close($fh);
else {
# an unusual attachment (like images created by ImageGaleryPlugin);
# (pub/)directories/file
my $pubDir = Foswiki::Func::getPubDir();
my $src = "$pubDir/$rsrc";
if ( open( my $fh, '<', $src ) ) {
local $/;
binmode($fh);
$data = <$fh>;
close($fh);
}
else {
$this->logError("$src is not readable");
return "MISSING RESOURCE $rsrc";
}
}

}
}

if ( $attachment =~ /\.css$/i ) {
$data =~ s/\burl\((["']?)(.*?)\1\)/$1.$this->_processURL($2).$1/ge;

# styles use url() RELATIVE to the location of the file that makes the reference.
# _processURL will copy the resource to the correct location related to the target
# the reference itself should not be changed.
# SMELL
# the last statement (reference not changed) is not true.
# this works only for references like url(somefile.ext)
# references like url(../someplace/somefile.ext) need to translate to:
# url(../someplace.attachment/somefile.ext)
# could not make this work
# for html this is handled in rewrteTag, specific to the html tag.
# for css this won't happen. Left for another day

while ( $data =~ m/\burl\((["']?)(.*?)\1\)/g ) {
next if $2 =~ m!\A\s*data:!; # nothing to copy!!
$this->_processURL( $2, 'style:' . $rsrc );
}
}
my $path =
$this->{archive}->addAttachment( $web, $topic, $attachment, $data );
Expand Down

0 comments on commit 723d3e6

Please sign in to comment.