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