Skip to content

Commit

Permalink
[WIP] Ensure that images are not placed in subfolders
Browse files Browse the repository at this point in the history
  • Loading branch information
tarleb committed Apr 25, 2024
1 parent dc6adfd commit 1c5f66c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
8 changes: 4 additions & 4 deletions data/defaults/jats.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
to: jats_publishing+element_citations
output-file: paper.jats
extract-media: media
extract-media: '.'

filters:
- # Handle `\ref`, `\label`, and `\autoref` commands
Expand All @@ -19,6 +19,6 @@ filters:
type: lua
path: orcid-uri.lua

# This needs more work before we can really enable it.
# - type: lua
# path: extract-images.lua
- # Ensure that images are placed on the top-level, not in subfolders.
type: lua
path: unify-image-paths.lua
39 changes: 39 additions & 0 deletions data/filters/unify-image-paths.lua
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
6 changes: 3 additions & 3 deletions example/paper.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ Images that are larger than the text area are scaled to fit the page. It
can sometimes be useful to give images an explicit height and/or width,
e.g. when adding an image as part of a paragraph. The Markdown `![Nyan
cat](nyan-cat.png){height="9pt"}` includes the image "nyan-cat.png"
![Nyan cat](nyan-cat.png){height="9pt"} while scaling it to a height of
![Nyan cat](images/nyan-cat.png){height="9pt"} while scaling it to a height of
9 pt.


![The "Mandrill" standard test image, sometimes erroneously called
"Baboon", is a popular sample photo and used in image processing
research.](mandrill.jpg){#fig:mandrill}
research.](images/mandrill.jpg){#fig:mandrill}

### Citations

Expand Down Expand Up @@ -378,7 +378,7 @@ with `\ref` and `\autoref`.

![View of coastal dunes in a nature reserve on Sylt, an island in the
North Sea. Sylt (Danish: *Slid*) is Germany's northernmost
island.](sylt.jpg){#sylt width="100%"}
island.](images/sylt.jpg){#sylt width="100%"}

## Tables and figures

Expand Down

0 comments on commit 1c5f66c

Please sign in to comment.