/[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 7 - (show annotations)
Mon May 18 19:15:05 2009 UTC (15 years, 1 month ago) by dpavlin
File MIME type: text/plain
File size: 3524 byte(s)
make dates link to correct revision

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\d\.\d+Z}{};
34 $d =~ s{T}{ };
35 my $r = $_->{revision};
36 qq|<li>$_->{msg} <a class="date" title="r$r" href="http://svn.rot13.org/index.cgi/sysadmin-cookbook/revision?rev=$r">$d</a></li>|
37 } reverse @{ $log->{logentry} }
38 );
39
40 $path =~ s{^$recepies/*(.*?[^/]+)$}{$1} || next;
41 return ''
42 . qq|<ul class=changes>$changes</ul>|
43 . ( $path =~ m{(\.sh|Makefile)$}i ? qq|<a class="path" href="recepies/$path">$path</a>| : '' )
44 . qq|<pre class=content>$content</pre>|
45 ;
46 }
47
48 my @names;
49 find({ follow => 0, no_chdir => 1, wanted => sub {
50 push @names, $_ unless m{/\.};
51 }}, $recepies );
52
53 my $last_level = 0;
54 my $toc_html = '';
55 sub header {
56 my ($level, $content) = @_;
57 my $display = $content;
58 $display =~ s{^\d+[\.-]}{};
59 $display =~ s{-}{ }g;
60 $display =~ s{\.\w+$}{};
61 $content =~ s{\W+}{_}g;
62 html qq|<a name=$content></a>|;
63 html qq|<h$level>$display</h$level>|;
64
65 if ( $last_level > $level ) {
66 $toc_html .= "</ul>";
67 } elsif ( $last_level < $level ) {
68 $toc_html .= "<ul>";
69 }
70 $toc_html .= qq|<li><a href="#$content">$display</li>|;
71 $last_level = $level;
72 }
73
74 my $to_path = '';
75
76 foreach my $path ( sort @names ) {
77
78 my $name = $path;
79 $name =~ s{^$recepies.*?([^/]+)$}{$1} || next;
80 next unless $name;
81
82 if ( -d $path ) {
83 header 1,$name;
84 $to_path = '';
85 } elsif ( -l $path ) {
86 $to_path = " " . readlink $path;
87 next;
88 } else {
89 header 2, $name . $to_path;
90 $to_path = '';
91 html file( $path );
92 }
93
94 };
95
96 $toc_html .= "</ul>" foreach ( 1 .. $last_level );
97
98 print qq|
99 <html><head>
100 <title>Sysadmin Cookbook</title>
101 <!--
102 <link type=text/css rel=stylesheet href="style.css">
103 -->
104 <style type=text/css>
105
106 h1 {
107 background: #000;
108 color: #fff;
109 padding: 0.3em;
110 }
111
112 .toc {
113 font-size: 80%;
114 }
115
116 pre.changes {
117 color: #444;
118 }
119
120 pre.content {
121 padding: 0.5em;
122 margin: 1em;
123 background: #eee;
124 }
125
126 li .date {
127 font-family: monospace;
128 color: #888;
129 float: right;
130 margin-right: 1em;
131 }
132
133 </style>
134 </head><body>
135 <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>
136 <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>.
137 <br />
138 <small><a href="http://svn.rot13.org/index.cgi/sysadmin-cookbook/">Source code repository</a></small>
139 |
140 . "<div class=toc>$toc_html</div>"
141 , join("\n", @html)
142 , "</body></html>"
143 ;
144

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26