Skip to content

Instantly share code, notes, and snippets.

@jesusbagpuss
Created April 28, 2022 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesusbagpuss/c5ae75d8cbafb902af8299ec57101d8c to your computer and use it in GitHub Desktop.
Save jesusbagpuss/c5ae75d8cbafb902af8299ec57101d8c to your computer and use it in GitHub Desktop.
Hack different auto css/js versions into template
# save into [ARCHIVEID]/cfg/cfg.d/
#
#This trigger will find the auto- js and css references in the <head>.
# It must be called after the ~/lib/cfg.d/template_core.pl trigger (so the <head> has already been defined,
# - the priority (see end of code) is set lower than that template trigger.
#
# It will add a version number to the href/src e.g.:
# auto-3.3.10.js
# auto-3.3.101.js
#
# The code that re-creates the auto- files EPrints::Apache::Rewrite
# will match any series of digits - so auto-3.3.10000000000000000 would be an option...
$c->add_trigger( EP_TRIGGER_DYNAMIC_TEMPLATE, sub {
my %params = @_;
my $repo = $params{repository};
my $pins = $params{pins};
my $xhtml = $repo->xhtml;
my $jsVersion = 1; # this digit gets appended to the EPrints version number 3.3.10 -> 3.3.101
my $cssVersion = undef;
if( defined $pins->{head} )
{
my $head = $pins->{head};
# polish auto JS links
if( $jsVersion ){
my $js_src = $repo->current_url( path => "static", "javascript/auto-".EPrints->human_version.".js" );
(my $new_js_src = $js_src ) =~ s/\.js$/$jsVersion.js/;
my ($script) = $head->findnodes( "script[\@src='$js_src']" );
$script->setAttribute( src => $new_js_src );
if( defined $pins->{'utf-8.head'} )
{
$pins->{'utf-8.head'} =~ s/$js_src/$new_js_src/;
}
}
# polish auto CSS links
if( $cssVersion ){
my $css_href = $repo->current_url( path => "static", "style/auto-".EPrints->human_version.".css" );
(my $new_css_href = $css_href ) =~ s/\.css$/$cssVersion.css/;
my ($link) = $head->findnodes( "link[\@href='$css_href']" );
$link->setAttribute( href => $new_css_href );
if( defined $pins->{'utf-8.head'} )
{
$pins->{'utf-8.head'} =~ s/$css_href/$new_css_href/;
}
}
$pins->{head} = $head;
}
return;
}, priority => 1001); #run after ~/lib/cfg.d/template_core.pl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment