docs for pango done
[sdlgit/SDL-Site.git] / tools / RSS2html-snippet.pl
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5 use utf8;
6
7 use File::Spec::Functions qw(rel2abs splitpath splitdir catpath catdir catfile canonpath);
8 use XML::Feed;
9
10 my $feed            = XML::Feed->parse(URI->new('http://yapgh.blogspot.com/feeds/posts/default?alt=rss'))
11                       or die XML::Feed->errstr;
12
13 my ($volume, $dirs) = splitpath(rel2abs(__FILE__));
14
15 my @directories     = splitdir(canonpath($dirs));
16 pop(@directories);
17 my $parent_dir      = catpath($volume, catdir(@directories));
18 my $output_path     = catdir($parent_dir, 'pages');
19
20 die("Error: Output path '$output_path' not found.") unless(-d $output_path);
21
22 my $fh;
23 my %available_tags  = (); # tags to filenames
24 my %tag_overview    = (); # tags to short content
25
26 printf("path for placing files is: %s\n", $output_path);
27
28 my $i = 1;
29 for my $entry ($feed->entries)
30 {
31     my $output_file = sprintf("blog-%04d.html-inc", $i);
32     my @tags        = $entry->tags;
33         
34     foreach my $tag (sort @tags)
35     {
36         @{$available_tags{$tag}} = () unless defined ($available_tags{$tag});
37         push(@{$available_tags{$tag}}, $output_file);
38     }
39
40         open($fh, '>', catfile($output_path, $output_file));
41         binmode($fh, ":utf8");
42         print($fh "<div class=\"blog\">\n<h1 id=\"NAME\">\n",
43                   $entry->title, 
44                   "\n</h1>\n<div class=\"CONTENT\">\n", 
45                   $entry->content->body, 
46                           "</div>",
47                   "</div>"
48         );
49         close($fh);
50         
51         printf("created file: %s\n", $output_file);
52
53         $i++;
54 }
55
56 open($fh, '>', catfile($output_path, 'blog-0000.html-inc'));
57 binmode($fh, ":utf8");
58 print($fh "<div class=\"blog\"><h1>Articles</h1>\n");
59 $i = 1;
60 for my $entry ($feed->entries)
61 {
62         my $tag_links = '';
63     my @tags      = $entry->tags;
64     foreach my $tag (sort @tags)
65     {
66                 my $_tag   = $tag;
67                    $_tag   =~ s/\W/-/g;
68         $tag_links .= sprintf(' <a href="tags-%s.html" style="font-size: 10px">[%s]</a>', $_tag, $tag);
69     }
70
71         my $text = $entry->content->body;
72            $text = $1 if $text =~ /^<div.+<\/div>(.+)<div.+<\/div>$/;
73            $text =~ s/<br\s*\/{0,1}>/\n/g;
74            $text =~ s/<[@#%\w\s"\/?&=:\-\.;']+>/ /g;
75            $text =~ s/^\n*//g;
76            $text =~ s/\n*$//g;
77            $text =~ s/\n+/\n/g;
78            $text =~ s/\n/<br \/>/g;
79            $text = $1 if $text =~ /^([^<>]+<br \/>[^<>]+<br \/>[^<>]+<br \/>).*$/;
80            $text =~ s/(<br \/>)+$//g;
81            
82         # overview of all blog entries
83         printf($fh '<div>'
84                      . '<a href="blog-%04d.html">%s</a><br />'
85                      . '<span style="font-size: 10px">%s</span><br />'
86                      . '<span style="font-size: 10px">Tags:</span>%s<br />'
87                              . '%s<br /><a href="blog-%04d.html" style="font-size: 12px">[more]</a><br /><br />'
88                          . '</div>'
89                          . '<hr />',
90                 $i, $entry->title, $entry->issued->strftime('%A, %d %B %Y'), $tag_links, $text, $i
91         );
92
93         # preparing the %tag_overview hash for tag-overview-pages
94     @tags      = $entry->tags;
95     foreach my $tag (sort @tags)
96     {
97         @{$tag_overview{$tag}} = () unless defined ($tag_overview{$tag});
98         push(@{$tag_overview{$tag}}, 
99                         sprintf('<div>'
100                                          . '<a href="blog-%04d.html">%s</a><br />'
101                                          . '<span style="font-size: 10px">%s</span><br />'
102                                          . '<span style="font-size: 10px">Tags: %s</span><br />'
103                                          . '%s<br /><a href="blog-%04d.html" style="font-size: 12px">[more]</a><br /><br />'
104                                  . '</div>',
105                         $i, $entry->title, $entry->issued->strftime('%A, %d %B %Y'), $tag_links, $text, $i
106                 ));
107     }
108
109         $i++;
110 }
111 print($fh "</div>\n");
112 close($fh);
113 printf("created file: %s\n", 'blog-0000.html-inc');
114
115
116 # csv: "tagname: file1,file2\n"
117 open($fh, '>', catfile($output_path, 'tags-index'));
118 binmode($fh, ":utf8");
119 foreach my $tag (sort keys %available_tags)
120 {
121         printf($fh "%s: %s\n", $tag, join(',', @{$available_tags{$tag}}));
122 }
123 close($fh);
124 printf("created file: %s\n", 'tags-index');
125
126 # overview pages for tags
127 foreach my $tag (sort keys %tag_overview)
128 {
129         my $_tag = $tag;
130        $_tag =~ s/\W/-/g;
131         open($fh, '>', catfile($output_path, 'tags-' . $_tag . '.html-inc'));
132         binmode($fh, ":utf8");
133         print($fh '<div class="blog"><h1>Results for tag: ' . $tag . '</h1>' 
134                 . join('<hr />', @{$tag_overview{$tag}})
135                         . '</div>');
136         close($fh);
137         printf("created file: %s\n", 'tags-' . $_tag . '.html-inc');
138 }
139