forgot page
[sdlgit/SDL-Site.git] / tools / RSS2html-snippet.pl
CommitLineData
b3ef54ec 1#!/usr/bin/env perl
2
3use strict;
4use warnings;
5use utf8;
6
7use File::Spec::Functions qw(rel2abs splitpath splitdir catpath catdir catfile canonpath);
8use XML::Feed;
9
10my $feed = XML::Feed->parse(URI->new('http://yapgh.blogspot.com/feeds/posts/default?alt=rss'))
11 or die XML::Feed->errstr;
12
13my ($volume, $dirs) = splitpath(rel2abs(__FILE__));
14
15my @directories = splitdir(canonpath($dirs));
16pop(@directories);
17my $parent_dir = catpath($volume, catdir(@directories));
18my $output_path = catdir($parent_dir, 'pages');
19
20die("Error: Output path '$output_path' not found.") unless(-d $output_path);
21
22my $fh;
23my %available_tags = (); # tags to filenames
24my %tag_overview = (); # tags to short content
25
26printf("path for placing files is: %s\n", $output_path);
27
28my $i = 1;
29for 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
56open($fh, '>', catfile($output_path, 'blog-0000.html-inc'));
57binmode($fh, ":utf8");
58print($fh "<div class=\"blog\"><h1>Articles</h1>\n");
59$i = 1;
60for 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}
111print($fh "</div>\n");
112close($fh);
113printf("created file: %s\n", 'blog-0000.html-inc');
114
115
116# csv: "tagname: file1,file2\n"
117open($fh, '>', catfile($output_path, 'tags-index'));
118binmode($fh, ":utf8");
119foreach my $tag (sort keys %available_tags)
120{
121 printf($fh "%s: %s\n", $tag, join(',', @{$available_tags{$tag}}));
122}
123close($fh);
124printf("created file: %s\n", 'tags-index');
125
126# overview pages for tags
127foreach 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