docs for audio
[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
b82df135 41 if($#matches)
42 {
43 my $section_path = '';
44 my $doit = 1;
45
46 for my $section (@matches)
47 {
48 last if $section eq $matches[$#matches];
49
50 $section_path .= (length($section_path) ? ', ' : '') . $section;
51
52 if($last_section =~ /^$section_path/)
53 {
54 $doit = 0;
55 }
56 }
57
58 if($doit)
59 {
60 my @this_matches = ( $section_path =~ m/\w+/xg );
61 my $i = 0;
62 for my $this_section (@this_matches)
63 {
64 printf($fh '<table style="margin-left: %dpx; margin-top: 5px"><tr><td colspan="3"><strong style="font-size: 14px">%s</strong></td></tr>',
65 $i++ * 30, $this_section);
66 }
67 }
68 }
69
bfdd9c2e 70 if($last_section ne $files{$key}{'section'})
9f2a2b91 71 {
bfdd9c2e 72 print ($fh '</table>') if $last_section;
73 print ($fh '<br />') if $last_section && !$#matches;
33a96b1f 74 printf($fh '<table style="margin-left: %dpx; margin-top: 5px"><tr><td colspan="3"><strong style="font-size: 14px">%s</strong></td></tr>',
bfdd9c2e 75 $#matches * 30, pop(@matches));
76 $last_section = $files{$key}{'section'};
9f2a2b91 77 }
78
79 printf($fh '<tr><td>%s</td><td><a href="%s">%s</a></td><td>%s</td></tr>',
bfdd9c2e 80 $icon, $files{$key}{'path'}, $files{$key}{'name'}, $files{$key}{'desc'});
b3ef54ec 81}
9f2a2b91 82print($fh "</table></div>\n");
b3ef54ec 83close($fh);
84
85sub read_file
86{
87 my $path = shift;
88 my @files = <$path/*>;
89
90 foreach(@files)
91 {
92 read_file($_) if(-d $_);
b3ef54ec 93 if($_ =~ /\.pod$/i)
94 {
bfdd9c2e 95 my $key = '';
9f2a2b91 96 my $file_name = $_;
97 $file_name =~ s/^$input_path\/*//;
98 my $module_name = $file_name;
99 $module_name =~ s/\//::/g;
100 $module_name =~ s/(\.pm|\.pod)$//i;
101 $file_name =~ s/\//-/g;
102 $file_name =~ s/(\.pm|\.pod)$/.html-inc/i;
103 my $file_path = $file_name;
104 $file_path =~ s/\-inc$//;
105 $file_name = File::Spec->catfile($pages_path, $file_name);
106 $parser->parse_from_file($_); #, $file_name);
107
9f2a2b91 108
bfdd9c2e 109
110 $key = $parser->asString =~ /<div id="CATEGORY_CONTENT">\s*<p>\s*([^<>]+)\s*<\/p>\s*<\/div>/
111 ? "$1 $_"
112 : "UNCATEGORIZED/$_";
60f74f6f 113 $key = " $key" if $key =~ /^Core/;
bfdd9c2e 114 $files{$key}{'path'} = $file_path;
115 $files{$key}{'name'} = $module_name;
116 $files{$key}{'desc'} = $parser->asString =~ /<div id="NAME_CONTENT">\s*<p>\s*[^<>\-]+\-([^<>]+)\s*<\/p>\s*<\/div>/
117 ? $1
118 : '';
119 $files{$key}{'section'} = $parser->asString =~ /<div id="CATEGORY_CONTENT">\s*<p>\s*([^<>]+)\s*<\/p>\s*<\/div>/
120 ? $1
121 : 'UNCATEGORIZED';
9f2a2b91 122
123 # handling images
cbc85b7f 124 my $image_path = $_;
125 $image_path =~ s/\.pod$//;
126 my @images = <$image_path*>;
127
128 my $image_html = '';
129
130 foreach my $image_file (@images)
131 {
9f2a2b91 132 if($image_file =~ /^($image_path)(_\w+){0,1}\.(jpg|jpeg|png|gif)$/)
cbc85b7f 133 {
134 my (undef, undef, $image_file_name) = splitpath($image_file);
135
9f2a2b91 136 if($image_file_name =~ /_thumb\.(jpg|jpeg|png|gif)$/)
137 {
bfdd9c2e 138 $files{$key}{'thumb'} = $image_file_name;
9f2a2b91 139 }
140 else
141 {
142 $image_html .= sprintf('<a href="assets/%s" target="_blank">'
143 . '<img src="assets/%s" style="height: 160px" alt="%s"/>'
144 . '</a>', $image_file_name, $image_file_name, $image_file_name);
145 }
146
cbc85b7f 147 copy($image_file, File::Spec->catfile($assets_path, $image_file_name));
148 }
149 }
150
cbc85b7f 151 # modifying the html-snippet and insert the images
152 my $html = $parser->asString;
153 $html =~ s/<!-- INDEX END -->/<!-- INDEX END -->$image_html<hr \/>/ if $image_html;
154
155 open($fh, '>', $file_name);
156 binmode($fh, ":utf8");
157 print($fh $html);
158 close($fh);
b3ef54ec 159 }
160 }
161}