    {-# LANGUAGE OverloadedStrings #-}
    {- |
       Module      : Text.Pandoc.Lua.Module.Scaffolding
       Copyright   : Copyright © 2022-2026 Albert Krewinkel, John MacFarlane
       License     : GNU GPL, version 2 or above
       Maintainer  : Albert Krewinkel <albert+pandoc@tarleb.com>

    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 PandocError
    documentedModule = Name -> Module PandocError
    forall e. Name -> Module e
    defmodule Name
    "pandoc.scaffolding"
      Module PandocError -> Text -> Module PandocError
    forall a. HasDescription a => a -> Text -> a
    `withDescription` Text
    "Scaffolding for custom writers."
      Module PandocError -> [Field PandocError] -> Module PandocError
    forall e. Module e -> [Field e] -> Module e
    `withFields` [Field PandocError
    writerScaffolding]

    -- | Template module functions.
    writerScaffolding :: Field PandocError
    writerScaffolding :: Field PandocError
    writerScaffolding = Name -> Field PandocError
    forall e. Name -> Field e
    deffield Name
    "Writer"
      Field PandocError -> TypeSpec -> Field PandocError
    forall e. Field e -> TypeSpec -> Field e
    `withType` TypeSpec
    "table"
      Field PandocError -> Text -> Field PandocError
    forall a. HasDescription a => a -> Text -> a
    `withDescription` [Text] -> Text
    T.unlines
        [ Text
    "An object to be used as a `Writer` function; the construct handles"
        , Text
    "most of the boilerplate, expecting only render functions for all"
        , Text
    "AST elements"
        ]
      Field PandocError -> LuaE PandocError () -> Field PandocError
    forall e. Field e -> LuaE e () -> Field e
    `withValue` do
          LuaE PandocError NumResults
    pushWriterScaffolding
          -- pretend that it's a submodule so we get better error messages
          StackIndex -> Name -> LuaE PandocError Type
    forall e. LuaError e => StackIndex -> Name -> LuaE e Type
    getfield StackIndex
    registryindex Name
    loaded
          StackIndex -> LuaE PandocError ()
    forall e. StackIndex -> LuaE e ()
    pushvalue (CInt -> StackIndex
    nth CInt
    2)
          StackIndex -> Name -> LuaE PandocError ()
    forall e. LuaError e => StackIndex -> Name -> LuaE e ()
    setfield (CInt -> StackIndex
    nth CInt
    2) (Name -> Name
    submod Name
    "Writer")
          -- same for fields "Block" and "Inline"
          StackIndex -> Name -> LuaE PandocError Type
    forall e. LuaError e => StackIndex -> Name -> LuaE e Type
    getfield (CInt -> StackIndex
    nth CInt
    2) Name
    "Inline" LuaE PandocError Type -> LuaE PandocError () -> LuaE PandocError ()
    forall a b.
    LuaE PandocError a -> LuaE PandocError b -> LuaE PandocError b
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> StackIndex -> Name -> LuaE PandocError ()
    forall e. LuaError e => StackIndex -> Name -> LuaE e ()
    setfield (CInt -> StackIndex
    nth CInt
    2) (Name -> Name
    submod Name
    "Writer.Inline")
          StackIndex -> Name -> LuaE PandocError Type
    forall e. LuaError e => StackIndex -> Name -> LuaE e Type
    getfield (CInt -> StackIndex
    nth CInt
    2) Name
    "Block" LuaE PandocError Type -> LuaE PandocError () -> LuaE PandocError ()
    forall a b.
    LuaE PandocError a -> LuaE PandocError b -> LuaE PandocError b
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> StackIndex -> Name -> LuaE PandocError ()
    forall e. LuaError e => StackIndex -> Name -> LuaE e ()
    setfield (CInt -> StackIndex
    nth CInt
    2) (Name -> Name
    submod Name
    "Writer.Block")

          Int -> LuaE PandocError ()
    forall e. Int -> LuaE e ()
    pop Int
    1 -- remove "LOADED_TABLE"
     where submod :: Name -> Name
    submod Name
    x = Module PandocError -> Name
    forall e. Module e -> Name
    moduleName Module PandocError
    documentedModule Name -> Name -> Name
    forall a. Semigroup a => a -> a -> a
    <> Name
    "." Name -> Name -> Name
    forall a. Semigroup a => a -> a -> a
    <> Name
    x