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