added images inside pod docs
[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 $fh;
22
23 read_file($input_path);
24
25 # creating index file
26 open($fh, '>', File::Spec->catfile($pages_path, 'documentation.html-inc'));
27 binmode($fh, ":utf8");
28 print($fh "<div class=\"pod\">\n<h1>Documentation (latest development branch)</h1>");
29 for my $module_name (sort keys %module_names)
30 {
31         print($fh '<a href="' . $module_names{$module_name} . '">',
32                   $module_name, 
33                   '</a><br />'
34         );
35 }
36 print($fh "</div>\n");
37 close($fh);
38
39 sub read_file
40 {
41         my $path = shift;
42         my @files      = <$path/*>;
43
44         foreach(@files)
45         {
46                 read_file($_) if(-d $_);
47                 if($_ =~ /\.pod$/i)
48                 {
49                         my $image_path  = $_;
50                            $image_path  =~ s/\.pod$//;
51                         my @images = <$image_path*>;
52                         
53                         my $image_html = '';
54                         
55                         foreach my $image_file (@images)
56                         {
57                                 if($image_file =~ /^($image_path)(_\d+){0,1}\.(jpg|jpeg|png|gif)$/)
58                                 {
59                                         my (undef, undef, $image_file_name) = splitpath($image_file);
60                                         
61                                         $image_html .= sprintf('<a href="assets/%s" target="_blank">'
62                                                                  . '<img src="assets/%s" style="height: 160px" alt="%s"/>'
63                                                              . '</a>', $image_file_name, $image_file_name, $image_file_name);
64                                         
65                                         copy($image_file, File::Spec->catfile($assets_path, $image_file_name));
66                                 }
67                         }
68                         
69                         my $file_name   = $_;
70                            $file_name   =~ s/^$input_path\/*//;
71                         my $module_name = $file_name;
72                            $module_name =~ s/\//::/g;
73                            $module_name =~ s/(\.pm|\.pod)$//i;
74                            $file_name   =~ s/\//-/g;
75                            $file_name   =~ s/(\.pm|\.pod)$/.html-inc/i;
76                         my $file_path   = $file_name;
77                            $file_path   =~ s/\-inc$//;
78                         $module_names{$module_name} = $file_path;
79                            $file_name   = File::Spec->catfile($pages_path, $file_name);
80
81                         $parser->parse_from_file($_); #, $file_name);
82                         
83                         # modifying the html-snippet and insert the images
84                         my $html = $parser->asString;
85                            $html =~ s/<!-- INDEX END -->/<!-- INDEX END -->$image_html<hr \/>/ if $image_html;
86                         
87                         open($fh, '>', $file_name);
88                         binmode($fh, ":utf8");
89                         print($fh $html);
90                         close($fh);
91                 }
92         }
93 }