@@ -0,0 +1,108 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
2+{-# LANGUAGE NoImplicitPrelude #-}
3+{-
4+Copyright © 2017-2018 Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
5+
6+This program is free software; you can redistribute it and/or modify
7+it under the terms of the GNU General Public License as published by
8+the Free Software Foundation; either version 2 of the License, or
9+(at your option) any later version.
10+
11+This program is distributed in the hope that it will be useful,
12+but WITHOUT ANY WARRANTY; without even the implied warranty of
13+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+GNU General Public License for more details.
15+
16+You should have received a copy of the GNU General Public License
17+along with this program; if not, write to the Free Software
18+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19+-}
20+{- |
21+ Module : Text.Pandoc.Lua
22+ Copyright : Copyright © 2017-2018 Albert Krewinkel
23+ License : GNU GPL, version 2 or above
24+
25+ Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
26+ Stability : alpha
27+
28+Pandoc's Lua globals.
29+-}
30+module Text.Pandoc.Lua.Global
31+ ( Global (..)
32+ , setGlobals
33+ ) where
34+
35+import Prelude
36+import Data.Data (Data)
37+import Data.Version (Version (versionBranch))
38+import Foreign.Lua (Lua, Peekable, Pushable)
39+import Foreign.Lua.Userdata ( ensureUserdataMetatable, pushAnyWithMetatable
40+ , metatableName)
41+import Paths_pandoc (version)
42+import Text.Pandoc.Class (CommonState)
43+import Text.Pandoc.Definition (Pandoc (Pandoc), pandocTypesVersion)
44+import Text.Pandoc.Lua.StackInstances ()
45+import Text.Pandoc.Lua.Util (addFunction)
46+import Text.Pandoc.Options (ReaderOptions)
47+
48+import qualified Foreign.Lua as Lua
49+
50+-- | Permissible global Lua variables.
51+data Global =
52+ FORMAT String
53+ | PANDOC_API_VERSION
54+ | PANDOC_DOCUMENT Pandoc
55+ | PANDOC_READER_OPTIONS ReaderOptions
56+ | PANDOC_SCRIPT_FILE FilePath
57+ | PANDOC_STATE CommonState
58+ | PANDOC_VERSION
59+ -- Cannot derive instance of Data because of CommonState
60+
61+-- | Set all given globals.
62+setGlobals :: [Global] -> Lua ()
63+setGlobals = mapM_ setGlobal
64+
65+setGlobal :: Global -> Lua ()
66+setGlobal global = case global of
67+ -- This could be simplified if Global was an instance of Data.
68+ FORMAT format -> do
69+ Lua.push format
70+ Lua.setglobal "FORMAT"
71+ PANDOC_API_VERSION -> do
72+ Lua.push (versionBranch pandocTypesVersion)
73+ Lua.setglobal "PANDOC_API_VERSION"
74+ PANDOC_DOCUMENT doc -> do
75+ Lua.push (LazyPandoc doc)
76+ Lua.setglobal "PANDOC_DOCUMENT"
77+ PANDOC_READER_OPTIONS ropts -> do
78+ Lua.push ropts
79+ Lua.setglobal "PANDOC_READER_OPTIONS"
80+ PANDOC_SCRIPT_FILE filePath -> do
81+ Lua.push filePath
82+ Lua.setglobal "PANDOC_SCRIPT_FILE"
83+ PANDOC_STATE commonState -> do
84+ Lua.push commonState
85+ Lua.setglobal "PANDOC_STATE"
86+ PANDOC_VERSION -> do
87+ Lua.push (versionBranch version)
88+ Lua.setglobal "PANDOC_VERSION"
89+
90+-- | Readonly and lazy pandoc objects.
91+newtype LazyPandoc = LazyPandoc Pandoc
92+ deriving (Data)
93+
94+instance Pushable LazyPandoc where
95+ push lazyDoc = pushAnyWithMetatable pushPandocMetatable lazyDoc
96+ where
97+ pushPandocMetatable = ensureUserdataMetatable (metatableName lazyDoc) $
98+ addFunction "__index" indexLazyPandoc
99+
100+instance Peekable LazyPandoc where
101+ peek = Lua.peekAny
102+
103+indexLazyPandoc :: LazyPandoc -> String -> Lua Lua.NumResults
104+indexLazyPandoc (LazyPandoc (Pandoc meta blks)) field = 1 <$
105+ case field of
106+ "blocks" -> Lua.push blks
107+ "meta" -> Lua.push meta
108+ _ -> Lua.pushnil