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