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