From d58f161e9eb8e12dc180742c227cfcaf677bb098 Mon Sep 17 00:00:00 2001 From: Tangent Date: Wed, 6 Nov 2024 17:05:00 -0700 Subject: [PATCH] close #17 automatic_naming implemented --- README.md | 1 + make-epub.lua | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e43b572..e0acb26 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,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, there will be no Table of Contents. + - `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.) Example: ```json diff --git a/make-epub.lua b/make-epub.lua index 34fffd6..aee5fd1 100755 --- a/make-epub.lua +++ b/make-epub.lua @@ -223,7 +223,25 @@ local function concatenate_pages(config) for page = 1, config.page_counts[section - (config.sections.start - 1)] do local page_file, err = io.open(section_dir .. page .. ".md", "r") if not page_file then error(err) end - section_file:write(page_file:read("*a") .. "\n") -- the \n probably isn't needed, but does guarantee safety.. + if config.sections.automatic_naming then + local naming_patterns = { + "^Prologue$", + "^Chapter %d+$", + } + 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") + end + else + section_file:write(page_file:read("*a")) + end + section_file:write("\n") -- guarantees no accidental line collisions page_file:close() end end