From 65f299a8b89e8423a271b48116b178fb1d08bf31 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Fri, 28 May 2021 17:51:16 +0200 Subject: Use pandoc.path to compute URLs --- scripts/metadata_filter.lua | 43 +++++-------------------------------------- 1 file changed, 5 insertions(+), 38 deletions(-) (limited to 'scripts') diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index 6c3817a..895139d 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua @@ -1,3 +1,5 @@ +local path = require 'pandoc.path' + function format_date(date) if not date then return date end @@ -55,51 +57,16 @@ function group_by(l, field, insert) return groups end -function splitstr(input, sep) - sep = sep or "%s" - local t = {} - for str in input:gmatch("([^" .. sep .. "]+)") do table.insert(t, str) end - return t -end - -function relative_to(dir, target) - dir = splitstr(dir, "/") - target = splitstr(target, "/") - - local prefix = true - local path = "" - - for i = 1, math.min(#dir, #target) do - local t = target[i] - if prefix then - if dir[i] ~= t then - prefix = false - path = "../" .. t - end - else - path = "../" .. path .. "/" .. t - end - end - - if #dir < #target then - for i = #dir + 1, #target do path = path .. (path == "" and "" or "/") .. target[i] end - elseif #dir > #target then - for _ = #target + 1, #dir do path = "../" .. path end - end - - return path -end - function make_absolute(rel, base) - return rel:find("^/") and rel or base:gsub("^(.*)/.-$", "%1") .. "/" .. rel + return path.is_absolute(rel) and rel or path.join({ path.directory(base), rel }) end function resolve_url(site_url, ref_file, target_file) target_file = target_file:gsub("/index%.html$", "/") - local ref_base_dir = ref_file:gsub("^(.*)/.-$", "%1") + local ref_base_dir = path.directory(ref_file) local abs = target_file - local rel = relative_to(ref_base_dir, abs):gsub("/index%.html$", "/") + local rel = path.make_relative(abs, ref_base_dir, true) return { abs = abs, rel = rel, full = (site_url .. abs) } end -- cgit v1.2.3-70-g09d2