| 1 | package HTTP::AppServer::Plugin::PlainHTML; | ||
| 2 | # Plugin for HTTP::AppServer that creates static html pages for | ||
| 3 | # a given set of database tables to make a website better accessable | ||
| 4 | # for search engine crawlers and drive SEO. | ||
| 5 | # 2010 by Tom Kirchner | ||
| 6 | |||
| 7 | use 5.010000; | ||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use HTTP::AppServer::Plugin; | ||
| 11 | use base qw(HTTP::AppServer::Plugin); | ||
| 12 | |||
| 13 | our $VERSION = '0.01'; | ||
| 14 | |||
| 15 | my $Tables = { | ||
| 16 | # <tablename> => { | ||
| 17 | # url => <url>, | ||
| 18 | # title => <string>, | ||
| 19 | # description => <string>, | ||
| 20 | # } | ||
| 21 | }; | ||
| 22 | |||
| 23 | my $JavaScriptURL = ''; | ||
| 24 | |||
| 25 | # called by the server when the plugin is installed | ||
| 26 | # to determine which routes are handled by the plugin | ||
| 27 | sub init | ||
| 28 | { | ||
| 29 | my ($class, $server, %options) = @_; | ||
| 30 | |||
| 31 | # all database tables that should be able to be dumped | ||
| 32 | $Tables = $options{'Tables'} if exists $options{'Tables'}; | ||
| 33 | |||
| 34 | # the url the integrated JS should redirect to | ||
| 35 | $JavaScriptURL = $options{'JavaScriptURL'} if exists $options{'JavaScriptURL'}; | ||
| 36 | |||
| 37 | # the plugin installs a URL handler | ||
| 38 | my @handlers = (); | ||
| 39 | foreach my $tablename (keys %{$Tables}) { | ||
| 40 | my $info = $Tables->{$tablename}; | ||
| 41 | $info->{'tablename'} = $tablename; | ||
| 42 | push @handlers, | ||
| 43 | ($info->{'url'}, | ||
| 44 | sub { | ||
| 45 | my ($server, $cgi) = @_; | ||
| 46 | return _dump_as_html($server, $cgi, $info); | ||
| 47 | }); | ||
| 48 | } | ||
| 49 | return @handlers; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub _dump_as_html | ||
| 53 | { | ||
| 54 | my ($server, $cgi, $info) = @_; | ||
| 55 | my @rows = $server->findall(-tables => [$info->{'tablename'}]); | ||
| 56 | |||
| 57 | print "HTTP/1.0 200 Ok\r\n"; | ||
| 58 | print $cgi->header('text/html'); | ||
| 59 | print '<html><head><title>'.ucfirst($info->{'title'}).'</title></head><body>'; | ||
| 60 | print '<script>window.location = "'.$JavaScriptURL.'";</script>' | ||
| 61 | if length $JavaScriptURL; | ||
| 62 | print '<h1>'.ucfirst($info->{'title'}).'</h1>'; | ||
| 63 | print '<p>'.ucfirst($info->{'description'}).'</p>'; | ||
| 64 | foreach my $row (@rows) { | ||
| 65 | print '<dl>'; | ||
| 66 | foreach my $key (sort keys %{$row}) { | ||
| 67 | print '<dt>'.ucfirst($key).':</dt><dd>'.$row->{$key}.'</dd>'; | ||
| 68 | } | ||
| 69 | print '</dl>'; | ||
| 70 | } | ||
| 71 | print '</body></html>'; | ||
| 72 | return 1; | ||
| 73 | } | ||
| 74 | |||
| 75 | 1; | ||
| 76 | __END__ |