diff options
| author | Albert Krewinkel <[email protected]> | 2025-05-28 11:10:51 +0200 |
|---|---|---|
| committer | Albert Krewinkel <[email protected]> | 2025-05-28 13:19:01 +0200 |
| commit | ce5e2a2efc0823833040f917d4470da82b634c13 (patch) | |
| tree | 060834c00de6412a0ea69b8fdc3f89939aec520d | |
| parent | ccc0a5a630df414a580c5c58f1b0ad8133e1a398 (diff) | |
Lua: add function `pandoc.mediabag.make_data_uri`.
The function takes a MIME type and raw data from which it creates an
RFC 2397 data URI.
Closes: #10876
| -rw-r--r-- | doc/lua-filters.md | 32 | ||||
| -rw-r--r-- | pandoc-lua-engine/src/Text/Pandoc/Lua/Module/MediaBag.hs | 25 | ||||
| -rw-r--r-- | pandoc-lua-engine/test/lua/module/pandoc-mediabag.lua | 11 |
3 files changed, 68 insertions, 0 deletions
diff --git a/doc/lua-filters.md b/doc/lua-filters.md index 9a29732d9..b2ad38ad5 100644 --- a/doc/lua-filters.md +++ b/doc/lua-filters.md @@ -4532,6 +4532,38 @@ Returns: *Since: 2.0* +### make_data_uri {#pandoc.mediabag.make_data_uri} + +`make_data_uri (mime_type, raw_data)` + +Convert the input data into a data URI as defined by RFC 2397. + +Example: + + -- Embed an unofficial pandoc logo + local pandoc_logo_url = 'https://raw.githubusercontent.com/' + .. 'tarleb/pandoc-logo/main/pandoc.svg' + + local datauri = pandoc.mediabag.make_data_uri( + pandoc.mediabag.fetch(pandoc_logo_url) + ) + + local image = pandoc.Image('Logo', datauri) + +Parameters: + +`mime_type` +: MIME type of the data (string) + +`raw_data` +: data to encode (string) + +Returns: + +- data uri (string) + +*Since: 3.7.1* + ### write {#pandoc.mediabag.write} `write (dir[, fp])` diff --git a/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/MediaBag.hs b/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/MediaBag.hs index 44957e246..da666779a 100644 --- a/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/MediaBag.hs +++ b/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/MediaBag.hs @@ -27,6 +27,7 @@ import Text.Pandoc.Lua.Marshal.List (pushPandocList) import Text.Pandoc.Lua.Orphans () import Text.Pandoc.Lua.PandocLua (unPandocLua) import Text.Pandoc.MIME (MimeType) +import Text.Pandoc.SelfContained (makeDataURI) import qualified Data.ByteString.Lazy as BL import qualified Data.Text as T @@ -60,6 +61,7 @@ documentedModule = Module , items `since` makeVersion [2,7,3] , list `since` makeVersion [2,0] , lookup `since` makeVersion [2,0] + , make_data_uri `since` makeVersion [3,7,1] , write `since` makeVersion [3,0] ] , moduleOperations = [] @@ -243,6 +245,29 @@ fetch = defun "fetch" , " local mt, contents = pandoc.mediabag.fetch(diagram_url)" ] +make_data_uri :: DocumentedFunction PandocError +make_data_uri = defun "make_data_uri" + ### (\mime raw -> pure $ makeDataURI (mime, raw)) + <#> parameter Lua.peekText "string" "mime_type" "MIME type of the data" + <#> parameter Lua.peekByteString "string" "raw_data" "data to encode" + =#> functionResult Lua.pushText "string" "data uri" + #? T.unlines + [ "Convert the input data into a data URI as defined by RFC 2397." + , "" + , "Example:" + , "" + , " -- Embed an unofficial pandoc logo" + , " local pandoc_logo_url = 'https://raw.githubusercontent.com/'" + , " .. 'tarleb/pandoc-logo/main/pandoc.svg'" + , "" + , " local datauri = pandoc.mediabag.make_data_uri(" + , " pandoc.mediabag.fetch(pandoc_logo_url)" + , " )" + , "" + , " local image = pandoc.Image('Logo', datauri)" + ] + + -- | Extract the mediabag or just a single entry. write :: DocumentedFunction PandocError write = defun "write" diff --git a/pandoc-lua-engine/test/lua/module/pandoc-mediabag.lua b/pandoc-lua-engine/test/lua/module/pandoc-mediabag.lua index f001bf8db..39653cccc 100644 --- a/pandoc-lua-engine/test/lua/module/pandoc-mediabag.lua +++ b/pandoc-lua-engine/test/lua/module/pandoc-mediabag.lua @@ -104,4 +104,15 @@ return { end), }, + group 'make_data_uri' { + test('returns a data URI', function () + local uri = mediabag.make_data_uri('text/plain', 'foo') + assert.are_equal(uri:sub(1,5), 'data:') + end), + test('URI specifies the given MIME type', function () + local mimetype = 'text/plain' + local uri = mediabag.make_data_uri(mimetype, 'foo') + assert.are_equal(uri:sub(6, 5 + #mimetype), mimetype) + end), + } } |
