/[sysadmin-cookbook-html]/bin/html.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /bin/html.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (hide annotations)
Wed May 13 20:16:30 2009 UTC (15 years, 1 month ago) by dpavlin
File MIME type: text/plain
File size: 1695 byte(s)
added table of contents

1 dpavlin 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 dpavlin 2 use Data::Dump qw/dump/;
11 dpavlin 1
12     my @html;
13     sub html { push @html, @_ }
14    
15     my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
16     my $escape_re = join '|' => keys %escape;
17    
18     sub file {
19     my $path = shift;
20     my $content = read_file $path;
21     $content =~ s{[\n\r\s]+$}{}s;
22     $content =~ s/($escape_re)/$escape{$1}/gs;
23 dpavlin 2 return ''
24     . "<pre class=changes>" . `svn log $path` . "</pre>"
25     . "<pre class=content>$content</pre>"
26     ;
27 dpavlin 1 }
28    
29     my @names;
30     find({ follow => 0, no_chdir => 1, wanted => sub {
31     push @names, $_ unless m{/\.};
32     }}, $recepies );
33    
34 dpavlin 2 my $last_level = 0;
35     my $toc_html = '';
36     sub header {
37     my ($level, $content) = @_;
38     html qq|<a name=$content></a>|;
39     html qq|<h$level>$content</h$level>|;
40    
41     if ( $last_level > $level ) {
42     $toc_html .= "</ul>";
43     } elsif ( $last_level < $level ) {
44     $toc_html .= "<ul>";
45     }
46     $toc_html .= qq|<li><a href="#$content">$content</li>|;
47     $last_level = $level;
48     }
49    
50 dpavlin 1 foreach my $path ( sort @names ) {
51    
52     my $name = $path;
53 dpavlin 2 $name =~ s{^$recepies.*?([^/]+)$}{$1};
54     next unless $name;
55 dpavlin 1
56     if ( -d $path ) {
57 dpavlin 2 header 1,$name;
58 dpavlin 1 } elsif ( -l $path ) {
59     my $to = readlink $path;
60 dpavlin 2 header 2,$name;
61     html $to;
62 dpavlin 1 } else {
63 dpavlin 2 header 2, $name;
64     html file( $path );
65 dpavlin 1 }
66    
67     };
68    
69 dpavlin 2 $toc_html .= "</ul>" foreach ( 1 .. $last_level );
70 dpavlin 1
71     print qq|
72     <html><head>
73     <title>Sysadmin Cookbook</title>
74     <!--
75     <link type=text/css rel=stylesheet href="style.css">
76     -->
77     <style type=text/css>
78 dpavlin 2 pre.changes {
79     color: #444;
80     }
81     pre.content {
82     padding: 1em;
83 dpavlin 1 background: #eee;
84     }
85     </style>
86     </head><body>
87     |
88 dpavlin 2 . "<div class=toc>$toc_html</div>"
89 dpavlin 1 , join("\n", @html)
90     , "</body></html>"
91     ;
92    

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26