/[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 15 - (hide annotations)
Sat Feb 15 15:07:44 2014 UTC (10 years, 4 months ago) by dpavlin
File MIME type: text/plain
File size: 5440 byte(s)
display rss path
1 dpavlin 1 #!/usr/bin/perl
2    
3     use warnings;
4     use strict;
5    
6 dpavlin 9 my $svn = "http://svn.rot13.org/index.cgi/sysadmin-cookbook";
7 dpavlin 8 my $recepies = 'recepies/';
8 dpavlin 1
9     use File::Find;
10     use File::Slurp;
11 dpavlin 10 use File::Path;
12 dpavlin 2 use Data::Dump qw/dump/;
13 dpavlin 4 use XML::Simple;
14     use Regexp::Common qw /URI/;
15 dpavlin 9 use XML::FeedPP;
16 dpavlin 1
17     my @html;
18     sub html { push @html, @_ }
19    
20     my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
21     my $escape_re = join '|' => keys %escape;
22    
23     sub file {
24     my $path = shift;
25     my $content = read_file $path;
26     $content =~ s{[\n\r\s]+$}{}s;
27     $content =~ s/($escape_re)/$escape{$1}/gs;
28 dpavlin 4 $content =~ s[$RE{URI}{HTTP}{-keep}][<a href="$1">$1</a>]gs;
29    
30     my $log = XMLin( scalar `svn log --xml $path`,
31     ForceArray => [ 'logentry' ],
32     );
33     my $changes = join("\n",
34     map {
35     my $d = $_->{date};
36 dpavlin 7 $d =~ s{:\d\d\.\d+Z}{};
37 dpavlin 4 $d =~ s{T}{ };
38 dpavlin 7 my $r = $_->{revision};
39 dpavlin 9 qq|<li>$_->{msg} <a class="date" title="r$r" href="$svn/revision?rev=$r">$d</a></li>|
40 dpavlin 7 } reverse @{ $log->{logentry} }
41 dpavlin 4 );
42    
43     $path =~ s{^$recepies/*(.*?[^/]+)$}{$1} || next;
44 dpavlin 14 warn "## path = $path\n";
45 dpavlin 2 return ''
46 dpavlin 4 . qq|<ul class=changes>$changes</ul>|
47 dpavlin 14 . ( $path =~ m{(\.sh|Makefile|\.conf|\.pl)$}i || -x "$recepies/$path" ? qq|<a class="path" href="$svn/view/recepies/$path">$path</a>| : '' )
48 dpavlin 4 . qq|<pre class=content>$content</pre>|
49 dpavlin 2 ;
50 dpavlin 1 }
51    
52     my @names;
53     find({ follow => 0, no_chdir => 1, wanted => sub {
54 dpavlin 8 push @names, $_ unless m{/\.} || m{^\.};
55 dpavlin 1 }}, $recepies );
56    
57 dpavlin 14 warn "## names = ",dump( @names );
58    
59     our $last_level = 0;
60     our $toc_html = '';
61 dpavlin 2 sub header {
62 dpavlin 14 my ($level, $name, $full_name) = @_;
63     $full_name ||= $name;
64     $full_name =~ s{recepies/}{};
65     warn "## header $level $name [$full_name]\n";
66 dpavlin 11
67     my $display = $name;
68 dpavlin 3 $display =~ s{^\d+[\.-]}{};
69     $display =~ s{-}{ }g;
70 dpavlin 4 $display =~ s{\.\w+$}{};
71 dpavlin 11
72 dpavlin 14 my $anchor = $full_name;
73 dpavlin 11 $anchor =~ s{</?[^>]+>}{}g;
74     $anchor =~ s{\W+}{_}g;
75    
76     html qq|<a name=$anchor></a>|;
77 dpavlin 3 html qq|<h$level>$display</h$level>|;
78 dpavlin 2
79     if ( $last_level > $level ) {
80     $toc_html .= "</ul>";
81 dpavlin 14 warn "## $last_level > $level toc /ul";
82 dpavlin 2 } elsif ( $last_level < $level ) {
83     $toc_html .= "<ul>";
84 dpavlin 14 warn "## $last_level < $level toc ul";
85 dpavlin 2 }
86 dpavlin 11 $toc_html .= qq|<li><a href="#$anchor">$display</li>|;
87 dpavlin 2 $last_level = $level;
88     }
89    
90 dpavlin 4 my $to_path = '';
91 dpavlin 9 our @item;
92 dpavlin 4
93 dpavlin 10 sub mkfilepath {
94     my $path = shift;
95     $path =~ s{/[^/]+$}{};
96     mkpath $path unless -d $path;
97     }
98    
99 dpavlin 9 sub new_feed {
100     my $name = shift;
101     my $feed = XML::FeedPP::RSS->new();
102     $feed->title( "Sysadmin Cookbook" . ( $name ? " :: $name" : '' ) );
103     $feed->link( "http://sysadmin-cookbook.rot13.org/" . ( $name ? "#$name" : '' ) );
104     #$feed->pubDate( "Thu, 23 Feb 2006 14:43:43 +0900" );
105     return $feed;
106     }
107    
108     our $feed_all = new_feed;
109     sub add_item {
110     my $name = shift;
111     my $content = join("\n", @_);
112     return unless $name && $content;
113    
114     add_feed_item_description($feed_all, $name, "http://sysadmin-cookbook.rot13.org/rss/$name.xml", $content);
115    
116     my $item_feed = new_feed( $name );
117     add_feed_item_description($item_feed, $name, "http://sysadmin-cookbook.rot13.org/#$name", $content);
118 dpavlin 10 my $file = "rss/$name.xml";
119     mkfilepath $file;
120     $item_feed->to_file($file);
121 dpavlin 9
122 dpavlin 15 warn "# $name $file\n";
123 dpavlin 9 }
124    
125     sub add_feed_item_description {
126     my ( $feed, $name, $url, $description ) = @_;
127     my $item = $feed->add_item( $url );
128     $item->title( $name );
129     #$item->pubDate( "2006-02-23T14:43:43+09:00" );
130     $item->description( $description );
131     }
132    
133 dpavlin 1 foreach my $path ( sort @names ) {
134    
135 dpavlin 14 next if ( -d $path && -e "$path/.svn" );
136 dpavlin 8
137 dpavlin 1 my $name = $path;
138 dpavlin 10 # $name =~ s{^$recepies.*?([^/]+)$}{$1} || next;
139 dpavlin 4 $name =~ s{^$recepies.*?([^/]+)$}{$1} || next;
140 dpavlin 1
141 dpavlin 10 my @just_path = split m{/}, $path;
142     @just_path = splice @just_path, 1, -1;
143    
144 dpavlin 14 warn "## ?? $path";
145    
146 dpavlin 1 if ( -d $path ) {
147 dpavlin 9 add_item( splice(@item,0) );
148 dpavlin 10 my $h1 = join(' ',@just_path);
149     $h1 = qq|<span class="p">$h1</span> | if $h1;
150     $h1 .= $name;
151     header 1, $h1;
152 dpavlin 4 $to_path = '';
153 dpavlin 9 push @item, $name;
154 dpavlin 1 } elsif ( -l $path ) {
155 dpavlin 4 $to_path = " " . readlink $path;
156     next;
157 dpavlin 1 } else {
158 dpavlin 14 header 2, $name . $to_path, $path;
159 dpavlin 4 $to_path = '';
160 dpavlin 9 my $content = file $path;
161     html $content;
162     push @item, qq|<h4>$name</h4>\n$content|;
163 dpavlin 1 }
164    
165     };
166    
167 dpavlin 2 $toc_html .= "</ul>" foreach ( 1 .. $last_level );
168 dpavlin 1
169 dpavlin 9 $feed_all->to_file( "rss/index.xml" );
170    
171 dpavlin 1 print qq|
172     <html><head>
173     <title>Sysadmin Cookbook</title>
174     <!--
175     <link type=text/css rel=stylesheet href="style.css">
176     -->
177     <style type=text/css>
178 dpavlin 4
179     h1 {
180     background: #000;
181     color: #fff;
182     padding: 0.3em;
183     }
184    
185 dpavlin 10 h1 .p {
186     color: #888;
187     }
188    
189 dpavlin 4 .toc {
190     font-size: 80%;
191     }
192    
193 dpavlin 2 pre.changes {
194     color: #444;
195     }
196 dpavlin 4
197 dpavlin 2 pre.content {
198 dpavlin 5 padding: 0.5em;
199     margin: 1em;
200 dpavlin 1 background: #eee;
201     }
202 dpavlin 4
203     li .date {
204     font-family: monospace;
205     color: #888;
206     float: right;
207 dpavlin 7 margin-right: 1em;
208 dpavlin 4 }
209    
210 dpavlin 1 </style>
211     </head><body>
212 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>
213     <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>.
214 dpavlin 6 <br />
215 dpavlin 9 <small><a href="$svn">Source code repository</a></small>
216 dpavlin 1 |
217 dpavlin 2 . "<div class=toc>$toc_html</div>"
218 dpavlin 1 , join("\n", @html)
219     , "</body></html>"
220     ;
221    

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26