diff --git a/books.pl b/books.pl index 401525c..54bbffb 100755 --- a/books.pl +++ b/books.pl @@ -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] = "

CHAPTER" . $chapter_lines[0] . "

\n\n

"; - } + my @chapter_lines = split(/\r\n/, $chapters[$j]); + if ($j != 0){ + $chapter_lines[0] = "

CHAPTER" . $chapter_lines[0] . "

\n\n

"; + } # 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] = "

" . $page[0]; + # how many lines are in the whole chapter + my $chapter_length = @chapter_lines; - foreach (@page) { + my $i = 0; - if ($_ eq "") { - $_ = "

\n\n

"; - } - } # 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] . "

\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] = "

" . $page[0]; + + # ad in blank space + foreach (@page) { + if ($_ eq "") { + $_ = "

\n\n

"; + } + } # 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] . "

\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] = "

Chapter" . $chapter_lines[0] . "

\n\n

"; - - -# 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 "") { -# $_ = "

\n\n

"; -# } -# } # foreach page -# -# $page_text[$i] = join("", @page); -# $page_text[$i] = $page_text[$i] . "

\n\n"; -# -# $i++; -# $line_number = 31 * $i; -# -# } -# -# print($page_text[1]); \ No newline at end of file +# FIN \ No newline at end of file