added thumbs
[sdlgit/SDL-Site.git] / tools / PM-Pod2html-snippet.pl
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5 use Carp;
6 use Pod::Xhtml;
7 use File::Copy;
8 use File::Spec::Functions qw(rel2abs splitpath splitdir catpath catdir catfile canonpath);
9
10 my $input_path      = 'C:/SDL_perl/lib/pods';
11    $input_path   = $ARGV[0] if $ARGV[0];
12
13 my ($volume, $dirs) = splitpath(rel2abs(__FILE__));
14 my @directories     = splitdir(canonpath($dirs));
15 pop(@directories);
16 my $parent_dir      = catpath($volume, catdir(@directories));
17 my $pages_path      = catdir($parent_dir, 'pages');
18 my $assets_path     = catdir($parent_dir, 'htdocs/assets');
19 my $parser          = Pod::Xhtml->new(FragmentOnly => 1, StringMode => 1);
20 my %module_names    = ();
21 my %thumbnails      = ();
22 my $fh;
23
24 read_file($input_path);
25
26 # creating index file
27 open($fh, '>', File::Spec->catfile($pages_path, 'documentation.html-inc'));
28 binmode($fh, ":utf8");
29 print($fh "<div class=\"pod\">\n<h1>Documentation (latest development branch)</h1><table>");
30 for my $module_name (sort keys %module_names)
31 {
32         my $icon = sprintf('<img src="assets/bubble-%d-mini.png" alt="thumb" />', int((rand() * 7) + 1));
33         my $name = $module_name;
34         my $desc = '';
35         
36         if($module_name =~ /^([^\-]+)\-(.+)$/)
37         {
38                 $name = $1;
39                 $desc = $2;
40         }
41         
42         if(defined $thumbnails{$module_name})
43         {
44                 $icon = sprintf('<img src="assets/%s" alt="thumb" />', $thumbnails{$module_name});
45         }
46         
47         printf($fh '<tr><td>%s</td><td><a href="%s">%s</a></td><td>%s</td></tr>', 
48                    $icon, $module_names{$module_name}, $name, $desc);
49 }
50 print($fh "</table></div>\n");
51 close($fh);
52
53 sub read_file
54 {
55         my $path = shift;
56         my @files      = <$path/*>;
57
58         foreach(@files)
59         {
60                 read_file($_) if(-d $_);
61                 if($_ =~ /\.pod$/i)
62                 {
63                         my $file_name   = $_;
64                            $file_name   =~ s/^$input_path\/*//;
65                         my $module_name = $file_name;
66                            $module_name =~ s/\//::/g;
67                            $module_name =~ s/(\.pm|\.pod)$//i;
68                            $file_name   =~ s/\//-/g;
69                            $file_name   =~ s/(\.pm|\.pod)$/.html-inc/i;
70                         my $file_path   = $file_name;
71                            $file_path   =~ s/\-inc$//;
72                            $file_name   = File::Spec->catfile($pages_path, $file_name);
73                         $parser->parse_from_file($_); #, $file_name);
74                         
75                         $module_name .= " - $1" if $parser->asString =~ /<div id="NAME_CONTENT">\s*<p>\s*[^<>\-]+\-([^<>]+)\s*<\/p>\s*<\/div>/;
76                         
77                         $module_names{$module_name} = $file_path;
78
79                         # handling images
80                         my $image_path  = $_;
81                            $image_path  =~ s/\.pod$//;
82                         my @images = <$image_path*>;
83                         
84                         my $image_html = '';
85                         
86                         foreach my $image_file (@images)
87                         {
88                                 if($image_file =~ /^($image_path)(_\w+){0,1}\.(jpg|jpeg|png|gif)$/)
89                                 {
90                                         my (undef, undef, $image_file_name) = splitpath($image_file);
91                                         
92                                         if($image_file_name =~ /_thumb\.(jpg|jpeg|png|gif)$/)
93                                         {
94                                                 $thumbnails{$module_name} = $image_file_name;
95                                         }
96                                         else
97                                         {
98                                                 $image_html .= sprintf('<a href="assets/%s" target="_blank">'
99                                                                          . '<img src="assets/%s" style="height: 160px" alt="%s"/>'
100                                                                      . '</a>', $image_file_name, $image_file_name, $image_file_name);
101                                         }
102                                                                                 
103                                         copy($image_file, File::Spec->catfile($assets_path, $image_file_name));
104                                 }
105                         }
106                         
107                         # modifying the html-snippet and insert the images
108                         my $html = $parser->asString;
109                            $html =~ s/<!-- INDEX END -->/<!-- INDEX END -->$image_html<hr \/>/ if $image_html;
110                         
111                         open($fh, '>', $file_name);
112                         binmode($fh, ":utf8");
113                         print($fh $html);
114                         close($fh);
115                 }
116         }
117 }