From e243cd696f97410ba7d25ffd2800936c16d160bf Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Fri, 21 Oct 2022 19:11:45 +0200 Subject: Lua: add pandoc.scaffolding.Writer (#8377) This can be used to reduce boilerplate in custom writers. --- .../src/Text/Pandoc/Lua/Module/Scaffolding.hs | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Scaffolding.hs (limited to 'pandoc-lua-engine/src/Text/Pandoc/Lua/Module') diff --git a/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Scaffolding.hs b/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Scaffolding.hs new file mode 100644 index 000000000..8bafe47cb --- /dev/null +++ b/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Scaffolding.hs @@ -0,0 +1,52 @@ +{-# LANGUAGE OverloadedStrings #-} +{- | + Module : Text.Pandoc.Lua.Module.Scaffolding + Copyright : Copyright © 2022 Albert Krewinkel, John MacFarlane + License : GNU GPL, version 2 or above + Maintainer : Albert Krewinkel + +Scaffolding for custom Writers. +-} +module Text.Pandoc.Lua.Module.Scaffolding + ( documentedModule + ) where + +import HsLua +import Text.Pandoc.Error (PandocError) +import Text.Pandoc.Lua.Writer.Scaffolding (pushWriterScaffolding) +import qualified Data.Text as T + +-- | The "pandoc.template" module. +documentedModule :: Module PandocError +documentedModule = Module + { moduleName = "pandoc.scaffolding" + , moduleDescription = T.unlines + [ "Scaffolding for custom writers." + ] + , moduleFields = [writerScaffolding] + , moduleOperations = [] + , moduleFunctions = [] + } + +-- | Template module functions. +writerScaffolding :: Field PandocError +writerScaffolding = Field + { fieldName = "Writer" + , fieldDescription = T.unlines + [ "An object to be used as a `Writer` function; the construct handles" + , "most of the boilerplate, expecting only render functions for all" + , "AST elements" + ] + , fieldPushValue = do + pushWriterScaffolding + -- pretend that it's a submodule so we get better error messages + getfield registryindex loaded + pushvalue (nth 2) + setfield (nth 2) (submod "Writer") + -- same for fields "Block" and "Inline" + getfield (nth 2) "Inline" *> setfield (nth 2) (submod "Writer.Inline") + getfield (nth 2) "Block" *> setfield (nth 2) (submod "Writer.Block") + + pop 1 -- remove "LOADED_TABLE" + } + where submod x = moduleName documentedModule <> "." <> x -- cgit v1.2.3