Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gmcninch-tufts
GitHub Repository: gmcninch-tufts/2024-Sp-Math190
Path: blob/main/build-assets/prefix-path.lua
898 views
1
pathPrefixMetaName = "pathToProjectRoot"
2
3
-- Global variable to store path prefix once it has been
4
-- extracted from the metadata
5
pathPrefix = nil
6
7
-- Traversal should run top down, in order for the
8
-- Meta function to be called before Link or Image
9
-- functions. Otherwise the path prefix is not known
10
-- when the Link/Image functions are run.
11
traverse = 'topdown'
12
13
-- Utility function with prepends paths with the path prefix
14
local function prefix_path(p)
15
return pandoc.path.normalize(pathPrefix .. "/" .. p)
16
end
17
18
-- Adjust absolute link targets
19
function Link(l)
20
if not pathPrefix then return end
21
if pandoc.path.is_absolute(l.target) then
22
local new_link = l:clone()
23
new_link.target = prefix_path(l.target)
24
return new_link
25
end
26
end
27
28
-- Adjust absolute src links of images
29
function Image(i)
30
if not pathPrefix then return end
31
if pandoc.path.is_absolute(i.src) then
32
local new_img = i:clone()
33
new_img.src = prefix_path(i.src)
34
return new_img
35
end
36
end
37
38
-- Optimize traversal, stop if no meta variable available
39
function Blocks(p)
40
if not pathPrefix then return nil, false end
41
end
42
43
-- Extract required meta variable
44
function Meta(m)
45
local meta_var = m[pathPrefixMetaName]
46
if not meta_var then return nil, false end
47
if type(meta_var) == "string" then
48
-- When specified via command line option -M, then
49
-- the meta variable will be a bare string
50
pathPrefix = meta_var
51
elseif type(meta_var) == "table"
52
and type(meta_var[1]) == "userdata"
53
and pandoc.utils.type(meta_var[1]) == "Inline" then
54
-- When specified via YAML, then the meta variable
55
-- will be a pandoc [Str "path/"]
56
pathPrefix = meta_var[1].text
57
end
58
end
59
60