create first html rendering of cookbook
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 13 May 2009 19:51:26 +0000 (19:51 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 13 May 2009 19:51:26 +0000 (19:51 +0000)
git-svn-id: file:///home/dpavlin/private/svn/sysadmin-cookbook-html@1 3e18072f-9615-4e06-9d3a-648eafba3f8d

bin/html.pl [new file with mode: 0755]

diff --git a/bin/html.pl b/bin/html.pl
new file mode 100755 (executable)
index 0000000..0436565
--- /dev/null
@@ -0,0 +1,67 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+
+my $recepies = '/srv/sysadmin-cookbook/recepies';
+
+use File::Find;
+use File::Slurp;
+
+my $in_ul = 0;
+
+my @html;
+sub html { push @html, @_ }
+
+my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
+my $escape_re  = join '|' => keys %escape;
+
+sub file {
+       my $path = shift;
+       my $content = read_file $path;
+       $content =~ s{[\n\r\s]+$}{}s;
+       $content =~ s/($escape_re)/$escape{$1}/gs;
+       return $content;
+}
+
+my @names;
+find({ follow => 0, no_chdir => 1, wanted => sub {
+       push @names, $_ unless m{/\.};
+}}, $recepies );
+
+foreach my $path ( sort @names ) {
+
+       my $name = $path;
+       $name =~ s{^$recepies/*}{};
+
+       if ( -d $path ) {
+               html "</ul>" if $in_ul++;
+               html "<h1>$name</h1><ul>";
+       } elsif ( -l $path ) {
+               my $to = readlink $path;
+               html "<li>$name</li>";
+       } else {
+               html "<li>$name<pre>", file( $path ), "</pre></li>";
+       }
+
+};
+
+html "</ul>" if $in_ul;
+
+print qq|
+<html><head>
+<title>Sysadmin Cookbook</title>
+<!--
+<link type=text/css rel=stylesheet href="style.css">
+-->
+<style type=text/css>
+pre {
+       background: #eee;
+}
+</style>
+</head><body>
+       |
+       , join("\n", @html)
+       , "</body></html>"
+       ;
+