1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
#!/usr/bin/perl
package Apache::OTL;
use strict;
use Apache::Constants qw/ :common /;
use Time::HiRes qw/ gettimeofday /;
sub handler
{
my $r = shift;
my $VERSION = '0.3';
my $t0 = Time::HiRes::gettimeofday;
my (
$file, # the absolute file path
$title, # the file's title
$uri, # the file uri
%re, # a hash of pre compiled regular expressions
$data, # file contents
%opt, # options from the otl file
@blocks, # todo groupings
$mtime, # last modification time of otl file
%get, # get arguments (sorting, etc)
);
return DECLINED unless $r->method() eq 'GET';
($file, $uri) = ($r->filename, $r->uri);
return DECLINED unless -e $file;
$mtime = localtime( (stat(_))[9] );
%get = $r->args;
%re =
(
title => qr/(?:.+)?\/(.+).otl$/i,
percent => qr/(\[.\]) (\d+)%/,
todo => qr/(\[_\]) /,
done => qr/(\[X\]) /,
comment => qr/^(?:\t+)?:(.+)/,
time => qr/(\d{2}:\d{2}:\d{2})/,
date => qr/(\d{2,4}-\d{2}-\d{2})/,
subitem => qr/^\t(?!\t)/,
line_wo_tabs => qr/^(?:\t+)?(.+)/,
linetext => qr/^(?:\[.\] (?:\d+%)?)? (.+)/,
);
open OTL, "$file"
|| ( $r->log_error("Unable to read $file: $!") && return DECLINED );
do {
local $/ = undef;
$data = <OTL>; # shlorp
};
close OTL;
# divide each outline into groups
@blocks = split /\n\n+/, $data;
# get optional settings and otl title
{
my $settings = shift @blocks;
if ($settings =~ $re{comment}) {
%opt = map { split /=/ } split /\s?:/, $settings;
}
# if the first group wasn't a comment,
# we probably just aren't using a settings
# line. push the group back into place.
else {
unshift @blocks, $settings;
}
}
# GET args override settings
$opt{$_} = $get{$_} foreach keys %get;
# set title (fallback to file uri)
$title =
$opt{title}
? $opt{title}
: $1 if $uri =~ $re{title};
$opt{style} ||= '/otl_style.css';
$r->send_http_header('text/html');
$r->print(<<EHTML);
<html>
<!--
generated by otl_handler $VERSION
Mahlon E. Smith <mahlon\@spime.net>
http://www.vimoutliner.org/
-->
<head>
<title>$title</title>
<link href="$opt{style}" rel="stylesheet" media="screen" type="text/css">
EHTML
if ($opt{js}) {
$r->print(
' ' x 8,
"<script type=\"text/javascript\" language=\"JavaScript\" src=\"$opt{js}\"></script>\n",
' ' x 4, "</head>\n",
"<body onLoad=\"init_page()\">\n",
);
} else {
$r->print(<<EHTML);
</head>
<body>
EHTML
}
$r->print("<span class=\"header\">$opt{title}</span><br />\n") if $opt{title};
$r->print("<span class=\"last_mod\">Last modified: $mtime</span><br />\n") if $opt{last_mod};
if ($opt{legend}) {
$r->print(<<EHTML);
<div class="legend">
<span class="done"> </span> Item completed<br />
<span class="todo"> </span> Item is incomplete<br />
</div>
EHTML
}
if ($opt{sort}) {
my %sorts = (
alpha => 'alphabetical',
percent => 'percentages',
);
$r->print("<div class=\"sort\">Sort: \n");
foreach (sort keys %sorts) {
if ($opt{sorttype} eq $_ && $opt{sortrev}) {
$r->print("<a href=\"$uri?sorttype=$_\">$sorts{$_}</a> ");
} elsif ($opt{sorttype} eq $_ && ! $opt{sortrev}) {
$r->print("<a href=\"$uri?sorttype=$_&sortrev=1\">$sorts{$_}</a> ");
} else {
$r->print("<a href=\"$uri?sorttype=$_\">$sorts{$_}</a> ");
}
}
$r->print("</div>\n");
}
my $bc = 0;
foreach my $block ( sort { sorter(\%opt, \%re) } @blocks ) {
# separate outline items
$r->print("<div class=\"group\">\n") if $opt{divs};
my $lc = 0;
my @items = split /\n/, $block;
# get item counts
my ($subs, $comments, $subsubs);
if ($opt{counts}) {
foreach (@items) {
if (/$re{comment}/) {
$comments++;
} elsif (/$re{subitem}/) {
$subs++;
}
}
$subsubs = (scalar @items - 1) - $subs - $comments;;
}
# parse
foreach (@items) {
my $level = tr/\t/\t/ || 0;
next unless /\w/;
# append counts
if ($lc == 0 && $opt{counts} && $_ !~ $re{comment}) {
my $itmstr = $subs == 1 ? 'item' : 'items';
my $sitmstr = $subsubs == 1 ? 'subitem' : 'subitems';
$_ .= " <span class=\"counts\">$subs $itmstr, $subsubs $sitmstr</span>";
}
s/^:// if ! $level;
if ($opt{js}) {
s#(.+)#<span id=\"itemtoplevel_$bc\">$1</span># if $lc == 0;
$r->print("<span id=\"itemgroup_$bc\">\n") if $lc == 1;
}
s#$re{'time'}#<span class="time">$1</span>#g if /$re{'time'}/;
s#$re{date}#<span class="date">$1</span>#g if /$re{date}/;
s#$re{percent}#$1 <span class="percent">$2%</span># if /$re{percent}/;
s#$re{todo}#<span class="todo"> </span># if /$re{todo}/;
s#$re{done}#<span class="done"> </span># if /$re{done}/;
s#$re{comment}#<span class="comment">$1</span># if /$re{comment}/;
s#$re{line_wo_tabs}#<span class="level$level">$1</span>#;
$r->print("$_\n");
$lc++;
}
$r->print("</span>\n") if $opt{js};
$r->print("</div>\n") if $opt{divs};
$r->print("<br /><hr /><br />\n") if $opt{dividers};
$r->print("<br /><br />\n") unless $opt{divs} || $opt{dividers};
$bc++;
}
my $t1 = Time::HiRes::gettimeofday;
my $td = sprintf("%0.3f", $t1 - $t0);
$r->print("<div class=\"timer\">OTL parsed in $td secs</div>") if $opt{timer};
$r->print(<<EHTML);
</body>
</html>
EHTML
return OK;
}
sub sorter
{
my ($opt, $re) = @_;
return 0 unless $opt->{sorttype};
my ($sa, $sb);
if ($opt->{sorttype} eq 'percent') {
$sa = $2 if $a =~ $re->{percent};
$sb = $2 if $b =~ $re->{percent};
return $opt->{sortrev} ? $sb <=> $sa : $sa <=> $sb;
}
else {
$sa = $1 if $a =~ $re->{linetext};
$sb = $1 if $b =~ $re->{linetext};
return $opt->{sortrev} ? $sb cmp $sa : $sa cmp $sb;
}
}
1;
|