mirror of
https://github.com/TangentFoxy/.lua-files.git
synced 2024-11-20 21:34:23 +00:00
close #30 optimize base_file_name
This commit is contained in:
parent
98a603e647
commit
98365ff861
@ -80,6 +80,17 @@ local function load_config(config_file_text)
|
|||||||
error("Number of section_titles does not match number of sections.")
|
error("Number of section_titles does not match number of sections.")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local base_file_name
|
||||||
|
if config.title and config.authors[1] then
|
||||||
|
-- first author in list gets top billing (this is problematic in anthologies unless an editor is the first entry)
|
||||||
|
base_file_name = config.title .. " by " .. config.authors[1]
|
||||||
|
elseif config.title then
|
||||||
|
base_file_name = config.title
|
||||||
|
else
|
||||||
|
base_file_name = "Book"
|
||||||
|
end
|
||||||
|
config.base_file_name = utility.make_safe_file_name(config.base_file_name or base_file_name)
|
||||||
|
|
||||||
return config
|
return config
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -106,34 +117,10 @@ local function format_metadata(config)
|
|||||||
return table.concat(metadata, "\n") .. "\n"
|
return table.concat(metadata, "\n") .. "\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO since this is called many times across the program, make load_config SET this within the config and use that instead!
|
|
||||||
local function get_base_file_name(config)
|
|
||||||
-- TODO move make_safe_file_name to utility
|
|
||||||
local function make_safe_file_name(file_name)
|
|
||||||
file_name = file_name:gsub("[%\"%:%\\%!%@%#%$%%%^%*%=%{%}%|%;%<%>%?%/]", "") -- everything except the &
|
|
||||||
file_name = file_name:gsub(" %&", ",") -- replacing & with a comma works for 99% of things
|
|
||||||
file_name = file_name:gsub("%&", ",") -- replacing & with a comma works for 99% of things
|
|
||||||
file_name = file_name:gsub("[%s+]", " ") -- more than one space in succession should be a single space
|
|
||||||
return file_name
|
|
||||||
end
|
|
||||||
|
|
||||||
local base_file_name
|
|
||||||
if config.title and config.authors[1] then
|
|
||||||
-- first author in list gets top billing (this is problematic in anthologies unless an editor is the first entry)
|
|
||||||
base_file_name = config.title .. " by " .. config.authors[1]
|
|
||||||
elseif config.title then
|
|
||||||
base_file_name = config.title
|
|
||||||
else
|
|
||||||
base_file_name = "Book"
|
|
||||||
end
|
|
||||||
|
|
||||||
return make_safe_file_name(config.base_file_name or base_file_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function download_pages(config)
|
local function download_pages(config)
|
||||||
local htmlparser = utility.require("htmlparser")
|
local htmlparser = utility.require("htmlparser")
|
||||||
utility.required_program("curl")
|
utility.required_program("curl")
|
||||||
local working_dir = get_base_file_name(config)
|
local working_dir = config.base_file_name
|
||||||
|
|
||||||
os.execute("mkdir " .. working_dir:enquote())
|
os.execute("mkdir " .. working_dir:enquote())
|
||||||
for section = config.sections.start, config.sections.finish do
|
for section = config.sections.start, config.sections.finish do
|
||||||
@ -182,7 +169,7 @@ end
|
|||||||
|
|
||||||
local function convert_pages(config)
|
local function convert_pages(config)
|
||||||
utility.required_program("pandoc")
|
utility.required_program("pandoc")
|
||||||
local working_dir = get_base_file_name(config)
|
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
|
||||||
local section_dir = working_dir .. path_separator .. tostring(section) .. path_separator
|
local section_dir = working_dir .. path_separator .. tostring(section) .. path_separator
|
||||||
@ -195,7 +182,7 @@ local function convert_pages(config)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function concatenate_pages(config)
|
local function concatenate_pages(config)
|
||||||
local working_dir = get_base_file_name(config)
|
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
|
||||||
local section_dir = working_dir .. path_separator .. tostring(section) .. path_separator
|
local section_dir = working_dir .. path_separator .. tostring(section) .. path_separator
|
||||||
@ -229,9 +216,9 @@ local function concatenate_pages(config)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function write_markdown_file(config)
|
local function write_markdown_file(config)
|
||||||
local working_dir = get_base_file_name(config)
|
local working_dir = config.base_file_name
|
||||||
|
|
||||||
utility.open(get_base_file_name(config) .. ".md", "w")(function(markdown_file)
|
utility.open(config.base_file_name .. ".md", "w")(function(markdown_file)
|
||||||
markdown_file:write(format_metadata(config))
|
markdown_file:write(format_metadata(config))
|
||||||
markdown_file:write(copyright_warning)
|
markdown_file:write(copyright_warning)
|
||||||
|
|
||||||
@ -261,12 +248,13 @@ local function make_epub(config)
|
|||||||
local output_dir = "All ePubs"
|
local output_dir = "All ePubs"
|
||||||
os.execute("mkdir " .. output_dir:enquote())
|
os.execute("mkdir " .. output_dir:enquote())
|
||||||
|
|
||||||
local base_file_name = get_base_file_name(config)
|
local markdown_file_name = config.base_file_name .. ".md"
|
||||||
os.execute("pandoc --from markdown --to epub " .. (base_file_name .. ".md"):enquote() .. " -o " .. (output_dir .. path_separator .. base_file_name .. ".epub"):enquote() .. " --toc=true")
|
local epub_file_name = output_dir .. path_separator .. config.base_file_name .. ".epub"
|
||||||
|
os.execute("pandoc --from markdown --to epub " .. markdown_file_name:enquote() .. " -o " .. epub_file_name:enquote() .. " --toc=true")
|
||||||
end
|
end
|
||||||
|
|
||||||
local function rm_page_files(config)
|
local function rm_page_files(config)
|
||||||
local working_dir = get_base_file_name(config)
|
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
|
||||||
local section_dir = working_dir .. path_separator .. tostring(section)
|
local section_dir = working_dir .. path_separator .. tostring(section)
|
||||||
@ -275,10 +263,10 @@ local function rm_page_files(config)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function rm_all(config)
|
local function rm_all(config)
|
||||||
local working_dir = get_base_file_name(config)
|
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())
|
||||||
os.execute("rm " .. (get_base_file_name(config) .. ".md"):enquote())
|
os.execute("rm " .. (config.base_file_name .. ".md"):enquote())
|
||||||
end
|
end
|
||||||
|
|
||||||
local function argparse(arguments, positional_arguments)
|
local function argparse(arguments, positional_arguments)
|
||||||
|
@ -100,6 +100,14 @@ utility.tmp_file_name = function()
|
|||||||
return "." .. utility.uuid() .. ".tmp"
|
return "." .. utility.uuid() .. ".tmp"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
utility.make_safe_file_name = function(file_name)
|
||||||
|
file_name = file_name:gsub("[%\"%:%\\%!%@%#%$%%%^%*%=%{%}%|%;%<%>%?%/]", "") -- everything except the &
|
||||||
|
file_name = file_name:gsub(" %&", ",") -- replacing & with a comma works for 99% of things
|
||||||
|
file_name = file_name:gsub("%&", ",") -- replacing & with a comma works for 99% of things
|
||||||
|
file_name = file_name:gsub("[%s+]", " ") -- more than one space in succession should be a single space
|
||||||
|
return file_name
|
||||||
|
end
|
||||||
|
|
||||||
-- io.open, but errors are immediately thrown, and the file is closed for you
|
-- io.open, but errors are immediately thrown, and the file is closed for you
|
||||||
utility.open = function(file_name, mode, custom_error_message)
|
utility.open = function(file_name, mode, custom_error_message)
|
||||||
local file, err = io.open(file_name, mode)
|
local file, err = io.open(file_name, mode)
|
||||||
|
Loading…
Reference in New Issue
Block a user