From f593a3c05b42f81acc52ae8e994fd14e9e3131a7 Mon Sep 17 00:00:00 2001 From: Tangent Date: Sat, 9 Nov 2024 00:37:27 -0700 Subject: [PATCH] fix #32 extract titles from URL --- README.md | 3 ++- make-epub.lua | 23 +++++++++++++++++++++++ utility-functions.lua | 20 ++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e4614cf..e4e740d 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ The following is shared: - `keywords`: Array of Strings: Keywords/Tags. (I'm not sure what the difference is in the final output so it goes in both.) - `sections`: **See "Book"/"Anthology" variations.** (I call LitErotica's stories sections - because they are often part of a larger whole.) - `section_titles`: (Optional) Array of Strings: The titles to be used for Table of Contents / headings. (If `sections.naming` is specified, `section_titles` will be ignored.) +- `lazy_titling`: (Optional) Boolean: URLs will be used to generate section titles. (Warning: This process is likely to create janky titles.) - `page_counts`: Array of Integers: The number of pages on LitErotica per "story". (I call them sections because this script was made to put together story series originally.) #### Variation: Book @@ -59,7 +60,7 @@ The following is shared: - `start`: (Optional) Number: Where to start. (`1` is the default, since it is the most common.) - `finish`: Number: Where to end. - `naming`: (Optional) String: How to name sections in the final output. The result is `[naming] [#]` (using section numbers). (If not specified, sections will not have headings.) - - `automatic_naming`: (Optional) Boolean: If any line matches "Prologue" or "Chapter #" (any number), it will be made into a heading. (Note: This does not override `naming`. Both can be used together.) + - `automatic_naming`: (Optional) Boolean: If any line matches "Prologue" or "Chapter #" (any number), it will be made into a heading. (Note: This does not override `naming`. Both can be used together.) (Other patterns will be added as I find them.) Example: ```json diff --git a/make-epub.lua b/make-epub.lua index 558c23a..1332820 100755 --- a/make-epub.lua +++ b/make-epub.lua @@ -227,6 +227,29 @@ local function write_markdown_file(config) markdown_file:write("\n\n# " .. config.sections.naming .. " " .. tostring(section)) elseif config.section_titles then markdown_file:write("\n\n# " .. config.section_titles[section]) + elseif config.lazy_titling then + local section_url + if section == 1 and config.first_section_url then + section_url = config.first_section_url + else + section_url = config.base_url + end + if config.manually_specified_sections then + section_url = config.sections[section] + end + + local title_parts = section_url:sub(30):gsplit("-") + while tonumber(title_parts[#title_parts]) do + title_parts[#title_parts] = nil + end + local last_part = title_parts[#title_parts] + if last_part == "ch" or last_part == "pt" then + title_parts[#title_parts] = nil + end + for index, part in ipairs(title_parts) do + title_parts[index] = part:sub(1, 1):upper() .. part:sub(2) + end + markdown_file:write("\n\n# " .. table.concat(title_parts, " ")) end markdown_file:write("\n\n") diff --git a/utility-functions.lua b/utility-functions.lua index 3260de3..c2de14e 100644 --- a/utility-functions.lua +++ b/utility-functions.lua @@ -59,6 +59,26 @@ function string.enquote(s) return "\"" .. s .. "\"" end +local function escape_special_characters(s) + local special_characters = "[()%%.[^$%]*+%-?]" + if s == nil then return end + return (s:gsub(special_characters, "%%%1")) +end + +function string.gsplit(s, delimiter) + delimiter = delimiter or "," + if s:sub(-#delimiter) ~= delimiter then s = s .. delimiter end + return s:gmatch("(.-)" .. escape_special_characters(delimiter)) +end + +function string.split(s, delimiter) + local result = {} + for item in s:gsplit(delimiter) do + result[#result + 1] = item + end + return result +end + utility.require = function(name) local success, package_or_err = pcall(function() return dofile((arg[0]:match("@?(.*/)") or arg[0]:match("@?(.*\\)")) .. name .. ".lua")