From 2dd1cde7158a032e58618c96543ce7db2fa1aa47 Mon Sep 17 00:00:00 2001 From: Albert Krewinkel <albert@zeitkraut.de> Date: Thu, 30 Dec 2021 16:53:34 +0100 Subject: [PATCH] Lua: allow binary (byte string) readers to be used with `pandoc.read` --- pandoc.cabal | 1 + src/Text/Pandoc/Lua/Module/Pandoc.hs | 14 +++++++------- test/lua/module/pandoc.lua | 8 ++++++++ test/lua/module/tiny.epub | Bin 0 -> 3097 bytes 4 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 test/lua/module/tiny.epub diff --git a/pandoc.cabal b/pandoc.cabal index cd948d253..1c6f3fbd8 100644 --- a/pandoc.cabal +++ b/pandoc.cabal @@ -400,6 +400,7 @@ extra-source-files: test/odt/native/*.native test/lua/*.lua test/lua/module/*.lua + test/lua/module/tiny.epub source-repository head type: git location: git://github.com/jgm/pandoc.git diff --git a/src/Text/Pandoc/Lua/Module/Pandoc.hs b/src/Text/Pandoc/Lua/Module/Pandoc.hs index 20c2f5af5..d2d74aaa8 100644 --- a/src/Text/Pandoc/Lua/Module/Pandoc.hs +++ b/src/Text/Pandoc/Lua/Module/Pandoc.hs @@ -21,7 +21,6 @@ module Text.Pandoc.Lua.Module.Pandoc import Prelude hiding (read) import Control.Monad (forM_, when) import Control.Monad.Catch (catch, throwM) -import Control.Monad.Except (throwError) import Data.Data (Data, dataTypeConstrs, dataTypeOf, showConstr) import Data.Default (Default (..)) import Data.Maybe (fromMaybe) @@ -47,6 +46,7 @@ import qualified Data.ByteString.Lazy as BL import qualified Data.ByteString.Lazy.Char8 as BSL import qualified Data.Text as T import qualified Text.Pandoc.Lua.Util as LuaUtil +import qualified Text.Pandoc.UTF8 as UTF8 import Text.Pandoc.Error -- | Push the "pandoc" package to the Lua stack. Requires the `List` @@ -170,12 +170,12 @@ functions = , defun "read" ### (\content mformatspec mreaderOptions -> do let formatSpec = fromMaybe "markdown" mformatspec - readerOptions = fromMaybe def mreaderOptions + readerOpts = fromMaybe def mreaderOptions res <- Lua.liftIO . runIO $ getReader formatSpec >>= \case - (TextReader r, es) -> r readerOptions{ readerExtensions = es } - content - _ -> throwError $ PandocSomeError - "Only textual formats are supported" + (TextReader r, es) -> + r readerOpts{ readerExtensions = es } (UTF8.toText content) + (ByteStringReader r, es) -> + r readerOpts{ readerExtensions = es } (BSL.fromStrict content) case res of Right pd -> return pd -- success, got a Pandoc document Left (PandocUnknownReaderError f) -> @@ -185,7 +185,7 @@ functions = "Extension " <> e <> " not supported for " <> f Left e -> throwM e) - <#> parameter peekText "string" "content" "text to parse" + <#> parameter peekByteString "string" "content" "text to parse" <#> optionalParameter peekText "string" "formatspec" "format and extensions" <#> optionalParameter peekReaderOptions "ReaderOptions" "reader_options" "reader options" diff --git a/test/lua/module/pandoc.lua b/test/lua/module/pandoc.lua index 892ffee03..397182438 100644 --- a/test/lua/module/pandoc.lua +++ b/test/lua/module/pandoc.lua @@ -279,6 +279,14 @@ return { pandoc.read(indented_code, 'markdown', {indented_code_classes={'foo'}}) ) end), + test('can read epub', function () + local epub = io.open('lua/module/tiny.epub', 'rb') + local blocks = pandoc.read(epub:read'a', 'epub').blocks + assert.are_equal( + blocks[#blocks], + pandoc.Para { pandoc.Emph 'EPUB' } + ) + end), test('failing read', function () assert.error_matches( function () pandoc.read('foo', 'nosuchreader') end, diff --git a/test/lua/module/tiny.epub b/test/lua/module/tiny.epub new file mode 100644 index 0000000000000000000000000000000000000000..9e92202b7cbbc1e28d0409956a9c88de38a75c7c GIT binary patch literal 3097 zcmdT`O>f*p7~VD%XoP5yDkP9V#vFrp_JeG9jn`EvsRX5ErECr$A(*wtS*!lS9w(bh zNc11@2k41YkvR1R5?nxBxghZmxN+f9ArQ|y_S#Nxq;O;-$79d?;rE&6d1q#~HlEn* z(C_2V9_+^atJkhzOxM%6LO&GpC=onK0^i}%k0U0Me6e-UPiB?p-G>i$VO;$#V6-#a zdy~~B@4)2K=<(;TZge_yU0$0waU?|~yK&-K*H&Sm9f&kTnyKFJ_H<1|PVD;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