aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlbert Krewinkel <[email protected]>2022-01-01 01:11:41 +0100
committerAlbert Krewinkel <[email protected]>2022-01-01 13:44:13 +0100
commite58a5ceed8bb629efbfd670bf162b65b7853ab7f (patch)
tree9574eb82e140eaa69ff28bc797d52707c8513ac7 /src
parent03054a33e895cacc9b2c74fd6dd5b698c58c3877 (diff)
Lua: marshal ReaderOptions field `extensions`, `track_changes` via JSON
Extensions are now available as a list of strings; the track-changes settings are given as the kebab-case representation used in JSON.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/Lua/Marshal/ReaderOptions.hs15
-rw-r--r--src/Text/Pandoc/Lua/Util.hs21
2 files changed, 29 insertions, 7 deletions
diff --git a/src/Text/Pandoc/Lua/Marshal/ReaderOptions.hs b/src/Text/Pandoc/Lua/Marshal/ReaderOptions.hs
index 225dcf116..5da42fd9e 100644
--- a/src/Text/Pandoc/Lua/Marshal/ReaderOptions.hs
+++ b/src/Text/Pandoc/Lua/Marshal/ReaderOptions.hs
@@ -22,6 +22,7 @@ module Text.Pandoc.Lua.Marshal.ReaderOptions
import Data.Default (def)
import HsLua as Lua
import Text.Pandoc.Lua.Marshal.List (pushPandocList)
+import Text.Pandoc.Lua.Util (peekViaJSON, pushViaJSON)
import Text.Pandoc.Options (ReaderOptions (..))
--
@@ -87,23 +88,23 @@ readerOptionsMembers =
(pushText, readerDefaultImageExtension)
(peekText, \opts x -> opts{ readerDefaultImageExtension = x })
, property "extensions" ""
- (pushString . show, readerExtensions)
- (peekRead, \opts x -> opts{ readerExtensions = x })
+ (pushViaJSON, readerExtensions)
+ (peekViaJSON, \opts x -> opts{ readerExtensions = x })
, property "indented_code_classes" ""
(pushPandocList pushText, readerIndentedCodeClasses)
(peekList peekText, \opts x -> opts{ readerIndentedCodeClasses = x })
- , property "strip_comments" ""
- (pushBool, readerStripComments)
- (peekBool, \opts x -> opts{ readerStripComments = x })
, property "standalone" ""
(pushBool, readerStandalone)
(peekBool, \opts x -> opts{ readerStandalone = x })
+ , property "strip_comments" ""
+ (pushBool, readerStripComments)
+ (peekBool, \opts x -> opts{ readerStripComments = x })
, property "tab_stop" ""
(pushIntegral, readerTabStop)
(peekIntegral, \opts x -> opts{ readerTabStop = x })
, property "track_changes" ""
- (pushString . show, readerTrackChanges)
- (peekRead, \opts x -> opts{ readerTrackChanges = x })
+ (pushViaJSON, readerTrackChanges)
+ (choice [peekRead, peekViaJSON], \opts x -> opts{ readerTrackChanges = x })
]
-- | Retrieves a 'ReaderOptions' object from a table on the stack, using
diff --git a/src/Text/Pandoc/Lua/Util.hs b/src/Text/Pandoc/Lua/Util.hs
index 9c6f42b2b..c663efb6f 100644
--- a/src/Text/Pandoc/Lua/Util.hs
+++ b/src/Text/Pandoc/Lua/Util.hs
@@ -15,11 +15,16 @@ module Text.Pandoc.Lua.Util
, callWithTraceback
, pcallWithTraceback
, dofileWithTraceback
+ , peekViaJSON
+ , pushViaJSON
) where
import Control.Monad (when)
import HsLua
+import HsLua.Aeson (peekValue, pushValue)
+import qualified Data.Aeson as Aeson
import qualified HsLua as Lua
+import qualified Text.Pandoc.UTF8 as UTF8
-- | Add a value to the table at the top of the stack at a string-index.
addField :: (LuaError e, Pushable a) => String -> a -> LuaE e ()
@@ -60,3 +65,19 @@ dofileWithTraceback fp = do
case loadRes of
Lua.OK -> pcallWithTraceback 0 Lua.multret
_ -> return loadRes
+
+
+-- These will become part of hslua-aeson in future versions.
+
+-- | Retrieves a value from the Lua stack via JSON.
+peekViaJSON :: (Aeson.FromJSON a, LuaError e) => Peeker e a
+peekViaJSON idx = do
+ value <- peekValue idx
+ case Aeson.fromJSON value of
+ Aeson.Success x -> pure x
+ Aeson.Error msg -> failPeek $ "failed to decode: " <>
+ UTF8.fromString msg
+
+-- | Pushes a value to the Lua stack as a JSON-like value.
+pushViaJSON :: (Aeson.ToJSON a, LuaError e) => Pusher e a
+pushViaJSON = pushValue . Aeson.toJSON