@@ -21,21 +21,24 @@ import Control.Exception
21
21
import Control.Monad ((<=<) )
22
22
import Data.Default (def )
23
23
import Data.Maybe (fromMaybe )
24
+ import Data.Text (Text )
24
25
import HsLua
25
26
import HsLua.Core.Run (newGCManagedState , withGCManagedState )
26
27
import Control.Monad.IO.Class (MonadIO )
27
28
import Text.Pandoc.Class (PandocMonad , findFileWithDataFallback )
28
- import Text.Pandoc.Error (PandocError )
29
+ import Text.Pandoc.Error (PandocError ( .. ) )
29
30
import Text.Pandoc.Format (ExtensionsConfig (.. ))
30
31
import Text.Pandoc.Lua.Global (Global (.. ), setGlobals )
31
32
import Text.Pandoc.Lua.Init (runLuaWith )
32
33
import Text.Pandoc.Lua.Marshal.Format (peekExtensionsConfig )
34
+ import Text.Pandoc.Lua.Marshal.Template (peekTemplate )
35
+ import Text.Pandoc.Templates (Template )
33
36
import Text.Pandoc.Writers (Writer (.. ))
34
37
import qualified Text.Pandoc.Lua.Writer.Classic as Classic
35
38
36
39
-- | Convert Pandoc to custom markup.
37
40
writeCustom :: (PandocMonad m , MonadIO m )
38
- => FilePath -> m (Writer m , ExtensionsConfig )
41
+ => FilePath -> m (Writer m , ExtensionsConfig , m ( Template Text ) )
39
42
writeCustom luaFile = do
40
43
luaState <- liftIO newGCManagedState
41
44
luaFile' <- fromMaybe luaFile <$> findFileWithDataFallback " writers" luaFile
@@ -56,25 +59,39 @@ writeCustom luaFile = do
56
59
pushName x
57
60
rawget (nth 2 ) <* remove (nth 2 ) -- remove global table
58
61
59
- let writerField = " PANDOC Writer function"
62
+ let writerField = " Pandoc Writer function"
60
63
61
64
extsConf <- rawgetglobal " writer_extensions" >>= \ case
62
- TypeNil -> pure $ ExtensionsConfig mempty mempty
65
+ TypeNil -> ExtensionsConfig mempty mempty <$ pop 1
63
66
_ -> forcePeek $ peekExtensionsConfig top `lastly` pop 1
64
67
68
+ -- Store template function in registry
69
+ let templateField = " Pandoc Writer Template"
70
+ rawgetglobal " Template" *> setfield registryindex templateField
71
+
72
+ let getTemplate = liftIO $ withGCManagedState @ PandocError luaState $ do
73
+ getfield registryindex templateField >>= \ case
74
+ TypeNil -> failLua $ " No default template for writer; " <>
75
+ " the global variable Template is undefined."
76
+ _ -> do
77
+ callTrace 0 1
78
+ forcePeek $ peekTemplate top `lastly` pop 1
79
+
80
+ let addProperties = (, extsConf, getTemplate)
81
+
65
82
rawgetglobal " Writer" >>= \ case
66
83
TypeNil -> rawgetglobal " ByteStringWriter" >>= \ case
67
84
TypeNil -> do
68
85
-- Neither `Writer` nor `BinaryWriter` are defined. Try to
69
86
-- use the file as a classic writer.
70
87
pop 1 -- remove nil
71
- pure $ (,extsConf) . TextWriter $ \ opts doc ->
88
+ pure $ addProperties . TextWriter $ \ opts doc ->
72
89
liftIO $ withGCManagedState luaState $ do
73
90
Classic. runCustom @ PandocError opts doc
74
91
_ -> do
75
92
-- Binary writer. Writer function is on top of the stack.
76
93
setfield registryindex writerField
77
- pure $ (,extsConf) . ByteStringWriter $ \ opts doc ->
94
+ pure $ addProperties . ByteStringWriter $ \ opts doc ->
78
95
-- Call writer with document and writer options as arguments.
79
96
liftIO $ withGCManagedState luaState $ do
80
97
getfield registryindex writerField
@@ -85,7 +102,7 @@ writeCustom luaFile = do
85
102
_ -> do
86
103
-- New-type text writer. Writer function is on top of the stack.
87
104
setfield registryindex writerField
88
- pure $ (,extsConf) . TextWriter $ \ opts doc ->
105
+ pure $ addProperties . TextWriter $ \ opts doc ->
89
106
liftIO $ withGCManagedState luaState $ do
90
107
getfield registryindex writerField
91
108
push doc
0 commit comments