[Home]WikiPatches/AlternateStyleSheets

UseModWiki | WikiPatches | RecentChanges | Preferences

This is a small patch with only a few places to modify. I expect it should run on even heavily modified versions of UseMod. It was built on UseMod 1.0 though it should run fine on 0.9x. It is in use on the [FriGames wiki] and was written 2004/9/30 by AdamKatz. This page is at both http://www.frigames.org/UseModChanges/AlternateStyleSheets and UseMod:WikiPatches/AlternateStyleSheets.

A disclaimer, I am quite proficient in shell scripting and in good software design, but I do not count Perl as a language I am terribly proficient in. There are likely places in this code that could use some help, including code with formatting that was mimicked imporperly or sub-optimally.

Usage

This patch takes the contents of the CSSLocalDir (which MUST be directory hosed online as CSSUriDir) directory on your filesystem and adds each stylesheet it contains to a list of alternate stylesheets for each page.

What is an "alternate stylesheet" anyway?

An alternate stylesheet is a seperate viewing of the current page, using a different set of Cascading Stylesheets (CSS). A more detailed description including a nice example can be found at http://www.w3.org/Style/Examples/007/alternatives.html.

How do I view alternate stylesheets?

What it does, how it works

This patch does two things:
  1. Creates alternate stylesheets for web browsers to pick up and use.
  2. Creates preferences for users to always use a certain alternate stylesheet.

Stylesheet Names

Names for alternate stylesheets are taken from comments on the first line of each sheet. If no comment is found, the filename is used, removing the .css at the end.

Installation

Two config variables:

 
 $CSSLocalDir = "";              # Path to directory of alt. CSS stylesheets
 $CSSUrlDir   = "";              # URL matching CSSLocalDir
 

Add CSSLocalDir and CSSUrlDir to the 'use vars qw(' Configuration/constant variables declaration.

In both your configuration file and in the wiki code, add the two above config lines under

 StyleSheet  = "";              # URL for CSS stylesheet (like "/wiki.css")

Insert the following function somewhere (advice needed as to where!), I chose between 'sub GetHistoryLink' and 'sub GetHeader'

 # AlternateStyleSheets patch by AdamKatz
 # http://www.usemod.com/cgi-bin/wiki.pl?WikiPatches/AlternateStyleSheets 
 sub GetAlternateStyleSheets {
   if ($CSSLocalDir ne '' and $CSSUrlDir ne '') {
     my ($arg) = @_;
     my $sheet;
     my $altsheets;
     my %menuitems = ('' => "User Specified"); # allow 'use URL' option
     opendir DIRH, "$CSSLocalDir" or die "couldn't open: $!";
     foreach (sort readdir DIRH) {
       $sheet = $_;
       if ($sheet =~ m/\.css$/) {
         open(INFILE,$CSSLocalDir.'/'.$sheet) or return 0;
         chomp ($_ = <INFILE>);
         close INFILE;
         if (defined $_ and $_ =~ m:/\* *(.*?) *\*/:) {# line 1 has a comment
           $_ = $1;                                    # grab only the comment
         } else {                                      # no comment, use filename
           $_ = $sheet;
           $_ =~ s/\.css$//;
         }
         if ($arg =~ m/head/) {
           $altsheets .= qq(<link rel="alternate stylesheet" title="$_" );
           $altsheets .= qq(href="$CSSUrlDir/$sheet">\n);
         } else {
           %menuitems = (%menuitems, "$CSSUrlDir/$sheet"=>"$_");
         }
       }
     }
     if (not $arg =~ m/head/) {
       my @menuvalues = keys(%menuitems);
       # if using a stylesheet URL (not an altss), blank altss (user-specified)
       if (not exists($menuitems{$arg})) {
         $arg = '';
       }
       $altsheets .= '<br />' . T('StyleSheet:') . ' ';
       $altsheets .= $q->popup_menu(-name=>'p_altstylesheet',
                                    -values=>\@menuvalues,
                                    -default=>$arg,
                                    -labels=>\%menuitems);
       $altsheets .= ' ' . T('or enter a');
     }
     closedir DIRH;
     return $altsheets;
   }
   return "";
 }
 # End of AlternateStyleSheets patch
 

Find these lines:

  
  if ($stylesheet ne '') {      
    $html .= qq(<link rel="stylesheet" href="$stylesheet">\n);
  }
  
Add this after them:
  
  $html .= GetAlternateStyleSheets("head");
  

Find these lines:

  
  print '<br>' . T('StyleSheet URL:') . ' ',
        &GetFormText('stylesheet', "", 30, 150);
  
Add this before them:
  
  print GetAlternateStyleSheets(&GetParam('stylesheet', 1));
  

Find thie line:

  
  $stylesheet = &GetParam('p_stylesheet', '');
  
Add this after it:
  
  my $altstylesheet = &GetParam('p_altstylesheet', '');
  my $currentstyle = $UserData{'stylesheet'};
  # use altss only if it is changed and not blank (blank=user-specified)
  # and only if the user-specified stylesheet is unchanged or blank)
  if (($altstylesheet ne $currentstyle  and  $altstylesheet ne '')
      and ($stylesheet eq $currentstyle  or  $stylesheet eq '')) {
    $stylesheet = $altstylesheet;  # trump user-specified stylesheet URL
  }
  

Bugs

The name of the currently selected stylesheet is not extracted. Therefore, it has no title and Firefox displays it as "Basic Theme." The Preferences page displays it by full path. This is an attempt at keeping the complexity and length of code to a minimum. It also minimizes the server load. frigames.org is a 350MHz machine and needs all the help it can get in this department.



UseModWiki | WikiPatches | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited November 6, 2007 11:54 pm by MarkusLude (diff)
Search: