aboutsummaryrefslogtreecommitdiff
path: root/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Scaffolding.hs
blob: 8bafe47cb960be954f6b7cee4bab9984ec52e1cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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 <[email protected]>

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