#!/usr/bin/perl -w
#
# Usage: regexopmltohtml.pl /home/superkuh/www/rss-feeds-2019.opml > feeds.html
#      ./regexopmltohtml.pl /home/superkuh/www/rss-feeds-2019.opml > test.html


use strict;
#use Data::Dumper;


my $opmlfiletoparse = shift;
my @categories;
my $list;

open( THEFILE, "<$opmlfiletoparse") or die "Can't open file $opmlfiletoparse $!";

# <outline text="Astronomy">
# <outline text="Acta Astronautica" type="rss" htmlUrl="https://www.sciencedirect.com/journal/acta-astronautica" xmlUrl="http://rss.sciencedirect.com/publication/science/00945765"/>
print qq[<div>];
while(<THEFILE>) {
	if ( m{<outline text=\"(.+)\">\n}sg) {
		my $name = $1;
		$list .= qq[<hr />];
		#print qq[<hr />];
		push(@categories,$name);
		#print qq[<a name="$name">];
		#print qq[<h3>$name</h3>];
		$list .= qq[<a name="$name">];
		$list .= qq[<h3>$name</h3>];
	} elsif ( m{<outline text=\"(.+)\" type=\".+?\" htmlUrl=\"(.+)?\" xmlUrl=\"(.+)\"/>}sg) {
		#print "1: $1, 2: $2, 3: $3\n" if $1;
		my($name,$htmlurl,$xmlurl) = ($1,$2,$3);
		$list .= qq[<p>];
		$list .= qq[<a href="$xmlurl"><img src="/rss.png" border="0" /></a>\n];
		$list .= qq[<a href="$htmlurl">$name</a></p>];
		#print qq[<p>];
		#print qq[<a href="$xmlurl"><img src="/rss.png" border="0" /></a>\n];
		#print qq[<a href="$htmlurl">$name</a></p>];
	}
}
print qq[<div style="float:right;"><ul>];
foreach my $category (@categories) {
	print qq[<li><a href="\#$category">$category</a></li>];
}
print qq[</ul></div>];

print $list;

print qq[</div>];



#    print qq[<p>];
#    print qq[<a href="$xmlurl"><img src="/rss.png" border="0" /></a>\n];
#    print qq[<a href="$htmlurl">$name</a></p>];

#my $file_content = do { local $/; <$fh> };
#while($file_content =~ m{<div><h3>(<a href="">)?(.*)(</a>)?</h3>}sg) {
#	$title = $1;	
#}