[Lxr-dev] Use static files where possible
Brought to you by:
ajlittoz
From: Mel G. <me...@cs...> - 2003-01-29 19:18:06
|
(Second attempt, first mail got held) This is a patch against the stable version 0.3 to use static files whenever possible. I didn't check much against 0.9 but it looks like it would not take a lot of work to apply if active developers are interested in this feature. Obviously, I am not an active developer, nor am I subscribed to this list. This is a flying visit :-) The basic idea is that there is a static/ directory as well as a source/ . If a file is browsed with the source script the first time, a static HTML file will be outputted to static/ . The fileref() function checks to see if a static file is available when creating links and links to it if it is, or to the source script if it is not. Pros o Browser can now use cached local versions (nice for lagging connections) o LXR has less work to do as it doesn't regenerate the same file repeatadly Cons o Server needs more disk space as it eventually will have a second copy of the entire source Additinally, I tested this on my local LXR setup at http://136.201.24.87/lxr/http/source . For browsing a file, the rough timings were 1. Script generate and download file - 13 seconds 2. View static version for first time - 8 seconds 3. View static second time - 3 seconds diff -ruN lxr-0.3-clean/Common.pm lxr-0.3-mel/Common.pm --- lxr-0.3-clean/Common.pm Wed Jan 29 15:08:16 2003 +++ lxr-0.3-mel/Common.pm Wed Jan 29 18:09:46 2003 @@ -80,10 +80,25 @@ sub fileref { my ($desc, $path, $line, @args) = @_; - return("<a href=\"source$path". + local ($staticpath, $urlbase); + + # Check for a static version + $staticpath = $Conf->staticroot . $path . ".html"; + + if ( -e $staticpath ) { + return("<a href=\"" . $Conf->staticurl . + "/" . + $Conf->variable('v') . + "$path.html". + &urlargs(@args). + ($line > 0 ? "#L$line" : ""). + "\"\>$desc</a>"); + } else { + return("<a href=\"source$path". &urlargs(@args). ($line > 0 ? "#L$line" : ""). "\"\>$desc</a>"); + } } @@ -523,6 +538,8 @@ sub makeheader { local $who = shift; + local $outfun = shift; + local $expanded; if ($Conf->htmlhead && !open(TEMPL, $Conf->htmlhead)) { &warning("Template ".$Conf->htmlhead." does not exist."); @@ -534,7 +551,6 @@ close(TEMPL); } - print( #"<!doctype html public \"-//W3C//DTD HTML 3.2//EN\">\n", # "<html>\n", # "<head>\n", @@ -542,18 +558,25 @@ # "<base href=\"",$Conf->baseurl,"\">\n", # "</head>\n", - &expandtemplate($template, + $expanded = &expandtemplate($template, ('title', \&titleexpand), ('banner', \&bannerexpand), ('baseurl', \&baseurl), ('thisurl', \&thisurl), ('modes', \&modeexpand), - ('variables', \&varexpand))); + ('variables', \&varexpand)); + + if ($who eq "source") { + &$outfun($expanded); + } + print($expanded); } sub makefooter { local $who = shift; + local $outfun = shift; + local $expanded; if ($Conf->htmltail && !open(TEMPL, $Conf->htmltail)) { &warning("Template ".$Conf->htmltail." does not exist."); @@ -565,12 +588,17 @@ close(TEMPL); } - print(&expandtemplate($template, + $expanded = &expandtemplate($template, ('banner', \&bannerexpand), ('thisurl', \&thisurl), ('modes', \&modeexpand), - ('variables', \&varexpand)), - "</html>\n"); + ('variables', \&varexpand)) . + "</html>\n"; + if ($who eq "source") { + &$outfun($expanded); + } + print $expanded; + } diff -ruN lxr-0.3-clean/Config.pm lxr-0.3-mel/Config.pm --- lxr-0.3-clean/Config.pm Wed Jan 29 15:08:16 2003 +++ lxr-0.3-mel/Config.pm Wed Jan 29 18:35:02 2003 @@ -72,7 +72,7 @@ unless ($conf) { ($conf = $0) =~ s#/[^/]+$#/#; - $conf .= $confname; + $conf = $confname; } unless (open(CONFIG, $conf)) { @@ -96,6 +96,8 @@ $self->{vdefault}->{$args[0]}; } } elsif ($dir eq 'sourceroot' || + $dir eq 'staticroot' || + $dir eq 'staticurl' || $dir eq 'srcrootname' || $dir eq 'baseurl' || $dir eq 'incprefix' || @@ -171,6 +173,15 @@ return($self->varexpand($self->{'sourceroot'})); } +sub staticroot { + my $self = shift; + return($self->varexpand($self->{'staticroot'})); +} + +sub staticurl { + my $self = shift; + return($self->varexpand($self->{'staticurl'})); +} sub sourcerootname { my $self = shift; diff -ruN lxr-0.3-clean/Makefile lxr-0.3-mel/Makefile --- lxr-0.3-clean/Makefile Wed Jan 29 15:08:16 2003 +++ lxr-0.3-mel/Makefile Wed Jan 29 18:12:34 2003 @@ -22,6 +22,7 @@ install --directory $(INSTALLPREFIX)/http/lib/LXR install --directory $(INSTALLPREFIX)/bin install --directory $(INSTALLPREFIX)/source + install --directory $(INSTALLPREFIX)/static install --mode 755 $(CGISCRIPTS) $(INSTALLPREFIX)/http/ install --mode 750 genxref $(INSTALLPREFIX)/bin/ install --mode 755 Common.pm Config.pm $(INSTALLPREFIX)/http/lib/LXR diff -ruN lxr-0.3-clean/http/lxr.conf lxr-0.3-mel/http/lxr.conf --- lxr-0.3-clean/http/lxr.conf Wed Jan 29 15:08:16 2003 +++ lxr-0.3-mel/http/lxr.conf Wed Jan 29 18:38:01 2003 @@ -7,7 +7,10 @@ variable: a, Architecture, (i386, alpha, m68k, mips, ppc, sparc, sparc64) # Define the base url for the LXR files. -baseurl: http://lxr/ +baseurl: http://lxr/http/ + +# Define the base url for statically generated LXR files +staticurl: http://lxr/static/ # These are the templates for the HTML heading, directory listing and # footer, respectively. @@ -18,6 +21,9 @@ # The source is here. sourceroot: /local/lxr/source/$v/linux/ srcrootname: Linux + +# The static root is here +staticroot: /local/lxr/static/$v/ # "#include <foo.h>" is mapped to this directory (in the LXR source # tree) diff -ruN lxr-0.3-clean/source.in lxr-0.3-mel/source.in --- lxr-0.3-clean/source.in Wed Jan 29 15:08:16 2003 +++ lxr-0.3-mel/source.in Wed Jan 29 18:33:52 2003 @@ -203,6 +203,7 @@ if (open(SRCFILE, $Path->{'real'}.README)) { print("<hr><pre>"); + &markupfile(\*SRCFILE, $Path->{'virt'}, 'README', sub { print shift }); print("</pre>"); @@ -212,9 +213,11 @@ } else { if (open(SRCFILE, $Path->{'realf'})) { print("<pre>"); + print STATIC ("<pre>"); &markupfile(\*SRCFILE, $Path->{'virt'}, $Path->{'file'}, - sub { print shift }); + sub { $out = shift; print $out; print STATIC $out; }); print("</pre>"); + print STATIC "</pre>"; close(SRCFILE); } else { @@ -227,9 +230,42 @@ } } +sub openstatic { + local $mkpath; + local $pathpart; + local $fullpath; + + unless (! $Path->{'file'}) { + # Check if there is a static version of this file + if (-e ($Conf->staticroot . + $Path->{'virt'} . + $Path->{'file'})) { return; } + + #Create the required directory + $fullpath = $Conf->staticroot . $Path->{'virt'}; + foreach $pathpart (split/\//, $fullpath) { + $mkpath .= "/$pathpart"; + if ( ! -e $mkpath) { mkdir($mkpath); } + } + + # Open the file + open(STATIC, ">$fullpath/" . $Path->{'file'} . ".html") || + open(STATIC, ">$fullpath/" . $Path->{'file'} . ".html") || + &warning("Could not open static file: $fullpath" . $Path->{'file'}); + } +} + +sub closestatic { + unless (! $Path->{'file'}) { + close STATIC; + } +} + ($Conf, $HTTP, $Path) = &init; -&makeheader('source'); +&openstatic; +&makeheader('source', sub { print STATIC shift }); &printfile; -&makefooter('source'); +&makefooter('source', sub { print STATIC shift }); +&closestatic; |