diff options
| author | John MacFarlane <[email protected]> | 2022-11-09 17:36:23 -0800 |
|---|---|---|
| committer | John MacFarlane <[email protected]> | 2022-12-20 20:58:44 -0800 |
| commit | 063480accd3421bfc77478b8dbddc1985553ed75 (patch) | |
| tree | 4ef6d846f17085cbc2f7410123d7698c4196b069 /pandoc-lua-engine/test | |
| parent | 8553459f61eb2290008fafedcfbb1df0bb1fe1b4 (diff) | |
T.P.Scripting: Refactor the scripting engine.
The new type CustomComponents is exported from T.P.Scripting, and the
ScriptEngine fields are changed. Instead of separate fields for custom
readers and writers, we now have a single function that loads any number
of "components" from a script: these may be custom readers, custom
writers, templates for writers, or extension configs. (Note: it's
possible to have a custom reader and a custom writer for a format
together in the same file.)
Pandoc now checks the folder `custom` in the user's data directory for a
matching script if it can't find one in the local directory. Previously,
the `readers` and `writers` data directories were search for custom
readers and writers, respectively. Scripts in those directories must be
moved to the `custom` folder.
Custom readers used to implement a fallback behavior that allowed to
consume just a string value as input to the `Reader` function. This has
been removed, the first argument is now always a list of sources. Use
`tostring` on that argument to get a string.
Closes #8417.
Signed-off-by: Albert Krewinkel <[email protected]>
Diffstat (limited to 'pandoc-lua-engine/test')
| -rw-r--r-- | pandoc-lua-engine/test/Tests/Lua/Reader.hs | 12 | ||||
| -rw-r--r-- | pandoc-lua-engine/test/Tests/Lua/Writer.hs | 16 |
2 files changed, 19 insertions, 9 deletions
diff --git a/pandoc-lua-engine/test/Tests/Lua/Reader.hs b/pandoc-lua-engine/test/Tests/Lua/Reader.hs index 15ad685b4..dbf125432 100644 --- a/pandoc-lua-engine/test/Tests/Lua/Reader.hs +++ b/pandoc-lua-engine/test/Tests/Lua/Reader.hs @@ -9,11 +9,13 @@ Tests for custom Lua readers. -} module Tests.Lua.Reader (tests) where +import Control.Arrow ((>>>)) import Data.Char (chr) import Data.Default (Default (def)) import Text.Pandoc.Class (runIOorExplode) -import Text.Pandoc.Lua (readCustom) -import Text.Pandoc.Readers (Reader (ByteStringReader, TextReader)) +import Text.Pandoc.Lua (loadCustom) +import Text.Pandoc.Readers (Reader (ByteStringReader)) +import Text.Pandoc.Scripting (customReader) import Test.Tasty (TestTree) import Test.Tasty.HUnit ((@?=), testCase) @@ -26,9 +28,9 @@ tests = [ testCase "read binary to code block" $ do input <- BL.readFile "bytestring.bin" doc <- runIOorExplode $ - readCustom "bytestring-reader.lua" >>= \case - (ByteStringReader f, _) -> f def input - (TextReader {}, _) -> error "Expected a bytestring reader" + loadCustom "bytestring-reader.lua" >>= (customReader >>> \case + Just (ByteStringReader f) -> f def input + _ -> error "Expected a bytestring reader") let bytes = mconcat $ map (B.str . T.singleton . chr) [0..255] doc @?= B.doc (B.plain bytes) ] diff --git a/pandoc-lua-engine/test/Tests/Lua/Writer.hs b/pandoc-lua-engine/test/Tests/Lua/Writer.hs index 19db66da0..692c6b83d 100644 --- a/pandoc-lua-engine/test/Tests/Lua/Writer.hs +++ b/pandoc-lua-engine/test/Tests/Lua/Writer.hs @@ -16,9 +16,10 @@ import Text.Pandoc.Class (runIOorExplode, readFileStrict) import Text.Pandoc.Extensions (Extension (..), extensionsFromList) import Text.Pandoc.Format (ExtensionsDiff (..), FlavoredFormat (..), applyExtensionsDiff) -import Text.Pandoc.Lua (writeCustom) +import Text.Pandoc.Lua (loadCustom) import Text.Pandoc.Options (WriterOptions (..)) import Text.Pandoc.Readers (readNative) +import Text.Pandoc.Scripting (CustomComponents (..)) import Text.Pandoc.Writers (Writer (ByteStringWriter, TextWriter)) import Test.Tasty (TestTree) import Test.Tasty.Golden (goldenVsString) @@ -35,9 +36,9 @@ tests = (runIOorExplode $ do source <- UTF8.toText <$> readFileStrict "testsuite.native" doc <- readNative def source - txt <- writeCustom "sample.lua" >>= \case - (TextWriter f, _, _) -> f def doc - _ -> error "Expected a text writer" + txt <- customWriter <$> loadCustom "sample.lua" >>= \case + Just (TextWriter f) -> f def doc + _ -> error "Expected a text writer" pure $ BL.fromStrict (UTF8.fromText txt)) , goldenVsString "tables testsuite" @@ -84,3 +85,10 @@ tests = _ -> error "Expected a text writer" result @?= "smart extension is enabled;\ncitations extension is enabled\n" ] + where + writeCustom fp = do + components <- loadCustom fp + let exts = fromMaybe mempty (customExtensions components) + case customWriter components of + Nothing -> error "Expected a writer to be defined" + Just w -> return (w, exts, customTemplate components) |
