#!/usr/bin/perl
# use cPanelUserConfig; #for cpanel servers

use 5.010;
use strict;
use warnings;
use File::Slurp; # https://metacpan.org/pod/File::Slurp
use JSON;

###################################
#
# Daily Blackout Poetry Challenge
# 
# This script supports the Daily Blackout Poetry Challenge
# it takes in text files of books and converts them into JSON 
# splitting them up into "pages" for easier display 
# 
# license GPLv3.0 https://www.gnu.org/licenses/gpl-3.0.en.html
# Code repository: 
# 
# Written by Jacob Haddon https://jacobhaddon.com
# 
# This uses File::Slurp - https://metacpan.org/pod/File::Slurp
# Texts are from Project Gutenburg - https://www.gutenberg.org/
#
###################################
 
my $json = new JSON;
 
my %books = (
  title => "Wuthering Heights",
  author  => "Emily Bronte",
  url  => "https://www.gutenberg.org/ebooks/768",
  file => "books/wutheringheights.txt"
); # books hash 

# print($books{'file'});

 
# read in a whole file into a scalar
my $book = read_file($books{'file'});

my @chapters = split('\nCHAPTER', $book);
my $test = @chapters;
print($test);

# number of lines in a page
my $number_of_lines  = 31;

my $j = 0;
my @page_text;
foreach (@chapters) {
# 	print($j);
	my @chapter_lines = split(/\r\n/, $chapters[$j]);
	if ($j != 0){ 
		$chapter_lines[0] = "<h1>CHAPTER" . $chapter_lines[0] . "</h1>\n\n<p>";
	}

	my $test2 = @chapter_lines;
# 	print ($test2);
	my $i = 0;
	my $line_number = $number_of_lines * $i;
	
	while($line_number < $test2) {
# 		print($line_number);
# 		print($i);
	
		# print($chapter_lines[6]);
		my @page = splice(@chapter_lines, 0, $number_of_lines);
		# print($page[22]);
		
		$page[0] = "<p>" . $page[0];

		foreach (@page) {

			if ($_ eq "") {
				$_ = "</p>\n\n<p>";
			}
		} # foreach page

		$page_text[$j][$i] = join(" ", @page);
		$page_text[$j][$i]  = $page_text[$j][$i] . "</p>\n\n";
		

		$i++;
		$line_number = $number_of_lines * $i;
		
	} # while line number

		$j++;

# 	print($i);
# 	$i++;
# 	my @chapter_lines = split(/[\n|\r]/, $_);
# 	print($chapter_lines[0]);


} # foreach chapters

$books{'pages'} = \@page_text;

my @tst_page = @{$books{'pages'} };

# print($tst_page[12][0]);

# my $json_text = encode_json(\%books);
my $json_text = $json->pretty->encode( \%books );

my $js_text = "let book = " . $json_text;

# write the file 
open(FH, '>', "test3.js") or die $!;
print FH $js_text;
close(FH);

# 	print($page_text[12][0]);

# print($books{'pages'});

# print($chapters[1]);
# my @chapter_lines = split(/\r\n/, $chapters[1]);
# $chapter_lines[0] = "<h1>Chapter" . $chapter_lines[0] . "</h1>\n\n<p>";


# my $test2 = @chapter_lines;
# print ($test2);
# 
# my $line_number = 31 * $i;
# 
# while($line_number < $test2) {
# 	print($line_number);
# 	print($i);
# 	
# 	# print($chapter_lines[6]);
# 	my @page = splice(@chapter_lines, 0, 31);
# 	# print($page[22]);
# 
# 	foreach (@page) {
# 		if ($_ eq "") {
# 			$_ = "</p>\n\n<p>";
# 		}
# 	} # foreach page
# 
# 	$page_text[$i] = join("", @page);
# 	$page_text[$i]  = $page_text[$i] . "</p>\n\n";
# 	
# 	$i++;
# 	$line_number = 31 * $i;
# 		
# }
# 
# print($page_text[1]);