2024-11-05 07:16:24 +00:00
#!/usr/bin/env luajit
local help = [ [ Usage :
make - epub.lua < config ( JSON file ) > [ action ]
2024-11-07 03:40:32 +00:00
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 .
[ action ] : If not specified , all steps will be taken in order ( except cleanall ) .
2024-11-05 08:11:37 +00:00
download : All pages will be downloaded to their own HTML files .
2024-11-06 23:12:15 +00:00
convert : Each page is converted to Markdown .
2024-11-05 08:11:37 +00:00
concat : A file is created for each section out of its pages .
markdown : Metadata frontmatter and Markdown section files will be
concatenated into a single Markdown file .
epub : Markdown file will be converted to an ePub using pandoc .
2024-11-07 03:40:32 +00:00
cleanpage : All page files will be deleted , along with their extra
2024-11-05 08:11:37 +00:00
directories .
2024-11-07 03:40:32 +00:00
cleanall : Deletes everything except the config file and ePub .
2024-11-05 07:16:24 +00:00
Requirements :
- Binaries : pandoc , curl
2024-11-07 01:03:40 +00:00
For how to write a configuration and examples , see the . lua - files README :
2024-11-07 01:07:43 +00:00
https : // github.com / TangentFoxy / . lua - files # make - epublua
2024-11-05 07:16:24 +00:00
] ]
2024-11-05 07:28:21 +00:00
local success , utility = pcall ( function ( )
2024-11-05 23:19:37 +00:00
return dofile ( ( arg [ 0 ] : match ( " @?(.*/) " ) or arg [ 0 ] : match ( " @?(.* \\ ) " ) ) .. " utility-functions.lua " )
2024-11-05 07:28:21 +00:00
end )
if not success then
print ( " \n \n " .. tostring ( utility ) )
error ( " \n \n This script may be installed improperly. Follow instructions at: \n \t https://github.com/TangentFoxy/.lua-files#installation \n " )
end
2024-11-07 02:56:44 +00:00
local path_separator = utility.path_separator
2024-11-07 01:03:40 +00:00
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 "
2024-11-07 02:45:08 +00:00
-- 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 )
2024-11-07 02:00:21 +00:00
local json = utility.require ( " json " )
2024-11-07 02:45:08 +00:00
config = json.decode ( config_file_text )
config.config_file_text = config_file_text
2024-11-05 07:16:24 +00:00
2024-11-05 23:19:37 +00:00
if not config.authors then
2024-11-07 00:11:15 +00:00
config.authors = { } -- at least have an empty table so it doesn't error below TODO verify that this is actually true
2024-11-06 04:48:33 +00:00
end
if not config.keywords then
config.keywords = { } -- TODO test if it will work empty
2024-11-05 23:19:37 +00:00
end
2024-11-07 00:11:15 +00:00
if config.author then -- old style single author will be prepended to authors list
table.insert ( config.authors , 1 , config.author )
end
2024-11-07 02:00:21 +00:00
-- if only using a single section
if config.first_section_url and not config.base_url then
config.base_url = config.first_section_url -- prevent errors due to required item being missing
end
2024-11-05 09:25:50 +00:00
-- detecting manually specified sections and flagging it to the rest of the script
if config.sections [ 1 ] then
config.sections . start = 1
config.sections . finish = # config.sections
config.manually_specified_sections = true -- decided to make this part of the config spec, but it's set here again just in case
2024-11-05 16:42:00 +00:00
config.base_url = " http://example.com/ " -- must be defined to prevent errors; it will be manipulated and ignored in this use case
2024-11-05 09:25:50 +00:00
end
2024-11-06 04:48:33 +00:00
if not config.sections . start then
config.sections . start = 1 -- the first one can be optional since the common use case is ALL OF THEM
end
2024-11-05 07:16:24 +00:00
if # config.page_counts ~= config.sections . finish - config.sections . start + 1 then
error ( " Number of page_counts does not match number of sections. " )
end
2024-11-05 23:19:37 +00:00
if config.section_titles and # config.section_titles ~= config.sections . finish - config.sections . start + 1 then
error ( " Number of section_titles does not match number of sections. " )
end
2024-11-05 07:16:24 +00:00
return config
end
local function format_metadata ( config )
local function stringify_list ( list )
2024-11-07 02:00:21 +00:00
local output = utility.escape_quotes ( list [ 1 ] ) : enquote ( )
2024-11-05 07:16:24 +00:00
for i = 2 , # list do
2024-11-07 02:00:21 +00:00
output = output .. " , " .. utility.escape_quotes ( list [ i ] ) : enquote ( )
2024-11-05 07:16:24 +00:00
end
return output
end
local keywords_string = stringify_list ( config.keywords )
local metadata = {
" --- " ,
2024-11-07 02:00:21 +00:00
" title: " .. utility.escape_quotes ( config.title ) : enquote ( ) ,
2024-11-05 23:19:37 +00:00
" author: [ " .. stringify_list ( config.authors ) .. " ] " ,
2024-11-05 07:16:24 +00:00
" keywords: [ " .. keywords_string .. " ] " ,
" tags: [ " .. keywords_string .. " ] " ,
" --- " ,
" " ,
}
return table.concat ( metadata , " \n " ) .. " \n "
end
2024-11-07 02:45:08 +00:00
-- TODO since this is called many times across the program, make load_config SET this within the config and use that instead!
2024-11-07 02:00:21 +00:00
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
2024-11-05 07:16:24 +00:00
local function download_pages ( config )
2024-11-05 07:53:24 +00:00
local htmlparser = utility.require ( " htmlparser " )
2024-11-05 07:28:21 +00:00
utility.required_program ( " curl " )
2024-11-07 02:00:21 +00:00
local working_dir = get_base_file_name ( config )
2024-11-05 07:16:24 +00:00
2024-11-07 02:00:21 +00:00
os.execute ( " mkdir " .. working_dir : enquote ( ) )
2024-11-05 07:16:24 +00:00
for section = config.sections . start , config.sections . finish do
2024-11-07 02:00:21 +00:00
local section_dir = working_dir .. path_separator .. tostring ( section ) .. path_separator
os.execute ( " mkdir " .. section_dir : sub ( 1 , - 2 ) : enquote ( ) )
2024-11-05 07:16:24 +00:00
local section_url
if section == 1 and config.first_section_url then
section_url = config.first_section_url
else
2024-11-05 07:28:21 +00:00
section_url = config.base_url .. string.format ( " %02i " , section ) -- leftpad 2 (This will eventually cause problems.)
2024-11-05 07:16:24 +00:00
end
2024-11-05 09:25:50 +00:00
if config.manually_specified_sections then
section_url = config.sections [ section ]
end
2024-11-05 07:16:24 +00:00
for page = 1 , config.page_counts [ section - ( config.sections . start - 1 ) ] do
local download_url
if page == 1 then
download_url = section_url
else
download_url = section_url .. " ?page= " .. tostring ( page )
end
2024-11-07 02:00:21 +00:00
local temporary_html_file_name = utility.tmp_file_name ( )
os.execute ( " curl " .. download_url : enquote ( ) .. " > " .. temporary_html_file_name )
2024-11-05 07:16:24 +00:00
2024-11-09 01:16:58 +00:00
utility.open ( temporary_html_file_name , " r " , " Could not download " .. download_url : enquote ( ) ) ( function ( html_file )
local raw_html = html_file : read ( " *all " )
2024-11-05 07:16:24 +00:00
2024-11-09 01:16:58 +00:00
local parser = htmlparser.parse ( raw_html )
local content_tag = parser : select ( " .article > div > div " ) -- TODO add ability to set selector in config!
local text = content_tag [ 1 ] : getcontent ( )
2024-11-05 07:16:24 +00:00
2024-11-09 01:16:58 +00:00
utility.open ( section_dir .. page .. " .html " , " w " ) ( function ( page_file )
page_file : write ( text .. " \n " )
end )
end )
2024-11-05 07:16:24 +00:00
2024-11-09 01:16:58 +00:00
os.execute ( " rm " .. temporary_html_file_name )
2024-11-05 07:16:24 +00:00
os.execute ( " sleep " .. tostring ( math.random ( 5 ) ) ) -- avoid rate limiting
end
end
end
2024-11-06 23:12:15 +00:00
local function convert_pages ( config )
utility.required_program ( " pandoc " )
2024-11-07 02:00:21 +00:00
local working_dir = get_base_file_name ( config )
2024-11-06 23:12:15 +00:00
for section = config.sections . start , config.sections . finish do
2024-11-07 02:00:21 +00:00
local section_dir = working_dir .. path_separator .. tostring ( section ) .. path_separator
2024-11-06 23:12:15 +00:00
for page = 1 , config.page_counts [ section - ( config.sections . start - 1 ) ] do
local page_file_name_base = section_dir .. page
2024-11-07 02:00:21 +00:00
os.execute ( " pandoc --from html --to markdown " .. ( page_file_name_base .. " .html " ) : enquote ( ) .. " -o " .. ( page_file_name_base .. " .md " ) : enquote ( ) )
2024-11-06 23:12:15 +00:00
end
end
end
2024-11-05 07:16:24 +00:00
local function concatenate_pages ( config )
2024-11-07 02:00:21 +00:00
local working_dir = get_base_file_name ( config )
2024-11-05 07:16:24 +00:00
for section = config.sections . start , config.sections . finish do
2024-11-07 02:00:21 +00:00
local section_dir = working_dir .. path_separator .. tostring ( section ) .. path_separator
2024-11-09 01:16:58 +00:00
utility.open ( working_dir .. path_separator .. tostring ( section ) .. " .md " , " w " ) ( function ( section_file )
for page = 1 , config.page_counts [ section - ( config.sections . start - 1 ) ] do
utility.open ( section_dir .. page .. " .md " , " r " ) ( function ( page_file )
if config.sections . automatic_naming then
local naming_patterns = {
" ^Prologue$ " ,
" ^Chapter %d+$ " ,
" ^%*%*CHAPTER " ,
}
local line = page_file : read ( " *line " )
while line do
for _ , pattern in ipairs ( naming_patterns ) do
if line : find ( pattern ) then
line = " # " .. line
end
end
section_file : write ( line .. " \n " )
line = page_file : read ( " *line " )
2024-11-07 00:05:00 +00:00
end
2024-11-09 01:16:58 +00:00
else
section_file : write ( page_file : read ( " *all " ) )
2024-11-07 00:05:00 +00:00
end
2024-11-09 01:16:58 +00:00
section_file : write ( " \n " ) -- guarantees no accidental line collisions
end )
2024-11-07 00:05:00 +00:00
end
2024-11-09 01:16:58 +00:00
end )
2024-11-05 07:16:24 +00:00
end
end
local function write_markdown_file ( config )
2024-11-07 02:00:21 +00:00
local working_dir = get_base_file_name ( config )
2024-11-09 01:16:58 +00:00
utility.open ( get_base_file_name ( config ) .. " .md " , " w " ) ( function ( markdown_file )
markdown_file : write ( format_metadata ( config ) )
markdown_file : write ( copyright_warning )
2024-11-05 07:16:24 +00:00
2024-11-09 01:16:58 +00:00
for section = config.sections . start , config.sections . finish do
if config.sections . naming then
markdown_file : write ( " \n \n # " .. config.sections . naming .. " " .. tostring ( section ) )
elseif config.section_titles then
markdown_file : write ( " \n \n # " .. config.section_titles [ section ] )
end
markdown_file : write ( " \n \n " )
2024-11-05 07:16:24 +00:00
2024-11-09 01:16:58 +00:00
local section_file_name = working_dir .. path_separator .. tostring ( section )
utility.open ( section_file_name .. " .md " , " r " ) ( function ( section_file )
markdown_file : write ( section_file : read ( " *all " ) )
end )
end
2024-11-05 07:16:24 +00:00
2024-11-09 01:16:58 +00:00
markdown_file : write ( " \n \n # Ebook Creation Metadata \n \n " )
markdown_file : write ( copyright_warning )
markdown_file : write ( " This ebook was created using the following config: \n \n " )
markdown_file : write ( " ```json \n " .. config.config_file_text .. " \n ``` \n " )
end )
2024-11-05 07:16:24 +00:00
end
local function make_epub ( config )
2024-11-05 07:28:21 +00:00
utility.required_program ( " pandoc " )
2024-11-07 02:04:16 +00:00
local output_dir = " All ePubs "
os.execute ( " mkdir " .. output_dir : enquote ( ) )
2024-11-07 02:00:21 +00:00
2024-11-05 07:16:24 +00:00
local base_file_name = get_base_file_name ( config )
2024-11-07 02:04:16 +00:00
os.execute ( " pandoc --from markdown --to epub " .. ( base_file_name .. " .md " ) : enquote ( ) .. " -o " .. ( output_dir .. path_separator .. base_file_name .. " .epub " ) : enquote ( ) .. " --toc=true " )
2024-11-05 07:16:24 +00:00
end
2024-11-07 03:40:32 +00:00
local function rm_page_files ( config )
2024-11-07 02:00:21 +00:00
local working_dir = get_base_file_name ( config )
2024-11-06 04:58:18 +00:00
2024-11-05 08:11:37 +00:00
for section = config.sections . start , config.sections . finish do
2024-11-07 02:00:21 +00:00
local section_dir = working_dir .. path_separator .. tostring ( section )
2024-11-07 03:40:32 +00:00
os.execute ( utility.recursive_remove_command .. section_dir : enquote ( ) )
2024-11-05 08:11:37 +00:00
end
end
local function rm_all ( config )
2024-11-07 02:00:21 +00:00
local working_dir = get_base_file_name ( config )
2024-11-05 08:11:37 +00:00
2024-11-07 03:40:32 +00:00
os.execute ( utility.recursive_remove_command .. working_dir : enquote ( ) )
2024-11-07 02:00:21 +00:00
os.execute ( " rm " .. ( get_base_file_name ( config ) .. " .md " ) : enquote ( ) )
2024-11-05 08:11:37 +00:00
end
2024-11-07 02:45:08 +00:00
local function argparse ( arguments , positional_arguments )
local recognized_arguments = { }
for index , argument in ipairs ( arguments ) do
for _ , help in ipairs ( { " -h " , " --help " , " /? " , " /help " , " help " } ) do
if argument == help then
print ( help )
return nil
end
end
if positional_arguments [ index ] then
recognized_arguments [ positional_arguments [ index ] ] = argument
end
end
return recognized_arguments
end
2024-11-07 02:56:44 +00:00
local function main ( arguments )
2024-11-09 01:16:58 +00:00
local config = utility.open ( arguments.json_file_name , " r " ) ( function ( config_file )
return load_config ( config_file : read ( " *all " ) )
end )
2024-11-07 02:56:44 +00:00
local actions = {
download = download_pages ,
convert = convert_pages ,
concat = concatenate_pages ,
markdown = write_markdown_file ,
epub = make_epub ,
2024-11-07 03:40:32 +00:00
cleanpage = rm_page_files ,
2024-11-07 02:56:44 +00:00
cleanall = rm_all ,
}
if arguments.action then
if actions [ arguments.action ] then
actions [ arguments.action ] ( config )
else
print ( help )
error ( " \n Invalid action specified. " )
end
else
print ( " \n Downloading pages... \n " )
download_pages ( config )
print ( " \n Converting pages... \n " )
convert_pages ( config )
print ( " \n Concatenating pages... \n " )
concatenate_pages ( config )
2024-11-07 03:40:32 +00:00
print ( " \n Removing page files... \n " )
rm_page_files ( config )
2024-11-07 02:56:44 +00:00
print ( " \n Writing Markdown file... \n " )
write_markdown_file ( config )
print ( " \n Making ePub... \n " )
make_epub ( config )
print ( " \n Done! \n " )
end
end
2024-11-07 02:45:08 +00:00
local positional_arguments = { " json_file_name " , " action " }
local arguments = argparse ( arg , positional_arguments )
if not arguments.json_file_name then
print ( help )
error ( " \n A config file name/path must be specified. " )
end
2024-11-07 02:56:44 +00:00
if arguments.json_file_name == " . " then
utility.ls ( " . " ) ( function ( file_name )
if file_name : find ( " .json$ " ) then
arguments.json_file_name = file_name
main ( arguments )
end
end )
2024-11-05 07:16:24 +00:00
else
2024-11-07 02:56:44 +00:00
main ( arguments )
2024-11-05 07:16:24 +00:00
end