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

256
books.pl
View File

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