From 12cc140272a0f2752584428ab765fb6410f29b92 Mon Sep 17 00:00:00 2001 From: Albert Krewinkel <albert@zeitkraut.de> Date: Tue, 12 Jul 2022 20:14:42 +0200 Subject: [PATCH] Lua: add fields `pandoc.readers` and `pandoc.writers`. (#8177) The set of supported input and output formats is made available to Lua users. --- doc/lua-filters.md | 14 +++++++++++- src/Text/Pandoc/Lua/Module/Pandoc.hs | 32 +++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/doc/lua-filters.md b/doc/lua-filters.md index 6a302622d..88bcf7043 100644 --- a/doc/lua-filters.md +++ b/doc/lua-filters.md @@ -2437,10 +2437,22 @@ indexing rules. # Module pandoc -Lua functions for pandoc scripts; includes constructors for +Fields and functions for pandoc scripts; includes constructors for document tree elements, functions to parse text in a given format, and functions to filter and modify a subtree. +## Static Fields {#pandoc.fields} + +### readers {#pandoc.readers} + +Set of formats that pandoc can parse. All keys in this table can +be used as the `format` value in `pandoc.read`. + +### writers {#pandoc.writers} + +Set of formats that pandoc can generate. All keys in this table +can be used as the `format` value in `pandoc.write`. + ## Pandoc ### `Pandoc (blocks[, meta])` {#pandoc.pandoc} diff --git a/src/Text/Pandoc/Lua/Module/Pandoc.hs b/src/Text/Pandoc/Lua/Module/Pandoc.hs index 3dacc48de..e708f4345 100644 --- a/src/Text/Pandoc/Lua/Module/Pandoc.hs +++ b/src/Text/Pandoc/Lua/Module/Pandoc.hs @@ -43,13 +43,14 @@ import Text.Pandoc.Lua.PandocLua (PandocLua (unPandocLua), liftPandocLua) import Text.Pandoc.Options ( ReaderOptions (readerExtensions) , WriterOptions (writerExtensions) ) import Text.Pandoc.Process (pipeProcess) -import Text.Pandoc.Readers (Reader (..), getReader) +import Text.Pandoc.Readers (Reader (..), getReader, readers) import Text.Pandoc.Sources (toSources) -import Text.Pandoc.Writers (Writer (..), getWriter) +import Text.Pandoc.Writers (Writer (..), getWriter, writers) import qualified HsLua as Lua import qualified Data.ByteString.Lazy as BL import qualified Data.ByteString.Lazy.Char8 as BSL +import qualified Data.Set as Set import qualified Data.Text as T import qualified Text.Pandoc.UTF8 as UTF8 @@ -68,7 +69,8 @@ documentedModule = Module , "document elements, functions to parse text in a given" , "format, and functions to filter and modify a subtree." ] - , moduleFields = stringConstants ++ [inlineField, blockField] + , moduleFields = readersField : writersField : + stringConstants ++ [inlineField, blockField] , moduleOperations = [] , moduleFunctions = mconcat [ functions @@ -79,6 +81,30 @@ documentedModule = Module ] } +-- | Set of input formats accepted by @read@. +readersField :: Field PandocError +readersField = Field + { fieldName = "readers" + , fieldDescription = T.unlines + [ "Set of formats that pandoc can parse. All keys in this table can" + , "be used as the `format` value in `pandoc.read`." + ] + , fieldPushValue = pushSet pushText $ + Set.fromList (map fst (readers @PandocLua)) + } + +-- | Set of input formats accepted by @write@. +writersField :: Field PandocError +writersField = Field + { fieldName = "writers" + , fieldDescription = T.unlines + [ "Set of formats that pandoc can generate. All keys in this table" + , "can be used as the `format` value in `pandoc.write`." + ] + , fieldPushValue = pushSet pushText $ + Set.fromList (map fst (writers @PandocLua)) + } + -- | Inline table field inlineField :: Field PandocError inlineField = Field