Lua: add function pandoc.format.extensions.

This simplifies the creation of custom readers and writers that are
based on built-in formats.
This commit is contained in:
Albert Krewinkel
2023-01-18 08:59:53 -08:00
committed by John MacFarlane
parent 671cfcd869
commit 3a0fc17880
4 changed files with 72 additions and 4 deletions
+21
View File
@@ -4441,6 +4441,27 @@ Returns:
- default extensions enabled for `format` (FormatExtensions)
### extensions {#pandoc.format.extensions}
Returns the extension configuration for the given format.
The configuration is represented as a table with all supported
extensions as keys and their default status as value, with
`true` indicating that the extension is enabled by default,
while `false` marks a supported extension that's disabled.
This function can be used to assign a value to the `Extensions`
global in custom readers and writers.
Parameters:
`format`
: format identifier (string)
Returns:
- extensions config
# Module pandoc.path
Module for file path manipulations.
@@ -1,5 +1,6 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{- |
Module : Text.Pandoc.Lua.Marshaling.Format
@@ -14,6 +15,7 @@ module Text.Pandoc.Lua.Marshal.Format
( peekExtensions
, pushExtensions
, peekExtensionsConfig
, pushExtensionsConfig
, peekFlavoredFormat
) where
@@ -23,20 +25,25 @@ import Data.Maybe (fromMaybe)
import HsLua
import Text.Pandoc.Error (PandocError (..))
import Text.Pandoc.Extensions
( Extension, Extensions, extensionsFromList
, getDefaultExtensions, readExtension )
( Extension, Extensions, extensionsFromList, extensionsToList
, getDefaultExtensions, readExtension, showExtension )
import Text.Pandoc.Format
( ExtensionsConfig (..), ExtensionsDiff (..), FlavoredFormat (..)
, diffExtensions, parseFlavoredFormat)
import Text.Pandoc.Lua.PandocLua (PandocLua (unPandocLua))
-- | Retrieves an 'Extensions' set from the Lua stack.
-- | Retrieves a single 'Extension' from the Lua stack.
peekExtension :: LuaError e => Peeker e Extension
peekExtension idx = do
extString <- peekString idx
return $ readExtension extString
{-# INLINE peekExtension #-}
-- | Pushes an individual 'Extension' to the Lua stack.
pushExtension :: LuaError e => Pusher e Extension
pushExtension = pushText . showExtension
{-# INLINE pushExtension #-}
-- | Retrieves an 'Extensions' set from the Lua stack.
peekExtensions :: LuaError e => Peeker e Extensions
peekExtensions = fmap extensionsFromList . peekList peekExtension
@@ -62,6 +69,14 @@ peekExtensionsConfig idx = do
, extsSupported = extsToEnable diff <> extsToDisable diff
}
-- | Pushes an 'ExtensionsConfig' value as a table with that maps
-- extensions to their default enabled/disabled status.
pushExtensionsConfig :: LuaError e => Pusher e ExtensionsConfig
pushExtensionsConfig (ExtensionsConfig def supported) =
pushKeyValuePairs pushExtension pushBool $
map (,False) (extensionsToList supported) ++
map (,True) (extensionsToList def)
instance Peekable ExtensionsConfig where
safepeek = peekExtensionsConfig
@@ -14,7 +14,8 @@ module Text.Pandoc.Lua.Module.Format
import HsLua
import Text.Pandoc.Error (PandocError)
import Text.Pandoc.Extensions (getAllExtensions, getDefaultExtensions)
import Text.Pandoc.Lua.Marshal.Format (pushExtensions)
import Text.Pandoc.Format (getExtensionsConfig)
import Text.Pandoc.Lua.Marshal.Format (pushExtensions, pushExtensionsConfig)
import Text.Pandoc.Lua.PandocLua ()
import qualified Data.Text as T
@@ -56,4 +57,19 @@ functions =
, "can have an effect when reading a format but not when"
, "writing it, or *vice versa*."
]
, defun "extensions"
### liftPure getExtensionsConfig
<#> textParam "format" "format identifier"
=#> functionResult pushExtensionsConfig "table" "extensions config"
#? T.unlines
[ "Returns the extension configuration for the given format."
, "The configuration is represented as a table with all supported"
, "extensions as keys and their default status as value, with"
, "`true` indicating that the extension is enabled by default,"
, "while `false` marks a supported extension that's disabled."
, ""
, "This function can be used to assign a value to the `Extensions`"
, "global in custom readers and writers."
]
]
@@ -31,4 +31,20 @@ return {
assert.are_same(format.all_extensions('docx'), docx_default_exts)
end),
},
group 'extensions' {
test('org', function ()
local org_default_exts = {
ascii_identifiers = false,
auto_identifiers = true,
citations = true,
east_asian_line_breaks = false,
fancy_lists = false,
gfm_auto_identifiers = false,
smart = false,
task_lists = true,
}
assert.are_same(format.extensions 'org', org_default_exts)
end),
},
}