git.iko.soy/typeable/pandoc
on master help sign in
Added abc.lua example to lua-filters documentation.

Also fixed docs for `pipe`.

commitd4f48e772dfb6fcf4e4ff8c972179ca29c0aeb77 authorJohn MacFarlane <jgm@berkeley.edu> dateTue 3 Oct 2017, 14:20 (9 years ago) parentc936714
1 file changed+61−14
doc/lua-filters.md+61−14
doc/lua-filters.md+61−14
@@ -36,7 +36,7 @@ JSON to stdout and reading it from stdin.
3636Here is an example of a lua filter that converts strong emphasis
3737to small caps:
3838
39−``` {.lua}
39+``` lua
4040return {
4141 {
4242 Strong = function (elem)
@@ -48,7 +48,7 @@ return {
4848
4949or equivalently,
5050
51−``` {.lua}
51+``` lua
5252function Strong(elem)
5353 return pandoc.SmallCaps(elem.c)
5454end
@@ -163,10 +163,15 @@ those elements accessible through the filter function parameter.
163163
164164## Exposed pandoc functionality
165165
166−Some filters will require access to certain functions provided
167−by pandoc. This is currently limited to the `read` function
168−which allows to parse strings into pandoc documents from within
169−the lua filter.
166+Some pandoc functions have been made available in lua:
167+
168+- `read` allows filters to parse strings into pandoc documents
169+- `pipe` runs an external command with input from and output to
170+ strings
171+- `sha1` generates a SHA1 hash
172+- The `mediabag` module allows access to the "mediabag,"
173+ which stores binary content such as images that may be
174+ included in the final document.
170175
171176# Examples
172177
@@ -175,7 +180,7 @@ the lua filter.
175180The following filter converts the string `{{helloworld}}` into
176181emphasized text "Hello, World".
177182
178−``` {.lua}
183+``` lua
179184return {
180185 {
181186 Str = function (elem)
@@ -195,7 +200,7 @@ This filter causes metadata defined in an external file
195200(`metadata-file.yaml`) to be used as default values in a
196201document's metadata:
197202
198−``` {.lua}
203+``` lua
199204-- read metadata file into string
200205local metafile = io.open('metadata-file.yaml', 'r')
201206local content = metafile:read("*a")
@@ -222,7 +227,7 @@ return {
222227This filter sets the date in the document's metadata to the
223228current date:
224229
225−``` {.lua}
230+``` lua
226231function Meta(m)
227232 m.date = os.date("%B %e, %Y")
228233 return m
@@ -234,7 +239,7 @@ end
234239This filter prints a table of all the URLs linked to in the
235240document, together with the number of links to that URL.
236241
237−``` {.lua}
242+``` lua
238243links = {}
239244
240245function Link (el)
@@ -275,7 +280,7 @@ Passing information from a higher level (e.g., metadata) to a
275280lower level (e.g., inlines) is still possible by using two
276281filters living in the same file:
277282
278−``` {.lua}
283+``` lua
279284local vars = {}
280285
281286function get_vars (meta)
@@ -328,6 +333,49 @@ will output:
328333</dl>
329334```
330335
336+## Converting ABC code to music notation
337+
338+This filter replaces code blocks with class `abc` with
339+images created by running their contents through `abcm2ps`
340+and ImageMagick's `convert`. (For more on ABC notation, see
341+<http://abcnotation.com>.)
342+
343+Images are added to the mediabag. For output to binary
344+formats, pandoc will use images in the mediabag. For textual
345+formats, use `--extract-media` to specify a directory where
346+the files in the mediabag will be written, or (for HTML only)
347+use `--self-contained`.
348+
349+``` lua
350+-- Pandoc filter to process code blocks with class "abc" containing
351+-- ABC notation into images.
352+--
353+-- * Assumes that abcm2ps and ImageMagick's convert are in the path.
354+-- * For textual output formats, use --extract-media=abc-images
355+-- * For HTML formats, you may alternatively use --self-contained
356+
357+local filetypes = { html = {"png", "image/png"}
358+ , latex = {"pdf", "application/pdf"}
359+ }
360+local filetype = filetypes[FORMAT][1] or "png"
361+local mimetype = filetypes[FORMAT][2] or "image/png"
362+
363+local function abc2eps(abc, filetype)
364+ local eps = pandoc.pipe("abcm2ps", {"-q", "-O", "-", "-"}, abc)
365+ local final = pandoc.pipe("convert", {"-", filetype .. ":-"}, eps)
366+ return final
367+end
368+
369+function CodeBlock(block)
370+ if block.classes[1] == "abc" then
371+ local img = abc2eps(block.text, filetype)
372+ local fname = pandoc.sha1(img) .. "." .. filetype
373+ pandoc.mediabag.insert(fname, mimetype, img)
374+ return pandoc.Para{ pandoc.Image({pandoc.Str("abc tune")}, fname) }
375+ end
376+end
377+```
378+
331379# Module pandoc
332380
333381Lua functions for pandoc scripts.
@@ -1078,16 +1126,15 @@ Lua functions for pandoc scripts.
10781126[`pipe (command, args, input)`]{#mediabag-sha1}
10791127
10801128: Runs command with arguments, passing it some input,
1081− and returns the exit code and the output.
1129+ and returns the output.
10821130
10831131 Returns:
10841132
1085− - Exit code from command.
10861133 - Output of command.
10871134
10881135 Usage:
10891136
1090− local ec, output = pandoc.pipe("sed", {"-e","s/a/b/"}, "abc")
1137+ local output = pandoc.pipe("sed", {"-e","s/a/b/"}, "abc")
10911138
10921139
10931140# Submodule mediabag