Skip to content

Commit 0f135b5

Browse files
committed
lilypond: avoid coreutils commands
Files are no longer created in the file system but only added to the mediabag. The files can still be created by running pandoc with the `--extract-media` option. This allows to only depend on lilypond; no other external commands are called. The old behavior could be restored with a separate filter if desired.
1 parent 8b61648 commit 0f135b5

File tree

4 files changed

+18
-58
lines changed

4 files changed

+18
-58
lines changed

lilypond/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/*.png

lilypond/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ DIFF ?= diff --strip-trailing-cr -u
33
test: test-appoggiaturas test-oboe test-code
44

55
test-appoggiaturas:
6-
@{ pandoc --lua-filter=lilypond.lua -t native appoggiaturas.md \
6+
@{ pandoc --lua-filter=lilypond.lua -t native \
7+
--extract-media=. appoggiaturas.md \
78
| $(DIFF) appoggiaturas.native.expected -; } && \
89
test -e beethoven.png
910

1011
test-oboe:
11-
@{ pandoc --lua-filter=lilypond.lua -t native oboe.md \
12+
@{ pandoc --lua-filter=lilypond.lua -t native --extract-media=. oboe.md \
1213
| $(DIFF) oboe.native.expected -; } && \
1314
test -e oboe.png && \
1415
test -e b-flat.png

lilypond/README.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,8 @@ The available (key-value) attributes are:
5555

5656
The available metadata options are:
5757

58-
* `lilypond.image_directory` (string): directory where generated images should
59-
be saved. Will be resolved relative to the output file for this run of pandoc.
60-
Default: `"."`.
61-
* `lilypond.relativize` (boolean): if enabled, use relative paths for the `src`
62-
attributes of generated images.
58+
* `lilypond.image_directory` (string): media sub-directory where
59+
generated images should be stored. Default: `"."`.
6360

6461
The classes and attributes listed above will *not* be copied to the generated
6562
image, but all other classes and attributes will be, and so will the identifier
@@ -73,9 +70,5 @@ The `lilypond` executable must be installed to a location on
7370
your `PATH`. You can obtain it [here](http://lilypond.org/download.html) or
7471
through your package manager.
7572

76-
The filter uses some path-manipulation commands from the GNU coreutils,
77-
specifically `realpath` and `dirname`, so you'll also need those installed.
78-
7973
Finally, because `lilypond.lua` uses functions from the `pandoc.system`
8074
submodule, it requires pandoc version 2.7.3 or later.
81-

lilypond/lilypond.lua

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ else
77
end
88

99
local OPTIONS = {
10-
image_directory = ".",
11-
relativize = false
12-
}
10+
image_directory = ".",
11+
}
1312

1413
local SPECIAL_CLASSES = {
1514
["lilypond"] = true,
@@ -45,30 +44,12 @@ local function wrap_fragment(src)
4544
)
4645
end
4746

48-
local function get_output_directory()
49-
return PANDOC_STATE.output_file
50-
and pandoc.pipe(
51-
"dirname",
52-
{PANDOC_STATE.output_file},
53-
""
54-
):gsub("\n", "")
55-
end
56-
57-
local function resolve_relative_path(what, where)
58-
return pandoc.system.with_working_directory(
59-
where,
60-
function ()
61-
return pandoc.pipe("realpath", {what}, ""):gsub("\n", "")
62-
end
63-
)
64-
end
65-
66-
local function generate_image(name, input, dpi, whither)
47+
local function generate_image(name, input, dpi)
6748
local fullname = name .. ".png"
68-
with_temporary_directory(
49+
return fullname, with_temporary_directory(
6950
"lilypond-lua-XXXXX",
7051
function (tmp_dir)
71-
pandoc.system.with_working_directory(
52+
return pandoc.system.with_working_directory(
7253
tmp_dir,
7354
function ()
7455
pandoc.pipe(
@@ -80,20 +61,14 @@ local function generate_image(name, input, dpi, whither)
8061
},
8162
input
8263
)
83-
pandoc.pipe("cp", {fullname, whither}, "")
64+
local fh = io.open(fullname, 'rb')
65+
local data = fh:read('*all')
66+
fh:close()
67+
return data
8468
end
8569
)
8670
end
8771
)
88-
return whither .. "/" .. fullname
89-
end
90-
91-
local function make_relative_path(to, from)
92-
return pandoc.pipe(
93-
"realpath",
94-
{"--relative-to=" .. from, to},
95-
""
96-
):gsub("\n", "")
9772
end
9873

9974
local function process_lilypond(elem, inline)
@@ -105,18 +80,11 @@ local function process_lilypond(elem, inline)
10580
local dpi = elem.attributes["ly-resolution"]
10681
local name = elem.attributes["ly-name"] or pandoc.sha1(code)
10782

108-
local out_dir = get_output_directory() or "."
109-
local dest = resolve_relative_path(OPTIONS.image_directory, out_dir)
110-
111-
local path = generate_image(name, input, dpi, dest)
112-
local img = io.open(path, "rb")
113-
pandoc.mediabag.insert(path, "image/png", img:read("*a"))
114-
img:close()
83+
local image_filename, image_data = generate_image(name, input, dpi)
84+
local src = OPTIONS.image_directory .. '/' .. image_filename
85+
pandoc.mediabag.insert(src, "image/png", image_data)
11586

11687
local caption = elem.attributes["ly-caption"] or "Musical notation"
117-
local src = OPTIONS.relativize
118-
and make_relative_path(path, out_dir)
119-
or path
12088
-- The "fig:" prefix causes this image to be rendered as a proper figure
12189
-- in HTML ouput (this is a rather ugly pandoc feature and may be replaced
12290
-- by something more elegant in the future).
@@ -154,9 +122,6 @@ local function meta_transformer(md)
154122
OPTIONS.image_directory = dir_conf
155123
and pandoc.utils.stringify(dir_conf)
156124
or OPTIONS.image_directory
157-
OPTIONS.relativize = ly_block.relativize
158-
or OPTIONS.relativize
159-
160125
md.lilypond = nil
161126
return md
162127
end

0 commit comments

Comments
 (0)