close #22 run all configs in a directory

This commit is contained in:
Tangent / Rose / Nebula Rosa 2024-11-06 19:56:44 -07:00
parent 3c648fbc85
commit 063437b596
2 changed files with 51 additions and 43 deletions

View File

@ -31,14 +31,7 @@ if not success then
error("\n\nThis script may be installed improperly. Follow instructions at:\n\thttps://github.com/TangentFoxy/.lua-files#installation\n") error("\n\nThis script may be installed improperly. Follow instructions at:\n\thttps://github.com/TangentFoxy/.lua-files#installation\n")
end end
-- TODO utility.path_separator should be a thing local path_separator = utility.path_separator
local path_separator
if utility.OS == "Windows" then
path_separator = "\\"
else
path_separator = "/"
end
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 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!
@ -322,6 +315,46 @@ local function argparse(arguments, positional_arguments)
return recognized_arguments return recognized_arguments
end end
local function main(arguments)
local config_file, err = io.open(arguments.json_file_name, "r")
if not config_file then error(err) end
local config = load_config(config_file:read("*all"))
config_file:close()
local actions = {
download = download_pages,
convert = convert_pages,
concat = concatenate_pages,
markdown = write_markdown_file,
epub = make_epub,
cleanhtml = rm_html_files,
cleanall = rm_all,
}
if arguments.action then
if actions[arguments.action] then
actions[arguments.action](config)
else
print(help)
error("\nInvalid action specified.")
end
else
print("\nDownloading pages...\n")
download_pages(config)
print("\nConverting pages...\n")
convert_pages(config)
print("\nConcatenating pages...\n")
concatenate_pages(config)
print("\nWriting Markdown file...\n")
write_markdown_file(config)
print("\nMaking ePub...\n")
make_epub(config)
-- print("\nRemoving HTML files...\n")
-- rm_html_files(config)
print("\nDone!\n")
end
end
local positional_arguments = {"json_file_name", "action"} local positional_arguments = {"json_file_name", "action"}
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
@ -329,40 +362,13 @@ if not arguments.json_file_name then
error("\nA config file name/path must be specified.") error("\nA config file name/path must be specified.")
end end
local config_file, err = io.open(arguments.json_file_name, "r") if arguments.json_file_name == "." then
if not config_file then error(err) end utility.ls(".")(function(file_name)
local config = load_config(config_file:read("*all")) if file_name:find(".json$") then
config_file:close() arguments.json_file_name = file_name
main(arguments)
local actions = { end
download = download_pages, end)
convert = convert_pages,
concat = concatenate_pages,
markdown = write_markdown_file,
epub = make_epub,
cleanhtml = rm_html_files,
cleanall = rm_all,
}
if arguments.action then
if actions[arguments.action] then
actions[arguments.action](config)
else
print(help)
error("\nInvalid action specified.")
end
else else
print("\nDownloading pages...\n") main(arguments)
download_pages(config)
print("\nConverting pages...\n")
convert_pages(config)
print("\nConcatenating pages...\n")
concatenate_pages(config)
print("\nWriting Markdown file...\n")
write_markdown_file(config)
print("\nMaking ePub...\n")
make_epub(config)
-- print("\nRemoving HTML files...\n")
-- rm_html_files(config)
print("\nDone!\n")
end end

View File

@ -16,8 +16,10 @@ local utility = {}
if package.config:sub(1, 1) == "\\" then if package.config:sub(1, 1) == "\\" then
utility.OS = "Windows" utility.OS = "Windows"
utility.path_separator = "\\"
else else
utility.OS = "UNIX-like" utility.OS = "UNIX-like"
utility.path_separator = "/"
end end
utility.path = arg[0]:match("@?(.*/)") or arg[0]:match("@?(.*\\)") -- inspired by discussion in https://stackoverflow.com/q/6380820 utility.path = arg[0]:match("@?(.*/)") or arg[0]:match("@?(.*\\)") -- inspired by discussion in https://stackoverflow.com/q/6380820