/[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 5 - (hide annotations)
Sat May 16 22:19:40 2009 UTC (14 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 3275 byte(s)
added Creative Commons Attribution-Noncommercial-Share Alike 3.0 Croatia License

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 4 use XML::Simple;
12     use Regexp::Common qw /URI/;
13 dpavlin 1
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 dpavlin 4 $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 dpavlin 2 return ''
41 dpavlin 4 . 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 dpavlin 2 ;
45 dpavlin 1 }
46    
47     my @names;
48     find({ follow => 0, no_chdir => 1, wanted => sub {
49     push @names, $_ unless m{/\.};
50     }}, $recepies );
51    
52 dpavlin 2 my $last_level = 0;
53     my $toc_html = '';
54     sub header {
55     my ($level, $content) = @_;
56 dpavlin 3 my $display = $content;
57     $display =~ s{^\d+[\.-]}{};
58     $display =~ s{-}{ }g;
59 dpavlin 4 $display =~ s{\.\w+$}{};
60     $content =~ s{\W+}{_}g;
61 dpavlin 2 html qq|<a name=$content></a>|;
62 dpavlin 3 html qq|<h$level>$display</h$level>|;
63 dpavlin 2
64     if ( $last_level > $level ) {
65     $toc_html .= "</ul>";
66     } elsif ( $last_level < $level ) {
67     $toc_html .= "<ul>";
68     }
69 dpavlin 3 $toc_html .= qq|<li><a href="#$content">$display</li>|;
70 dpavlin 2 $last_level = $level;
71     }
72    
73 dpavlin 4 my $to_path = '';
74    
75 dpavlin 1 foreach my $path ( sort @names ) {
76    
77     my $name = $path;
78 dpavlin 4 $name =~ s{^$recepies.*?([^/]+)$}{$1} || next;
79 dpavlin 2 next unless $name;
80 dpavlin 1
81     if ( -d $path ) {
82 dpavlin 2 header 1,$name;
83 dpavlin 4 $to_path = '';
84 dpavlin 1 } elsif ( -l $path ) {
85 dpavlin 4 $to_path = " " . readlink $path;
86     next;
87 dpavlin 1 } else {
88 dpavlin 4 header 2, $name . $to_path;
89     $to_path = '';
90 dpavlin 2 html file( $path );
91 dpavlin 1 }
92    
93     };
94    
95 dpavlin 2 $toc_html .= "</ul>" foreach ( 1 .. $last_level );
96 dpavlin 1
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 dpavlin 4
105     h1 {
106     background: #000;
107     color: #fff;
108     padding: 0.3em;
109     }
110    
111     .toc {
112     font-size: 80%;
113     }
114    
115 dpavlin 2 pre.changes {
116     color: #444;
117     }
118 dpavlin 4
119 dpavlin 2 pre.content {
120 dpavlin 5 padding: 0.5em;
121     margin: 1em;
122 dpavlin 1 background: #eee;
123     }
124 dpavlin 4
125     li .date {
126     font-family: monospace;
127     color: #888;
128     float: right;
129     }
130    
131 dpavlin 1 </style>
132     </head><body>
133 dpavlin 5 <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 dpavlin 1 |
136 dpavlin 2 . "<div class=toc>$toc_html</div>"
137 dpavlin 1 , join("\n", @html)
138     , "</body></html>"
139     ;
140    

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26