/[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

Contents of /bin/html.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4 - (show annotations)
Sat May 16 21:44:27 2009 UTC (15 years ago) by dpavlin
File MIME type: text/plain
File size: 2548 byte(s)
better html design, make links clickable, files downloadable
and nice lists of changes

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 use Data::Dump qw/dump/;
11 use XML::Simple;
12 use Regexp::Common qw /URI/;
13
14 my @html;
15 sub html { push @html, @_ }
16
17 my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
18 my $escape_re = join '|' => keys %escape;
19
20 sub file {
21 my $path = shift;
22 my $content = read_file $path;
23 $content =~ s{[\n\r\s]+$}{}s;
24 $content =~ s/($escape_re)/$escape{$1}/gs;
25 $content =~ s[$RE{URI}{HTTP}{-keep}][<a href="$1">$1</a>]gs;
26
27 my $log = XMLin( scalar `svn log --xml $path`,
28 ForceArray => [ 'logentry' ],
29 );
30 my $changes = join("\n",
31 map {
32 my $d = $_->{date};
33 $d =~ s{\.\d+Z}{};
34 $d =~ s{T}{ };
35 qq|<li>$_->{msg} <span class="date">$d</span></li>|
36 } @{ $log->{logentry} }
37 );
38
39 $path =~ s{^$recepies/*(.*?[^/]+)$}{$1} || next;
40 return ''
41 . qq|<ul class=changes>$changes</ul>|
42 . ( $path =~ m{(\.sh|Makefile)$}i ? qq|<a class="path" href="recepies/$path">$path</a>| : '' )
43 . qq|<pre class=content>$content</pre>|
44 ;
45 }
46
47 my @names;
48 find({ follow => 0, no_chdir => 1, wanted => sub {
49 push @names, $_ unless m{/\.};
50 }}, $recepies );
51
52 my $last_level = 0;
53 my $toc_html = '';
54 sub header {
55 my ($level, $content) = @_;
56 my $display = $content;
57 $display =~ s{^\d+[\.-]}{};
58 $display =~ s{-}{ }g;
59 $display =~ s{\.\w+$}{};
60 $content =~ s{\W+}{_}g;
61 html qq|<a name=$content></a>|;
62 html qq|<h$level>$display</h$level>|;
63
64 if ( $last_level > $level ) {
65 $toc_html .= "</ul>";
66 } elsif ( $last_level < $level ) {
67 $toc_html .= "<ul>";
68 }
69 $toc_html .= qq|<li><a href="#$content">$display</li>|;
70 $last_level = $level;
71 }
72
73 my $to_path = '';
74
75 foreach my $path ( sort @names ) {
76
77 my $name = $path;
78 $name =~ s{^$recepies.*?([^/]+)$}{$1} || next;
79 next unless $name;
80
81 if ( -d $path ) {
82 header 1,$name;
83 $to_path = '';
84 } elsif ( -l $path ) {
85 $to_path = " " . readlink $path;
86 next;
87 } else {
88 header 2, $name . $to_path;
89 $to_path = '';
90 html file( $path );
91 }
92
93 };
94
95 $toc_html .= "</ul>" foreach ( 1 .. $last_level );
96
97 print qq|
98 <html><head>
99 <title>Sysadmin Cookbook</title>
100 <!--
101 <link type=text/css rel=stylesheet href="style.css">
102 -->
103 <style type=text/css>
104
105 h1 {
106 background: #000;
107 color: #fff;
108 padding: 0.3em;
109 }
110
111 .toc {
112 font-size: 80%;
113 }
114
115 pre.changes {
116 color: #444;
117 }
118
119 pre.content {
120 padding: 1em;
121 background: #eee;
122 }
123
124 li .date {
125 font-family: monospace;
126 color: #888;
127 float: right;
128 }
129
130 </style>
131 </head><body>
132 |
133 . "<div class=toc>$toc_html</div>"
134 , join("\n", @html)
135 , "</body></html>"
136 ;
137

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26