# This module actually generates the blog content

my $entry_path = "entries";

# This is the template to the comment box, to be appended directly at the
# end of each entry.  We're going to find the anchor from the file, 
# and replace the anchor with the name of the entry to be commented.
# (We need this to distinguish comments of different entries in comment.cgi)
my $comment_anchor = "COMMANCHOR";
my $comment_box_src = "src/comment_box.html";

@entries = <$entry_path/*>;

sub getBlogContent {
	my $content;

	$content .= getTOC();
	$content .= "<br/><hr/>\n";

	foreach my $entry (@entries) {
		$content .= getEntryContent($entry);
	}
	
	return $content;
}

sub getEntryContent {
	my $entry = $_[0];
	my $content;

	open(ENTRY_DESC, "<$entry/description") or die "No description file for entry $entry";
	open(CONTENT, "<$entry/content.html") or die "No content.html file for entry $entry";

	my $date = <ENTRY_DESC>;
	my $desc = <ENTRY_DESC>;
	chomp $date;
	chomp $desc;
	
	my $entry_name = (split("/", $entry))[1];
	$content .= "<a id=$entry_name></a>";
	$content .= "<h1>$date</h1>";
	$content .= "<h2>$desc</h2>\n";

	# We read in the content data
	while (<CONTENT>) { $content .= $_; }
	$content .= "\n";

	# Adding comments
	$content .= getCommentSection($entry);
}

sub getCommentSection {
	my $entry = $_[0];
	my $comment_section = "<h3>Comments</h3>\n";

	# Adding the actual comments, if comments file exists
	open(COMMENTS, "<$entry/comments") and $comment_section .= getComments(COMMENTS);

	# Adding the comment box
	$comment_section .= "<p>\n";
	open(COMMENT_BOX, "<$comment_box_src") or die "Couldn't load comment box src from $comment_box_src";
	while (<COMMENT_BOX>) { s/$comment_anchor/\"$entry\"/; $comment_section .= $_; }
	$comment_section .= "</p><br/><hr/>\n";

	return $comment_section;
}

sub getComments {
	my $FILE = $_[0];
	my $comments;

	my $fullfile;
	# Reading comment file contents to memory at once, but adding <br/> to newlines
	while (<$FILE>) { $fullfile .= $_; }

	# Fields are separated by " #"
	my @comment_array = split(/ #/, $fullfile);

	for (my $i = 0; $i < int(@comment_array/4); $i++) {
		my $date = $comment_array[$i*4 + 0];
		my $nick = $comment_array[$i*4 + 1];
		my $e_mail = $comment_array[$i*4 + 2];
		
		# Let's remove any whitespaces from beginning and end of these..
		$date =~ s/^\s*//g; $date =~ s/\s*$//g;
		$nick =~ s/^\s*//g; $nick =~ s/\s*$//g;
		$e_mail =~ s/^\s*//g; $e_mail =~ s/\s*$//g;

		# Cutting the message off at 8k characters in case it's spam
		my $message = substr($comment_array[$i*4 + 3], 0, 8192);

		# We're restoring all escaped #s
		$message =~ s/\\#/#/g;
		$nick =~ s/\\#/#/g;
		$e_mail =~ s/\\#/#/g;

		$comments .= "<h1><small>$date</small></h1><p>";
		$comments .= "<div id=blog_comment><pre wrap>\n$message</pre>";

		# Adding the commenter's nick/e-mail
		if (length $nick || length $e_mail) {
			my $name;
			if (length $nick) { $name = $nick; } 
			else { $name = $e_mail; }

			if (length $e_mail) {
				$comments .= "- <a href=\"mailto:$e_mail\">$name</a>";
			} else {
				$comments .= "- $name";
			}
			$comments .= "\n<br/>";
		}

		$comments .= "</div></p>\n";
	}

	return $comments;
}

sub getTOC {
	my $TOC = "<h1>Table of contents</h1>\n<p>\n";

	my $passive_entry = $_[0]; # Can be empty

	$TOC .= "<table>\n";
	foreach my $entry (@entries) {
		open(ENTRY_DESC, "<$entry/description") or die "No description file for entry $entry";
		my $date = <ENTRY_DESC>;
		my $desc = <ENTRY_DESC>;
		chomp $date;
		chomp $desc;

		my $entry_name = (split("/", $entry))[1];
		if ($entry eq $passive_entry) {
			$TOC .= "<tr><td>$date</td><td>$desc</td></tr>\n";
		} else {
			# Okay this is a kludge I added when I decided to include separate
			# entry pages also.  passive_entry should be set to something non-existant
			# when root TOC is generated in "separate entries" mode.
			# This way it knows not to make the links aliases, but rather separate htmls.
			if ($passive_entry) {
				$TOC .= "<tr><td>$date</td><td><a href=\"$entry_name.html\">$desc</a></td></tr>\n";
			} else {
				$TOC .= "<tr><td>$date</td><td><a href=\"#$entry_name\">$desc</a></td></tr>\n";
			}
		}
	}

	$TOC .= "</table>\n</p>\n";
	return $TOC;
}

return 1;
