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

Diff of /bin/html.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.2  
changed lines
  Added in v.10

  ViewVC Help
Powered by ViewVC 1.1.26