Skip to content

Commit

Permalink
Use "flat" folder for JATS output
Browse files Browse the repository at this point in the history
The JATS output file is placed in a folder `paper.jats` together with
all media files. A truly flat structure is ensured, i.e., images are in
the same folder as the resulting `paper.jats` file, and never in
subfolders.
  • Loading branch information
tarleb committed May 8, 2024
1 parent a306739 commit 0becb81
Show file tree
Hide file tree
Showing 4 changed files with 56 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
10 changes: 10 additions & 0 deletions scripts/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ for format in $(printf "%s" "$outformats" | sed -e 's/,/ /g'); do
else
logfile=paper.${format}.log
fi
extra_args=
if [ "$format" = "jats" ]; then
[ "$verbosity" -gt 0 ] && printf 'Creating folder "paper.jats"'
mkdir -p "paper.jats"
extra_args="--extract-media=paper.jats --output=paper.jats/paper.jats"
fi
[ "$verbosity" -gt 0 ] && [ -n "$extra_args" ] && \
printf 'Extra pandoc args:%s\n' "${extra_args}"

# Note that the output file must be defined in the format's defaults file.
/usr/local/bin/pandoc \
--data-dir="$OPENJOURNALS_PATH"/data \
Expand All @@ -112,6 +121,7 @@ for format in $(printf "%s" "$outformats" | sed -e 's/,/ /g'); do
--variable=draft:"$draft" \
--metadata=draft:"$draft" \
--log="$logfile" \
${extra_args} \
"$input_file" \
"$@" || exit 1
if [ "$verbosity" -gt 0 ]; then
Expand Down

0 comments on commit 0becb81

Please sign in to comment.