/[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 6 - (show annotations)
Sun May 17 12:33:51 2009 UTC (15 years ago) by dpavlin
File MIME type: text/plain
File size: 3386 byte(s)
added link to source repository

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: 0.5em;
121 margin: 1em;
122 background: #eee;
123 }
124
125 li .date {
126 font-family: monospace;
127 color: #888;
128 float: right;
129 }
130
131 </style>
132 </head><body>
133 <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>
134 <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>.
135 <br />
136 <small><a href="http://svn.rot13.org/index.cgi/sysadmin-cookbook/">Source code repository</a></small>
137 |
138 . "<div class=toc>$toc_html</div>"
139 , join("\n", @html)
140 , "</body></html>"
141 ;
142

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26