From 4709f2ff38a6127469300c71b1153c934f45b9be Mon Sep 17 00:00:00 2001 From: Carsten Gips Date: Tue, 22 Aug 2023 08:13:56 +0200 Subject: [PATCH] [Filter] refactor makedeps to improve readability (#131) * [Filter] refactor makedeps to improve readability * fix typo --- filters/hugo_makedeps.lua | 42 ++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/filters/hugo_makedeps.lua b/filters/hugo_makedeps.lua index dbf8790..69b4d4e 100644 --- a/filters/hugo_makedeps.lua +++ b/filters/hugo_makedeps.lua @@ -121,12 +121,25 @@ local function _is_relative (target) return pandoc.path.is_relative(target) end +local function _is_url (target) + return target:match('https?://.*') +end + local function _is_markdown (target) return target:match('.*%.md') end -local function _is_url (target) - return target:match('https?://.*') +local function _is_local_path (path) + return _is_relative(path) and + not _is_url(path) +end + +local function _is_local_markdown_file_link (inline) + return inline and + inline.t and + inline.t == "Link" and + _is_markdown(inline.target) and + _is_local_path(inline.target) end local function _old_path (include_path, file) @@ -219,13 +232,13 @@ local function _process_doc (blocks, md_file, include_path) -- collect and enqueue all new images and links in this file 'include_path/md_file' local collect_images_links = { Image = function (image) - if _is_relative(image.src) and not _is_url(image.src) then + if _is_local_path(image.src) then -- remember this image _remember_image(include_path, md_file, image.src, newl) end end, Link = function (link) - if _is_markdown(link.target) and _is_relative(link.target) and not _is_url(link.target) then + if _is_local_markdown_file_link(link) then -- enqueue "include_path/link.target" for later processing _enqueue(_old_path(include_path, link.target)) end @@ -235,26 +248,20 @@ local function _process_doc (blocks, md_file, include_path) end end -local function _read_file (fname) - local fh = io.open(fname, "r") +local function _handle_file (oldl) + local fh = io.open(oldl, "r") if not fh then - io.stderr:write("\t (_read_file) WARNING: cannot open file '" .. fname .. "' ... skipping ... \n") + io.stderr:write("\t (_handle_file) WARNING: cannot open file '" .. oldl .. "' ... skipping ... \n") else - local content = fh:read "*all" + local blocks = pandoc.read(fh:read "*all", "markdown", PANDOC_READER_OPTIONS).blocks fh:close() - return pandoc.read(content, "markdown", PANDOC_READER_OPTIONS).blocks - end -end -local function _handle_file (oldl) - local blocks = _read_file(oldl) - - if blocks then pandoc.system.with_working_directory( pandoc.path.directory(oldl), -- may still contain '../' function () - local wrkdir = pandoc.system.get_working_directory() -- same as 'pandoc.path.directory(oldl)' but w/o '../' since Pandoc cd'ed here - _process_doc(blocks, _filename_woext(oldl), pandoc.path.make_relative(wrkdir, ROOT)) + -- same as 'pandoc.path.directory(oldl)' but w/o '../' since Pandoc cd'ed here + local include_path = pandoc.path.make_relative(pandoc.system.get_working_directory(), ROOT) + _process_doc(blocks, _filename_woext(oldl), include_path) end) end end @@ -284,7 +291,6 @@ local function _emit_links () end - -- main filter function function Pandoc (doc) -- init global vars using metadata: meta.prefix and meta.indexMD