link to .pl files
[sysadmin-cookbook-html] / bin / html.pl
index 87d6dfa..f247856 100755 (executable)
@@ -3,13 +3,16 @@
 use warnings;
 use strict;
 
-my $recepies = '/srv/sysadmin-cookbook/recepies';
+my $svn = "http://svn.rot13.org/index.cgi/sysadmin-cookbook";
+my $recepies = 'recepies/';
 
 use File::Find;
 use File::Slurp;
+use File::Path;
 use Data::Dump qw/dump/;
 use XML::Simple;
 use Regexp::Common qw /URI/;
+use XML::FeedPP;
 
 my @html;
 sub html { push @html, @_ }
@@ -30,35 +33,41 @@ sub file {
        my $changes = join("\n",
                map {
                        my $d = $_->{date};
-                       $d =~ s{\.\d+Z}{};
+                       $d =~ s{:\d\d\.\d+Z}{};
                        $d =~ s{T}{ };
-                       qq|<li>$_->{msg} <span class="date">$d</span></li>|
-               } @{ $log->{logentry} }
+                       my $r = $_->{revision};
+                       qq|<li>$_->{msg} <a class="date" title="r$r" href="$svn/revision?rev=$r">$d</a></li>|
+               } reverse @{ $log->{logentry} }
        );
 
        $path =~ s{^$recepies/*(.*?[^/]+)$}{$1} || next;
        return ''
                . qq|<ul class=changes>$changes</ul>|
-               . ( $path =~ m{(\.sh|Makefile)$}i ? qq|<a class="path" href="recepies/$path">$path</a>| : '' )
+               . ( $path =~ m{(\.sh|Makefile|\.conf|\.pl)$}i || -e $path ? qq|<a class="path" href="$svn/view/recepies/$path">$path</a>| : '' )
                . qq|<pre class=content>$content</pre>|
                ;
 }
 
 my @names;
 find({ follow => 0, no_chdir => 1, wanted => sub {
-       push @names, $_ unless m{/\.};
+       push @names, $_ unless m{/\.} || m{^\.};
 }}, $recepies );
 
 my $last_level = 0;
 my $toc_html = '';
 sub header {
-       my ($level, $content) = @_;
-       my $display = $content;
+       my ($level, $name) = @_;
+
+       my $display = $name;
        $display =~ s{^\d+[\.-]}{};
        $display =~ s{-}{ }g;
        $display =~ s{\.\w+$}{};
-       $content =~ s{\W+}{_}g;
-       html qq|<a name=$content></a>|;
+
+       my $anchor = $name;
+       $anchor =~ s{</?[^>]+>}{}g;
+       $anchor =~ s{\W+}{_}g;
+
+       html qq|<a name=$anchor></a>|;
        html qq|<h$level>$display</h$level>|;
 
        if ( $last_level > $level ) {
@@ -66,34 +75,89 @@ sub header {
        } elsif ( $last_level < $level ) {
                $toc_html .= "<ul>";
        }
-       $toc_html .= qq|<li><a href="#$content">$display</li>|;
+       $toc_html .= qq|<li><a href="#$anchor">$display</li>|;
        $last_level = $level;
 }
 
 my $to_path = '';
+our @item;
+
+sub mkfilepath {
+       my $path = shift;
+       $path =~ s{/[^/]+$}{};
+       mkpath $path unless -d $path;
+}
+
+sub new_feed {
+       my $name = shift;
+       my $feed = XML::FeedPP::RSS->new();
+       $feed->title( "Sysadmin Cookbook" . ( $name ? " :: $name" : '' ) );
+       $feed->link( "http://sysadmin-cookbook.rot13.org/" . ( $name ? "#$name" : '' ) );
+       #$feed->pubDate( "Thu, 23 Feb 2006 14:43:43 +0900" );
+       return $feed;
+}
+
+our $feed_all = new_feed;
+sub add_item {
+       my $name = shift;
+       my $content = join("\n", @_);
+       return unless $name && $content;
+
+       add_feed_item_description($feed_all, $name, "http://sysadmin-cookbook.rot13.org/rss/$name.xml", $content);
+
+       my $item_feed = new_feed( $name );
+       add_feed_item_description($item_feed, $name, "http://sysadmin-cookbook.rot13.org/#$name", $content);
+       my $file = "rss/$name.xml";
+       mkfilepath $file;
+       $item_feed->to_file($file);
+
+       warn "# $name\n";
+}
+
+sub add_feed_item_description {
+       my ( $feed, $name, $url, $description ) = @_;
+       my $item = $feed->add_item( $url );
+       $item->title( $name );
+       #$item->pubDate( "2006-02-23T14:43:43+09:00" );
+       $item->description( $description );
+}
 
 foreach my $path ( sort @names ) {
 
+       next if ( -d $path && ! -e "$path/.svn" );
+
        my $name = $path;
+#      $name =~ s{^$recepies.*?([^/]+)$}{$1} || next;
        $name =~ s{^$recepies.*?([^/]+)$}{$1} || next;
-       next unless $name;
+
+       my @just_path = split m{/}, $path;
+       @just_path = splice @just_path, 1, -1;
 
        if ( -d $path ) {
-               header 1,$name;
+               add_item( splice(@item,0) );
+               my $h1 = join(' ',@just_path);
+               $h1 = qq|<span class="p">$h1</span> | if $h1;
+               $h1 .= $name;
+               header 1, $h1;
                $to_path = '';
+               push @item, $name;
        } elsif ( -l $path ) {
                $to_path = " " . readlink $path;
                next;
        } else {
                header 2, $name . $to_path;
                $to_path = '';
-               html file( $path );
+               my $content = file $path;
+               html $content;
+               push @item, qq|<h4>$name</h4>\n$content|;
        }
 
 };
 
 $toc_html .= "</ul>" foreach ( 1 .. $last_level );
 
+$feed_all->to_file( "rss/index.xml" );
+
 print qq|
 <html><head>
 <title>Sysadmin Cookbook</title>
@@ -108,6 +172,10 @@ h1 {
        padding: 0.3em;
 }
 
+h1 .p {
+       color: #888;
+}
+
 .toc {
        font-size: 80%;
 }
@@ -126,6 +194,7 @@ li .date {
        font-family: monospace;
        color: #888;
        float: right;
+       margin-right: 1em;
 }
 
 </style>
@@ -133,7 +202,7 @@ li .date {
        <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/hr/"><img alt="Creative Commons License" style="border-width:0; float: right" src="http://i.creativecommons.org/l/by-nc-sa/3.0/hr/88x31.png" /></a>
        <span xmlns:dc="http://purl.org/dc/elements/1.1/" href="http://purl.org/dc/dcmitype/Text" property="dc:title" rel="dc:type">Sysadmin Cookbook</span> by <a xmlns:cc="http://creativecommons.org/ns#" href="http://www.rot13.org/~dpavlin/" property="cc:attributionName" rel="cc:attributionURL">Dobrica Pavlinusic</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/hr/">Creative Commons Attribution-Noncommercial-Share Alike 3.0 Croatia License</a>.
        <br />
-       <small><a href="http://svn.rot13.org/index.cgi/sysadmin-cookbook/">Source code repository</a></small>
+       <small><a href="$svn">Source code repository</a></small>
        |
        . "<div class=toc>$toc_html</div>"
        , join("\n", @html)