0436565491e81c5aed5f44af5d5e90db2afe0fda
[sysadmin-cookbook-html] / bin / html.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 my $recepies = '/srv/sysadmin-cookbook/recepies';
7
8 use File::Find;
9 use File::Slurp;
10
11 my $in_ul = 0;
12
13 my @html;
14 sub html { push @html, @_ }
15
16 my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
17 my $escape_re  = join '|' => keys %escape;
18
19 sub file {
20         my $path = shift;
21         my $content = read_file $path;
22         $content =~ s{[\n\r\s]+$}{}s;
23         $content =~ s/($escape_re)/$escape{$1}/gs;
24         return $content;
25 }
26
27 my @names;
28 find({ follow => 0, no_chdir => 1, wanted => sub {
29         push @names, $_ unless m{/\.};
30 }}, $recepies );
31
32 foreach my $path ( sort @names ) {
33
34         my $name = $path;
35         $name =~ s{^$recepies/*}{};
36
37         if ( -d $path ) {
38                 html "</ul>" if $in_ul++;
39                 html "<h1>$name</h1><ul>";
40         } elsif ( -l $path ) {
41                 my $to = readlink $path;
42                 html "<li>$name</li>";
43         } else {
44                 html "<li>$name<pre>", file( $path ), "</pre></li>";
45         }
46
47 };
48
49 html "</ul>" if $in_ul;
50
51 print qq|
52 <html><head>
53 <title>Sysadmin Cookbook</title>
54 <!--
55 <link type=text/css rel=stylesheet href="style.css">
56 -->
57 <style type=text/css>
58 pre {
59         background: #eee;
60 }
61 </style>
62 </head><body>
63         |
64         , join("\n", @html)
65         , "</body></html>"
66         ;
67