cleaned up the code of books.pl; let's not talk about the previous code

This commit is contained in:
Jacob Haddon 2024-03-17 20:37:57 -04:00
parent 697aff0b8d
commit 6a2177739a

184
books.pl
View File

@ -37,10 +37,8 @@ my $book_pages_js = "book_pages2.js";
# number of lines in a page # number of lines in a page
my $number_of_lines = 31; my $number_of_lines = 31;
my $json = new JSON; # how many pages do you want made?
my $number_of_pages = 365;
# my $date = localtime->strftime('%Y-%m-%d');
# print $date;
my @books = ( my @books = (
{ {
@ -66,92 +64,86 @@ my @books = (
} }
); # books hash ); # books hash
# print($books[1]{'file'}); my $json = new JSON;
# print(int(rand($for))); ###################################
# Make the books into pages
###################################
my $k = 0; my $k = 0;
foreach (@books) { foreach (@books) {
# print $books[$k]{'title'};
# read in a whole file into a scalar # read in a whole file into a scalar
my $book = read_file($books[$k]{'file'}); my $book = read_file($books[$k]{'file'});
# split the book into chapters by the appropriate marker
my @chapters = split('\n' . $books[$k]{'chapter_marker'}, $book); my @chapters = split('\n' . $books[$k]{'chapter_marker'}, $book);
my $test = @chapters;
# print($test);
# go through each chapter and make pages based on number of desired lines
my $j = 0; my $j = 0;
my @page_text; my @page_text;
foreach (@chapters) { foreach (@chapters) {
# print($j);
my @chapter_lines = split(/\r\n/, $chapters[$j]); my @chapter_lines = split(/\r\n/, $chapters[$j]);
if ($j != 0){ if ($j != 0){
$chapter_lines[0] = "<h1>CHAPTER" . $chapter_lines[0] . "</h1>\n\n<p>"; $chapter_lines[0] = "<h1>CHAPTER" . $chapter_lines[0] . "</h1>\n\n<p>";
} } # if j != 0
# how many lines are in the whole chapter
my $chapter_length = @chapter_lines;
my $test2 = @chapter_lines;
# print ($test2);
my $i = 0; my $i = 0;
# this is the starting place for the splice
my $line_number = $number_of_lines * $i; my $line_number = $number_of_lines * $i;
while($line_number < $test2) { # while the line number is still less than the number of lines in the chapter; make a page
# print($line_number); while($line_number < $chapter_length) {
# print($i);
# print($chapter_lines[6]);
my @page = splice(@chapter_lines, 0, $number_of_lines); my @page = splice(@chapter_lines, 0, $number_of_lines);
# print($page[22]);
# add a starting paragraph marker for the first page
$page[0] = "<p>" . $page[0]; $page[0] = "<p>" . $page[0];
# ad in blank space
foreach (@page) { foreach (@page) {
if ($_ eq "") { if ($_ eq "") {
$_ = "</p>\n\n<p>"; $_ = "</p>\n\n<p>";
} }
} # foreach page } # foreach page
# join the pages by a space into a string
$page_text[$j][$i] = join(" ", @page); $page_text[$j][$i] = join(" ", @page);
# Add trailing paragraph marker
$page_text[$j][$i] = $page_text[$j][$i] . "</p>\n\n"; $page_text[$j][$i] = $page_text[$j][$i] . "</p>\n\n";
#iterate
$i++; $i++;
$line_number = $number_of_lines * $i; $line_number = $number_of_lines * $i;
} # while line number } # while line number
# iterate
$j++; $j++;
# print($i);
# $i++;
# my @chapter_lines = split(/[\n|\r]/, $_);
# print($chapter_lines[0]);
} # foreach chapters } # foreach chapters
$books[$k]{'pages'} = \@page_text; $books[$k]{'pages'} = \@page_text;
# iterate
$k++; $k++;
} # foreach books } # foreach books
# my @tst_page = @{$books[$k]{'pages'} }; ###################################
# Make a complete_books_js file
###################################
# print($tst_page[12][0]); #turn the books variable into a JSON string
# my $json_text = encode_json(\%books);
my $json_text = $json->pretty->encode( \@books ); my $json_text = $json->pretty->encode( \@books );
# make the JSON formatted text into a Javascript object
my $js_text = "let books = " . $json_text; my $js_text = "let books = " . $json_text;
# write the file # write the file
@ -159,71 +151,47 @@ open(FH, '>', $complete_books_js) or die $!;
print FH $js_text; print FH $js_text;
close(FH); close(FH);
###################################
# make a list of 365 pages # make a list of 365 pages
###################################
my $for = @books; my $for = @books;
my @page_list; my %page_list;
my %page_list2;
my $dt = DateTime->now(); my $dt = DateTime->now();
# print $dt->ymd . "\n";
for ( my $l; $l < 365; $l++){ for ( my $l; $l < $number_of_pages; $l++){
# print $l; # get a random book
# $page_list[$l] = $l;
my $book_list = int(rand($for)); my $book_list = int(rand($for));
# print $book_list;
# my %book1 = $books[$book_list]; # get the number of chapters in the book
# print $books[$book_list]{'title'};
my $chapter_list = @{$books[$book_list]{'pages'}}; my $chapter_list = @{$books[$book_list]{'pages'}};
# print $chapter_list; # pull out a random chapter from that book
my $chapter_select = int(rand($chapter_list)); my $chapter_select = int(rand($chapter_list));
# print $chapter_select;
# get the number of pages in the chapter
my $page_list = @{$books[$book_list]{'pages'}[$chapter_select]}; my $page_list = @{$books[$book_list]{'pages'}[$chapter_select]};
# print $page_list; # pull out a random page from the chapter
my $page_select = int(rand($page_list)); my $page_select = int(rand($page_list));
# get the text of that page
my $page_rand = $books[$book_list]{'pages'}[$chapter_select][$page_select]; my $page_rand = $books[$book_list]{'pages'}[$chapter_select][$page_select];
# print $page_rand;
# my $page_select = int(rand($page_list));
$page_list[$l] = {
title => $books[$book_list]{'title'},
author => $books[$book_list]{'author'},
page => $page_rand
};
# my $new_test = $dt->add(days => 1)->ymd; $page_list{$dt->ymd} = {
# print $new_test . "\n";
# $page_list2{$dt->add(days => 1)->ymd} = {
# title => $books[$book_list]{'title'},
# author => $books[$book_list]{'author'},
# page => $page_rand
# };
$page_list2{$dt->ymd} = {
title => $books[$book_list]{'title'}, title => $books[$book_list]{'title'},
author => $books[$book_list]{'author'}, author => $books[$book_list]{'author'},
page => $page_rand page => $page_rand
}; };
$dt->add(days => 1); $dt->add(days => 1);
# print $dt->add(days => 1)->ymd;
# my $new_date = $dt->add(days => $l);
# print $new_date->ymd;
# for ( my $m; $m < $for; $m++) {
#
#
# } # for m
#
} # for l } # for l
# print scalar keys %page_list2; ###################################
# Make a books_pages_js file
###################################
# print $page_list[55]{'title'}; # made the page_ist into a JSON string
# my $json_text2 = $json->pretty->encode( \%page_list );
# my $json_text = encode_json(\%books);
my $json_text2 = $json->pretty->encode( \%page_list2 );
# make the JSON formatted text into a Javascript object
my $js_text2 = "let book_pages = " . $json_text2; my $js_text2 = "let book_pages = " . $json_text2;
# write the file # write the file
@ -232,56 +200,4 @@ print FH $js_text2;
close(FH); close(FH);
# $books{'pages'} = \@page_text; # FIN
#
# 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]);