CSSoD is a Perl solution that provides CSS on-the-fly, based on user preferences.
CSSoD consists of the Style Sheet generator programme (css), the CGI programme to allow the user to change their preferences (css_prefs) and a small piece of code that goes into the main Perl CGI to call the Style Sheet and to preserve state.
State may be preserved session-to-session by means of cookies; when cookies are unavailable, the CSS state information is carried using a simple query string.
CSSoD currently allows three parameters to be controlled:
My original implementation of CSSoD used extraordinarily long query strings which were scarcely human-readable and not at all bandwidth-efficient. These strings were along the lines of:
http://mysite/mycgi/css?size=large;scheme=colour;showimgs=y
If this were combined with other query data, it could make for a long string and also required quite a bit of code to make sense of.
The new approach is greatly simplified, with a single variable being used:
http://mysite/mycgi/css?css=100
The first digit in the variable represents the font size, the second the colour scheme and the third, whether or not to display images. The default value of each is 0, which equates to 'normal' font size, standard colours and display images (active FALSE).
At present, only three colour schemes are defined; suggestions for foreground/background combinations are welcome and will be incorporated into the system. Using a single, case-insensitive, character from 0-9, a-z, gives us a possible 36 different schemes which can be defined. This would probably be confusing for many users - half a dozen sounds like a more readable number, provided that they would cover all the basic demands of accessibility.
At present, the image display digit is either 0 (show images) or 1 (don't show images). The original project, from which sprang CSSoD, only used images as "eye candy" which is why the situation is so simple. This is obviously not adequate for many situations, so further development will still be a simple on/off, but with two classes of image beind defined, decorative and meaningful. This means that we can opt to display meaningful images, or at least present them so that their alttext or longdesc properties may be read.
The following snippet of code needs to be inserted into the Perl programme producing your page:
# Set style sheet preferences from cookie or query string
my $css;
if ($q->cookie('css')) { $css="css=" . $q->cookie('css'); } else { $css="css=" . $q->param('css'); }
my $csssrc="/cc/css?$css";
CSSoD is designed to work in conjunction with the Perl CGI module; the page header, which must appear after the above code may look like this:
print $q->header(-type=>'text/html', -expires=>'-1d');
print $q->start_html(
-title=>"Matthew Smith's Homepage",
-style=>{'src'=>$csssrc},
);
URLs of links within the document need to pass on state information in case we do not have cookies available; this can either be the query string or be appended to the query string if one already exists.
http://mysite/mycgi/mypage?$css
When calling the preferences programme (css_prefs), we need to include our current URL (escaped) in the query string so that we can return once the preferences have been set. $q is the CGI handle.
use URI::escape; my $self=$q->url; my $eself=uri_escape($self); print "<a href=\"/mycgi/css_prefs?referer=$eself\">Preferences</a>";
Of course, if we already have some preferences set, we should send them on too:
print "<a href=\"/mycgi/css_prefs?referer=$eself;$css\">Preferences</a>";
The best way to understand is to have a play - Change Preferences
The files css and css_prefs are available as a tarball: css_files_0.5.tgz
The css_prefs supplied in the original tarball (css_files_0.1.tgz) contains a few bugs which appeared when converting from the original code; this included the loss of cookie capabilities. The current version is css_files_0.5.tgz
CSSoD, being Perl based, is distributed under the same terms as Perl itself. Credit to the author appreciated.
Matthew Smith - Kadina, South Australia
January 2003