-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Ensure that images are not placed in subfolders
- Loading branch information
Showing
3 changed files
with
46 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
local mediabag = require 'pandoc.mediabag' | ||
local path = require 'pandoc.path' | ||
local sha1 = (require 'pandoc.utils').sha1 | ||
|
||
--- Map from old image filepaths to new names. | ||
local updated_filepath = {} | ||
|
||
local function unnest (filepath, contents) | ||
if updated_filepath[filepath] then | ||
return updated_filepath | ||
end | ||
|
||
local newpath = path.filename(filepath) | ||
if updated_filepath[newpath] then | ||
-- the filename is already in use. Prefix with sha1 hash. | ||
newpath = sha1(contents) .. '-' .. filepath | ||
end | ||
updated_filepath[filepath] = newpath | ||
return newpath | ||
end | ||
|
||
function Pandoc (doc) | ||
-- Ensure that all images have been fetched. | ||
doc = mediabag.fill(doc) | ||
local newpath | ||
for fp, mt, contents in mediabag.items() do | ||
-- Delete all mediabag items and re-insert them under their fixed | ||
-- name. | ||
newpath = unnest(fp, contents) | ||
mediabag.delete(fp) | ||
mediabag.insert(newpath, mt, contents) | ||
end | ||
return doc:walk { | ||
Image = function (img) | ||
img.src = updated_filepath[img.src] or img.src | ||
return img | ||
end | ||
} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters