- generate nice embeddable rss feeds for each recepie
[sysadmin-cookbook-html] / bin / html.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 my $svn = "http://svn.rot13.org/index.cgi/sysadmin-cookbook";
7 my $recepies = 'recepies/';
8
9 use File::Find;
10 use File::Slurp;
11 use Data::Dump qw/dump/;
12 use XML::Simple;
13 use Regexp::Common qw /URI/;
14 use XML::FeedPP;
15
16 my @html;
17 sub html { push @html, @_ }
18
19 my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
20 my $escape_re  = join '|' => keys %escape;
21
22 sub file {
23         my $path = shift;
24         my $content = read_file $path;
25         $content =~ s{[\n\r\s]+$}{}s;
26         $content =~ s/($escape_re)/$escape{$1}/gs;
27         $content =~ s[$RE{URI}{HTTP}{-keep}][<a href="$1">$1</a>]gs;
28
29         my $log = XMLin( scalar `svn log --xml $path`,
30                 ForceArray => [ 'logentry' ],   
31         );
32         my $changes = join("\n",
33                 map {
34                         my $d = $_->{date};
35                         $d =~ s{:\d\d\.\d+Z}{};
36                         $d =~ s{T}{ };
37                         my $r = $_->{revision};
38                         qq|<li>$_->{msg} <a class="date" title="r$r" href="$svn/revision?rev=$r">$d</a></li>|
39                 } reverse @{ $log->{logentry} }
40         );
41
42         $path =~ s{^$recepies/*(.*?[^/]+)$}{$1} || next;
43         return ''
44                 . qq|<ul class=changes>$changes</ul>|
45                 . ( $path =~ m{(\.sh|Makefile)$}i ? qq|<a class="path" href="$svn/view/recepies/$path">$path</a>| : '' )
46                 . qq|<pre class=content>$content</pre>|
47                 ;
48 }
49
50 my @names;
51 find({ follow => 0, no_chdir => 1, wanted => sub {
52         push @names, $_ unless m{/\.} || m{^\.};
53 }}, $recepies );
54
55 my $last_level = 0;
56 my $toc_html = '';
57 sub header {
58         my ($level, $content) = @_;
59         my $display = $content;
60         $display =~ s{^\d+[\.-]}{};
61         $display =~ s{-}{ }g;
62         $display =~ s{\.\w+$}{};
63         $content =~ s{\W+}{_}g;
64         html qq|<a name=$content></a>|;
65         html qq|<h$level>$display</h$level>|;
66
67         if ( $last_level > $level ) {
68                 $toc_html .= "</ul>";
69         } elsif ( $last_level < $level ) {
70                 $toc_html .= "<ul>";
71         }
72         $toc_html .= qq|<li><a href="#$content">$display</li>|;
73         $last_level = $level;
74 }
75
76 my $to_path = '';
77 our @item;
78
79 sub new_feed {
80         my $name = shift;
81         my $feed = XML::FeedPP::RSS->new();
82         $feed->title( "Sysadmin Cookbook" . ( $name ? " :: $name" : '' ) );
83         $feed->link( "http://sysadmin-cookbook.rot13.org/" . ( $name ? "#$name" : '' ) );
84         #$feed->pubDate( "Thu, 23 Feb 2006 14:43:43 +0900" );
85         return $feed;
86 }
87
88 our $feed_all = new_feed;
89 sub add_item {
90         my $name = shift;
91         my $content = join("\n", @_);
92         return unless $name && $content;
93
94         add_feed_item_description($feed_all, $name, "http://sysadmin-cookbook.rot13.org/rss/$name.xml", $content);
95
96         my $item_feed = new_feed( $name );
97         add_feed_item_description($item_feed, $name, "http://sysadmin-cookbook.rot13.org/#$name", $content);
98         $item_feed->to_file("rss/$name.xml");
99
100         warn "# $name\n";
101 }
102
103 sub add_feed_item_description {
104         my ( $feed, $name, $url, $description ) = @_;
105         my $item = $feed->add_item( $url );
106         $item->title( $name );
107         #$item->pubDate( "2006-02-23T14:43:43+09:00" );
108         $item->description( $description );
109 }
110
111 foreach my $path ( sort @names ) {
112
113         next if ( -d $path && ! -e "$path/.svn" );
114
115         my $name = $path;
116         $name =~ s{^$recepies.*?([^/]+)$}{$1} || next;
117         next unless $name;
118
119         if ( -d $path ) {
120                 add_item( splice(@item,0) );
121                 header 1,$name;
122                 $to_path = '';
123                 push @item, $name;
124         } elsif ( -l $path ) {
125                 $to_path = " " . readlink $path;
126                 next;
127         } else {
128                 header 2, $name . $to_path;
129                 $to_path = '';
130                 my $content = file $path;
131                 html $content;
132                 push @item, qq|<h4>$name</h4>\n$content|;
133         }
134
135 };
136
137 $toc_html .= "</ul>" foreach ( 1 .. $last_level );
138
139 $feed_all->to_file( "rss/index.xml" );
140
141 print qq|
142 <html><head>
143 <title>Sysadmin Cookbook</title>
144 <!--
145 <link type=text/css rel=stylesheet href="style.css">
146 -->
147 <style type=text/css>
148
149 h1 {
150         background: #000;
151         color: #fff;
152         padding: 0.3em;
153 }
154
155 .toc {
156         font-size: 80%;
157 }
158
159 pre.changes {
160         color: #444;
161 }
162
163 pre.content {
164         padding: 0.5em;
165         margin: 1em;
166         background: #eee;
167 }
168
169 li .date {
170         font-family: monospace;
171         color: #888;
172         float: right;
173         margin-right: 1em;
174 }
175
176 </style>
177 </head><body>
178         <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>
179         <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>.
180         <br />
181         <small><a href="$svn">Source code repository</a></small>
182         |
183         . "<div class=toc>$toc_html</div>"
184         , join("\n", @html)
185         , "</body></html>"
186         ;
187