Compare commits

..

3 Commits

2 changed files with 46 additions and 16 deletions

View File

@ -37,6 +37,9 @@ will be used to make multiple ebooks back-to-back.
directories. directories.
cleanall: Deletes everything except the config file and ePub. cleanall: Deletes everything except the config file and ePub.
[flag]: If "--continue" is passed, script will continue with the default order
of actions from the action specified.
Requirements: Requirements:
- Binaries: pandoc, curl - Binaries: pandoc, curl
``` ```

View File

@ -2,7 +2,7 @@
local help = [[Usage: local help = [[Usage:
make-epub.lua <config (JSON file)> [action] make-epub.lua <config (JSON file)> [action] [flag]
If "." is used instead of a JSON file, every JSON file in the current directory If "." is used instead of a JSON file, every JSON file in the current directory
will be used to make multiple ebooks back-to-back. will be used to make multiple ebooks back-to-back.
@ -18,6 +18,9 @@ will be used to make multiple ebooks back-to-back.
directories. directories.
cleanall: Deletes everything except the config file and ePub. cleanall: Deletes everything except the config file and ePub.
[flag]: If "--continue" is passed, script will continue with the default order
of actions from the action specified.
Requirements: Requirements:
- Binaries: pandoc, curl - Binaries: pandoc, curl
@ -34,7 +37,7 @@ if not success then
end end
local path_separator = utility.path_separator local path_separator = utility.path_separator
local copyright_warning = "This ebook was created by an automated tool for personal use. It cannot be distributed or sold without permission of copyright holder(s). (If you did not make this ebook, you may be infringing.)\n\n" local copyright_warning = "This ebook was created by an automated tool for personal use. It cannot be distributed or sold without permission of its copyright holder(s). (If you did not make this ebook, you may be infringing.)\n\n"
-- also checks for errors TODO make it check for ALL required elements and error if any are missing! -- also checks for errors TODO make it check for ALL required elements and error if any are missing!
local function load_config(config_file_text) local function load_config(config_file_text)
@ -60,6 +63,10 @@ local function load_config(config_file_text)
config.base_url = config.first_section_url -- prevent errors due to required item being missing config.base_url = config.first_section_url -- prevent errors due to required item being missing
end end
if not config.sections then
config.sections = {} -- I've decided to allow empty sections (defaults to 1 section, for single story ebooks)
end
-- detecting manually specified sections and flagging it to the rest of the script -- detecting manually specified sections and flagging it to the rest of the script
if config.sections[1] then if config.sections[1] then
config.sections.start = 1 config.sections.start = 1
@ -72,6 +79,10 @@ local function load_config(config_file_text)
config.sections.start = 1 -- the first one can be optional since the common use case is ALL OF THEM config.sections.start = 1 -- the first one can be optional since the common use case is ALL OF THEM
end end
if not config.sections.finish then
config.sections.finish = 1
end
if #config.page_counts ~= config.sections.finish - config.sections.start + 1 then if #config.page_counts ~= config.sections.finish - config.sections.start + 1 then
error("Number of page_counts does not match number of sections.") error("Number of page_counts does not match number of sections.")
end end
@ -118,6 +129,7 @@ local function format_metadata(config)
end end
local function download_pages(config) local function download_pages(config)
print("\nDownloading pages...\n")
local htmlparser = utility.require("htmlparser") local htmlparser = utility.require("htmlparser")
utility.required_program("curl") utility.required_program("curl")
local working_dir = config.base_file_name local working_dir = config.base_file_name
@ -172,6 +184,7 @@ local function download_pages(config)
end end
local function convert_pages(config) local function convert_pages(config)
print("\nConverting pages...\n")
utility.required_program("pandoc") utility.required_program("pandoc")
local working_dir = config.base_file_name local working_dir = config.base_file_name
@ -186,6 +199,7 @@ local function convert_pages(config)
end end
local function concatenate_pages(config) local function concatenate_pages(config)
print("\nConcatenating pages...\n")
local working_dir = config.base_file_name local working_dir = config.base_file_name
for section = config.sections.start, config.sections.finish do for section = config.sections.start, config.sections.finish do
@ -220,6 +234,7 @@ local function concatenate_pages(config)
end end
local function write_markdown_file(config) local function write_markdown_file(config)
print("\nWriting Markdown file...\n")
local working_dir = config.base_file_name local working_dir = config.base_file_name
utility.open(config.base_file_name .. ".md", "w")(function(markdown_file) utility.open(config.base_file_name .. ".md", "w")(function(markdown_file)
@ -271,6 +286,7 @@ local function write_markdown_file(config)
end end
local function make_epub(config) local function make_epub(config)
print("\nMaking ePub...\n")
utility.required_program("pandoc") utility.required_program("pandoc")
local output_dir = "All ePubs" local output_dir = "All ePubs"
os.execute("mkdir " .. output_dir:enquote()) os.execute("mkdir " .. output_dir:enquote())
@ -281,6 +297,7 @@ local function make_epub(config)
end end
local function rm_page_files(config) local function rm_page_files(config)
print("\nRemoving page files...\n")
local working_dir = config.base_file_name local working_dir = config.base_file_name
for section = config.sections.start, config.sections.finish do for section = config.sections.start, config.sections.finish do
@ -290,6 +307,7 @@ local function rm_page_files(config)
end end
local function rm_all(config) local function rm_all(config)
print("\nRemoving all extra files...\n")
local working_dir = config.base_file_name local working_dir = config.base_file_name
os.execute(utility.recursive_remove_command .. working_dir:enquote()) os.execute(utility.recursive_remove_command .. working_dir:enquote())
@ -326,32 +344,41 @@ local function main(arguments)
cleanpage = rm_page_files, cleanpage = rm_page_files,
cleanall = rm_all, cleanall = rm_all,
} }
local default_action_order = {
"download",
"convert",
"concat",
"cleanpage",
"markdown",
"epub",
}
if arguments.action then if arguments.action then
if actions[arguments.action] then if actions[arguments.action] then
actions[arguments.action](config) actions[arguments.action](config)
if arguments.flag == "--continue" then
local starting_point_reached = false
for _, action in ipairs(default_action_order) do
if starting_point_reached then
actions[action](config)
elseif action == arguments.action then
starting_point_reached = true
end
end
end
else else
print(help) print(help)
error("\nInvalid action specified.") error("\nInvalid action specified.")
end end
else else
print("\nDownloading pages...\n") for _, action in ipairs(default_action_order) do
download_pages(config) actions[action](config)
print("\nConverting pages...\n")
convert_pages(config)
print("\nConcatenating pages...\n")
concatenate_pages(config)
print("\nRemoving page files...\n")
rm_page_files(config)
print("\nWriting Markdown file...\n")
write_markdown_file(config)
print("\nMaking ePub...\n")
make_epub(config)
print("\nDone!\n")
end end
end
print("\nDone!\n")
end end
local positional_arguments = {"json_file_name", "action"} local positional_arguments = {"json_file_name", "action", "flag"}
local arguments = argparse(arg, positional_arguments) local arguments = argparse(arg, positional_arguments)
if not arguments.json_file_name then if not arguments.json_file_name then
print(help) print(help)