summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--scripts/metadata_filter.lua43
1 files changed, 5 insertions, 38 deletions
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 @@
1local path = require 'pandoc.path'
2
1function format_date(date) 3function format_date(date)
2 if not date then return date end 4 if not date then return date end
3 5
@@ -55,51 +57,16 @@ function group_by(l, field, insert)
55 return groups 57 return groups
56end 58end
57 59
58function splitstr(input, sep)
59 sep = sep or "%s"
60 local t = {}
61 for str in input:gmatch("([^" .. sep .. "]+)") do table.insert(t, str) end
62 return t
63end
64
65function relative_to(dir, target)
66 dir = splitstr(dir, "/")
67 target = splitstr(target, "/")
68
69 local prefix = true
70 local path = ""
71
72 for i = 1, math.min(#dir, #target) do
73 local t = target[i]
74 if prefix then
75 if dir[i] ~= t then
76 prefix = false
77 path = "../" .. t
78 end
79 else
80 path = "../" .. path .. "/" .. t
81 end
82 end
83
84 if #dir < #target then
85 for i = #dir + 1, #target do path = path .. (path == "" and "" or "/") .. target[i] end
86 elseif #dir > #target then
87 for _ = #target + 1, #dir do path = "../" .. path end
88 end
89
90 return path
91end
92
93function make_absolute(rel, base) 60function make_absolute(rel, base)
94 return rel:find("^/") and rel or base:gsub("^(.*)/.-$", "%1") .. "/" .. rel 61 return path.is_absolute(rel) and rel or path.join({ path.directory(base), rel })
95end 62end
96 63
97function resolve_url(site_url, ref_file, target_file) 64function resolve_url(site_url, ref_file, target_file)
98 target_file = target_file:gsub("/index%.html$", "/") 65 target_file = target_file:gsub("/index%.html$", "/")
99 66
100 local ref_base_dir = ref_file:gsub("^(.*)/.-$", "%1") 67 local ref_base_dir = path.directory(ref_file)
101 local abs = target_file 68 local abs = target_file
102 local rel = relative_to(ref_base_dir, abs):gsub("/index%.html$", "/") 69 local rel = path.make_relative(abs, ref_base_dir, true)
103 70
104 return { abs = abs, rel = rel, full = (site_url .. abs) } 71 return { abs = abs, rel = rel, full = (site_url .. abs) }
105end 72end