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.
This commit is contained in:
Albert Krewinkel 2022-01-01 01:11:41 +01:00
parent 03054a33e8
commit e58a5ceed8
No known key found for this signature in database
GPG key ID: 388DC0B21F631124
4 changed files with 33 additions and 10 deletions

View file

@ -1988,7 +1988,7 @@ Fields:
`extensions`
: string representation of the syntax extensions bit field
(string)
(sequence of strings)
`indented_code_classes`
: default classes for indented code blocks (list of strings)
@ -2006,8 +2006,8 @@ Fields:
(integer)
`track_changes`
: track changes setting for docx; one of `AcceptChanges`,
`RejectChanges`, and `AllChanges` (string)
: track changes setting for docx; one of `accept-changes`,
`reject-changes`, and `all-changes` (string)
## CommonState {#type-commonstate}

View file

@ -474,6 +474,7 @@ library
filepath >= 1.1 && < 1.5,
haddock-library >= 1.10 && < 1.11,
hslua >= 2.0.1 && < 2.1,
hslua-aeson >= 2.0.1 && < 2.1,
hslua-marshalling >= 2.0.1 && < 2.1,
hslua-module-path >= 1.0 && < 1.1,
hslua-module-system >= 1.0 && < 1.1,

View file

@ -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

View file

@ -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