git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #4153 from tarleb/unify-lua-init
52a8116John MacFarlane
pandoc.cabal+2−1
@@ -523,9 +523,10 @@ library
523 Text.Pandoc.Readers.Org.ParserState,
523 Text.Pandoc.Readers.Org.ParserState,
524 Text.Pandoc.Readers.Org.Parsing,
524 Text.Pandoc.Readers.Org.Parsing,
525 Text.Pandoc.Readers.Org.Shared,
525 Text.Pandoc.Readers.Org.Shared,
526+ Text.Pandoc.Lua.Filter,
527+ Text.Pandoc.Lua.Init,
526 Text.Pandoc.Lua.Packages,
528 Text.Pandoc.Lua.Packages,
527 Text.Pandoc.Lua.PandocModule,
529 Text.Pandoc.Lua.PandocModule,
528− Text.Pandoc.Lua.Filter,
529 Text.Pandoc.Lua.StackInstances,
530 Text.Pandoc.Lua.StackInstances,
530 Text.Pandoc.Lua.Util,
531 Text.Pandoc.Lua.Util,
531 Text.Pandoc.CSS,
532 Text.Pandoc.CSS,
src/Text/Pandoc/App.hs+2−2
@@ -223,7 +223,7 @@ convertWithOpts opts = do
223 if ".lua" `isSuffixOf` format
223 if ".lua" `isSuffixOf` format
224 -- note: use non-lowercased version writerName
224 -- note: use non-lowercased version writerName
225 then return (TextWriter
225 then return (TextWriter
226− (\o d -> liftIO $ writeCustom writerName o d)
226+ (\o d -> writeCustom writerName o d)
227 :: Writer PandocIO, mempty)
227 :: Writer PandocIO, mempty)
228 else case getWriter writerName of
228 else case getWriter writerName of
229 Left e -> E.throwIO $ PandocAppError $
229 Left e -> E.throwIO $ PandocAppError $
@@ -846,7 +846,7 @@ applyLuaFilters :: Maybe FilePath -> [FilePath] -> String -> Pandoc
846applyLuaFilters mbDatadir filters format d = do
846applyLuaFilters mbDatadir filters format d = do
847 expandedFilters <- mapM (expandFilterPath mbDatadir) filters
847 expandedFilters <- mapM (expandFilterPath mbDatadir) filters
848 let go f d' = do
848 let go f d' = do
849− res <- runLuaFilter mbDatadir f format d'
849+ res <- runLuaFilter f format d'
850 case res of
850 case res of
851 Right x -> return x
851 Right x -> return x
852 Left (LuaException s) -> E.throw (PandocFilterError f s)
852 Left (LuaException s) -> E.throw (PandocFilterError f s)
src/Text/Pandoc/Lua.hs+14−53
@@ -1,9 +1,3 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE FlexibleContexts #-}
3−{-# LANGUAGE FlexibleInstances #-}
4−{-# LANGUAGE MultiParamTypeClasses #-}
5−{-# LANGUAGE ScopedTypeVariables #-}
6−{-# OPTIONS_GHC -fno-warn-orphans #-}
7{-
1{-
8Copyright © 2017 Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
2Copyright © 2017 Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
9
3
@@ -29,48 +23,36 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
23 Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
30 Stability : alpha
24 Stability : alpha
31
25
32−Pandoc lua utils.
26+Running pandoc Lua filters.
33-}
27-}
34module Text.Pandoc.Lua
28module Text.Pandoc.Lua
35 ( LuaException (..)
29 ( LuaException (..)
36− , LuaPackageParams (..)
37− , pushPandocModule
38 , runLuaFilter
30 , runLuaFilter
39− , initLuaState
31+ , runPandocLua
40− , luaPackageParams
32+ , pushPandocModule
41 ) where
33 ) where
42
34
43import Control.Monad (when, (>=>))
35import Control.Monad (when, (>=>))
44−import Control.Monad.Identity (Identity)
45−import Control.Monad.Trans (MonadIO (..))
46−import Data.IORef (newIORef, readIORef)
47import Foreign.Lua (FromLuaStack (peek), Lua, LuaException (..),
36import Foreign.Lua (FromLuaStack (peek), Lua, LuaException (..),
48 Status (OK), ToLuaStack (push))
37 Status (OK), ToLuaStack (push))
49−import Text.Pandoc.Class (PandocIO, getCommonState, getMediaBag, setMediaBag)
38+import Text.Pandoc.Class (PandocIO)
50−import Text.Pandoc.Definition
39+import Text.Pandoc.Definition (Pandoc)
51import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter)
40import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter)
52−import Text.Pandoc.Lua.Packages (LuaPackageParams (..),
41+import Text.Pandoc.Lua.Init (runPandocLua)
53− installPandocPackageSearcher)
54import Text.Pandoc.Lua.PandocModule (pushPandocModule) -- TODO: remove
42import Text.Pandoc.Lua.PandocModule (pushPandocModule) -- TODO: remove
55−import Text.Pandoc.Lua.Util (loadScriptFromDataDir)
56import qualified Foreign.Lua as Lua
43import qualified Foreign.Lua as Lua
57−import qualified Foreign.Lua.Module.Text as Lua
58
44
45+-- | Run the Lua filter in @filterPath@ for a transformation to target
46+-- format @format@. Pandoc uses Lua init files to setup the Lua
47+-- interpreter.
59−runLuaFilter :: Maybe FilePath -> FilePath -> String
48+runLuaFilter :: FilePath -> String
60 -> Pandoc -> PandocIO (Either LuaException Pandoc)
49 -> Pandoc -> PandocIO (Either LuaException Pandoc)
61−runLuaFilter datadir filterPath format pd = do
50+runLuaFilter filterPath format doc =
62− luaPkgParams <- luaPackageParams datadir
63− res <- liftIO . Lua.runLuaEither $
64− runLuaFilter' luaPkgParams filterPath format pd
51+ runPandocLua (runLuaFilter' filterPath format doc)
65− newMediaBag <- liftIO (readIORef (luaPkgMediaBag luaPkgParams))
66− setMediaBag newMediaBag
67− return res
68
52
69−runLuaFilter' :: LuaPackageParams
70− -> FilePath -> String
53+runLuaFilter' :: FilePath -> String
71 -> Pandoc -> Lua Pandoc
54 -> Pandoc -> Lua Pandoc
72−runLuaFilter' luaPkgOpts filterPath format pd = do
55+runLuaFilter' filterPath format pd = do
73− initLuaState luaPkgOpts
74 -- store module in global "pandoc"
56 -- store module in global "pandoc"
75 registerFormat
57 registerFormat
76 top <- Lua.gettop
58 top <- Lua.gettop
@@ -90,24 +72,6 @@ runLuaFilter' luaPkgOpts filterPath format pd = do
90 push format
72 push format
91 Lua.setglobal "FORMAT"
73 Lua.setglobal "FORMAT"
92
74
93−luaPackageParams :: Maybe FilePath -> PandocIO LuaPackageParams
94−luaPackageParams datadir = do
95− commonState <- getCommonState
96− mbRef <- liftIO . newIORef =<< getMediaBag
97− return LuaPackageParams
98− { luaPkgCommonState = commonState
99− , luaPkgDataDir = datadir
100− , luaPkgMediaBag = mbRef
101− }
102−
103−-- Initialize the lua state with all required values
104−initLuaState :: LuaPackageParams -> Lua ()
105−initLuaState luaPkgParams = do
106− Lua.openlibs
107− Lua.preloadTextModule "text"
108− installPandocPackageSearcher luaPkgParams
109− loadScriptFromDataDir (luaPkgDataDir luaPkgParams) "init.lua"
110−
111pushGlobalFilter :: Lua ()
75pushGlobalFilter :: Lua ()
112pushGlobalFilter = do
76pushGlobalFilter = do
113 Lua.newtable
77 Lua.newtable
@@ -117,6 +81,3 @@ pushGlobalFilter = do
117
81
118runAll :: [LuaFilter] -> Pandoc -> Lua Pandoc
82runAll :: [LuaFilter] -> Pandoc -> Lua Pandoc
119runAll = foldr ((>=>) . walkMWithLuaFilter) return
83runAll = foldr ((>=>) . walkMWithLuaFilter) return
120−
121−instance (FromLuaStack a) => FromLuaStack (Identity a) where
122− peek = fmap return . peek
src/Text/Pandoc/Lua/Init.hsnew file+79
@@ -0,0 +1,79 @@
1+{-
2+Copyright © 2017 Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
3+
4+This program is free software; you can redistribute it and/or modify
5+it under the terms of the GNU General Public License as published by
6+the Free Software Foundation; either version 2 of the License, or
7+(at your option) any later version.
8+
9+This program is distributed in the hope that it will be useful,
10+but WITHOUT ANY WARRANTY; without even the implied warranty of
11+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+GNU General Public License for more details.
13+
14+You should have received a copy of the GNU General Public License
15+along with this program; if not, write to the Free Software
16+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17+-}
18+{- |
19+ Module : Text.Pandoc.Lua
20+ Copyright : Copyright © 2017 Albert Krewinkel
21+ License : GNU GPL, version 2 or above
22+
23+ Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
24+ Stability : alpha
25+
26+Functions to initialize the Lua interpreter.
27+-}
28+module Text.Pandoc.Lua.Init
29+ ( LuaException (..)
30+ , LuaPackageParams (..)
31+ , runPandocLua
32+ , initLuaState
33+ , luaPackageParams
34+ ) where
35+
36+import Control.Monad.Trans (MonadIO (..))
37+import Data.IORef (newIORef, readIORef)
38+import Foreign.Lua (Lua, LuaException (..))
39+import GHC.IO.Encoding (getForeignEncoding, setForeignEncoding, utf8)
40+import Text.Pandoc.Class (PandocIO, getCommonState, getUserDataDir, getMediaBag,
41+ setMediaBag)
42+import Text.Pandoc.Lua.Packages (LuaPackageParams (..),
43+ installPandocPackageSearcher)
44+import Text.Pandoc.Lua.Util (loadScriptFromDataDir)
45+
46+import qualified Foreign.Lua as Lua
47+import qualified Foreign.Lua.Module.Text as Lua
48+
49+-- | Run the lua interpreter, using pandoc's default way of environment
50+-- initalization.
51+runPandocLua :: Lua a -> PandocIO (Either LuaException a)
52+runPandocLua luaOp = do
53+ datadir <- getUserDataDir
54+ luaPkgParams <- luaPackageParams datadir
55+ enc <- liftIO $ getForeignEncoding <* setForeignEncoding utf8
56+ res <- liftIO $ Lua.runLuaEither (initLuaState luaPkgParams *> luaOp)
57+ liftIO $ setForeignEncoding enc
58+ newMediaBag <- liftIO (readIORef (luaPkgMediaBag luaPkgParams))
59+ setMediaBag newMediaBag
60+ return res
61+
62+-- | Generate parameters required to setup pandoc's lua environment.
63+luaPackageParams :: Maybe FilePath -> PandocIO LuaPackageParams
64+luaPackageParams datadir = do
65+ commonState <- getCommonState
66+ mbRef <- liftIO . newIORef =<< getMediaBag
67+ return LuaPackageParams
68+ { luaPkgCommonState = commonState
69+ , luaPkgDataDir = datadir
70+ , luaPkgMediaBag = mbRef
71+ }
72+
73+-- Initialize the lua state with all required values
74+initLuaState :: LuaPackageParams -> Lua ()
75+initLuaState luaPkgParams = do
76+ Lua.openlibs
77+ Lua.preloadTextModule "text"
78+ installPandocPackageSearcher luaPkgParams
79+ loadScriptFromDataDir (luaPkgDataDir luaPkgParams) "init.lua"
src/Text/Pandoc/Writers/Custom.hs+69−80
@@ -1,11 +1,5 @@
1−{-# OPTIONS_GHC -fno-warn-orphans #-}
2−{-# LANGUAGE CPP #-}
3{-# LANGUAGE DeriveDataTypeable #-}
1{-# LANGUAGE DeriveDataTypeable #-}
4{-# LANGUAGE FlexibleInstances #-}
2{-# LANGUAGE FlexibleInstances #-}
5−#if MIN_VERSION_base(4,8,0)
6−#else
7−{-# LANGUAGE OverlappingInstances #-}
8−#endif
9{- Copyright (C) 2012-2017 John MacFarlane <jgm@berkeley.edu>
3{- Copyright (C) 2012-2017 John MacFarlane <jgm@berkeley.edu>
10
4
11This program is free software; you can redistribute it and/or modify
5This program is free software; you can redistribute it and/or modify
@@ -36,19 +30,23 @@ Conversion of 'Pandoc' documents to custom markup using
36a lua writer.
30a lua writer.
37-}
31-}
38module Text.Pandoc.Writers.Custom ( writeCustom ) where
32module Text.Pandoc.Writers.Custom ( writeCustom ) where
33+import Control.Arrow ((***))
39import Control.Exception
34import Control.Exception
40import Control.Monad (when)
35import Control.Monad (when)
36+import Control.Monad.Trans (MonadIO (liftIO))
41import Data.Char (toLower)
37import Data.Char (toLower)
42import Data.List (intersperse)
38import Data.List (intersperse)
43import qualified Data.Map as M
39import qualified Data.Map as M
44import Data.Text (Text, pack)
40import Data.Text (Text, pack)
45import Data.Typeable
41import Data.Typeable
46−import Foreign.Lua (Lua, ToLuaStack (..), callFunc, runLua)
42+import Foreign.Lua (Lua, ToLuaStack (..), callFunc)
47import Foreign.Lua.Api
43import Foreign.Lua.Api
48−import GHC.IO.Encoding (getForeignEncoding, setForeignEncoding, utf8)
44+import Text.Pandoc.Class (PandocIO)
49import Text.Pandoc.Definition
45import Text.Pandoc.Definition
50import Text.Pandoc.Error
46import Text.Pandoc.Error
51−import Text.Pandoc.Lua.Util (addValue)
47+import Text.Pandoc.Lua.Init (runPandocLua)
48+import Text.Pandoc.Lua.StackInstances ()
49+import Text.Pandoc.Lua.Util (addValue, dostring')
52import Text.Pandoc.Options
50import Text.Pandoc.Options
53import Text.Pandoc.Templates
51import Text.Pandoc.Templates
54import qualified Text.Pandoc.UTF8 as UTF8
52import qualified Text.Pandoc.UTF8 as UTF8
@@ -60,43 +58,31 @@ attrToMap (id',classes,keyvals) = M.fromList
60 : ("class", unwords classes)
58 : ("class", unwords classes)
61 : keyvals
59 : keyvals
62
60
63−instance ToLuaStack Double where
61+newtype Stringify a = Stringify a
64− push = push . (realToFrac :: Double -> LuaNumber)
62+
65−
66−instance ToLuaStack Int where
63+instance ToLuaStack (Stringify Format) where
67− push = push . (fromIntegral :: Int -> LuaInteger)
68−
69−instance ToLuaStack Format where
70− push (Format f) = push (map toLower f)
64+ push (Stringify (Format f)) = push (map toLower f)
71−
65+
72−#if MIN_VERSION_base(4,8,0)
73−instance {-# OVERLAPS #-} ToLuaStack [Inline] where
66+instance ToLuaStack (Stringify [Inline]) where
74−#else
75−instance ToLuaStack [Inline] where
76−#endif
77− push ils = push =<< inlineListToCustom ils
67+ push (Stringify ils) = push =<< inlineListToCustom ils
78−
68+
79−#if MIN_VERSION_base(4,8,0)
80−instance {-# OVERLAPS #-} ToLuaStack [Block] where
69+instance ToLuaStack (Stringify [Block]) where
81−#else
82−instance ToLuaStack [Block] where
83−#endif
84− push ils = push =<< blockListToCustom ils
70+ push (Stringify blks) = push =<< blockListToCustom blks
85−
71+
86−instance ToLuaStack MetaValue where
72+instance ToLuaStack (Stringify MetaValue) where
87− push (MetaMap m) = push m
73+ push (Stringify (MetaMap m)) = push (fmap Stringify m)
88− push (MetaList xs) = push xs
74+ push (Stringify (MetaList xs)) = push (map Stringify xs)
89− push (MetaBool x) = push x
75+ push (Stringify (MetaBool x)) = push x
90− push (MetaString s) = push s
76+ push (Stringify (MetaString s)) = push s
91− push (MetaInlines ils) = push ils
77+ push (Stringify (MetaInlines ils)) = push (Stringify ils)
92− push (MetaBlocks bs) = push bs
78+ push (Stringify (MetaBlocks bs)) = push (Stringify bs)
93−
79+
94−instance ToLuaStack Citation where
80+instance ToLuaStack (Stringify Citation) where
95− push cit = do
81+ push (Stringify cit) = do
96 createtable 6 0
82 createtable 6 0
97 addValue "citationId" $ citationId cit
83 addValue "citationId" $ citationId cit
98− addValue "citationPrefix" $ citationPrefix cit
84+ addValue "citationPrefix" . Stringify $ citationPrefix cit
99− addValue "citationSuffix" $ citationSuffix cit
85+ addValue "citationSuffix" . Stringify $ citationSuffix cit
100 addValue "citationMode" $ show (citationMode cit)
86 addValue "citationMode" $ show (citationMode cit)
101 addValue "citationNoteNum" $ citationNoteNum cit
87 addValue "citationNoteNum" $ citationNoteNum cit
102 addValue "citationHash" $ citationHash cit
88 addValue "citationHash" $ citationHash cit
@@ -107,14 +93,11 @@ data PandocLuaException = PandocLuaException String
107instance Exception PandocLuaException
93instance Exception PandocLuaException
108
94
109-- | Convert Pandoc to custom markup.
95-- | Convert Pandoc to custom markup.
110−writeCustom :: FilePath -> WriterOptions -> Pandoc -> IO Text
96+writeCustom :: FilePath -> WriterOptions -> Pandoc -> PandocIO Text
111writeCustom luaFile opts doc@(Pandoc meta _) = do
97writeCustom luaFile opts doc@(Pandoc meta _) = do
112− luaScript <- UTF8.readFile luaFile
98+ luaScript <- liftIO $ UTF8.readFile luaFile
113− enc <- getForeignEncoding
99+ res <- runPandocLua $ do
114− setForeignEncoding utf8
115− (body, context) <- runLua $ do
116− openlibs
117− stat <- loadstring luaScript
100+ stat <- dostring' luaScript
118 -- check for error in lua script (later we'll change the return type
101 -- check for error in lua script (later we'll change the return type
119 -- to handle this more gracefully):
102 -- to handle this more gracefully):
120 when (stat /= OK) $
103 when (stat /= OK) $
@@ -127,7 +110,9 @@ writeCustom luaFile opts doc@(Pandoc meta _) = do
127 inlineListToCustom
110 inlineListToCustom
128 meta
111 meta
129 return (rendered, context)
112 return (rendered, context)
130− setForeignEncoding enc
113+ let (body, context) = case res of
114+ Left e -> throw (PandocLuaException (show e))
115+ Right x -> x
131 case writerTemplate opts of
116 case writerTemplate opts of
132 Nothing -> return $ pack body
117 Nothing -> return $ pack body
133 Just tpl ->
118 Just tpl ->
@@ -138,7 +123,7 @@ writeCustom luaFile opts doc@(Pandoc meta _) = do
138docToCustom :: WriterOptions -> Pandoc -> Lua String
123docToCustom :: WriterOptions -> Pandoc -> Lua String
139docToCustom opts (Pandoc (Meta metamap) blocks) = do
124docToCustom opts (Pandoc (Meta metamap) blocks) = do
140 body <- blockListToCustom blocks
125 body <- blockListToCustom blocks
141− callFunc "Doc" body metamap (writerVariables opts)
126+ callFunc "Doc" body (fmap Stringify metamap) (writerVariables opts)
142
127
143-- | Convert Pandoc block element to Custom.
128-- | Convert Pandoc block element to Custom.
144blockToCustom :: Block -- ^ Block element
129blockToCustom :: Block -- ^ Block element
@@ -146,41 +131,45 @@ blockToCustom :: Block -- ^ Block element
146
131
147blockToCustom Null = return ""
132blockToCustom Null = return ""
148
133
149−blockToCustom (Plain inlines) = callFunc "Plain" inlines
134+blockToCustom (Plain inlines) = callFunc "Plain" (Stringify inlines)
150
135
151blockToCustom (Para [Image attr txt (src,tit)]) =
136blockToCustom (Para [Image attr txt (src,tit)]) =
152− callFunc "CaptionedImage" src tit txt (attrToMap attr)
137+ callFunc "CaptionedImage" src tit (Stringify txt) (attrToMap attr)
153
138
154−blockToCustom (Para inlines) = callFunc "Para" inlines
139+blockToCustom (Para inlines) = callFunc "Para" (Stringify inlines)
155
140
156−blockToCustom (LineBlock linesList) = callFunc "LineBlock" linesList
141+blockToCustom (LineBlock linesList) = callFunc "LineBlock" (map Stringify linesList)
157
142
158blockToCustom (RawBlock format str) =
143blockToCustom (RawBlock format str) =
159− callFunc "RawBlock" format str
144+ callFunc "RawBlock" (Stringify format) str
160
145
161blockToCustom HorizontalRule = callFunc "HorizontalRule"
146blockToCustom HorizontalRule = callFunc "HorizontalRule"
162
147
163blockToCustom (Header level attr inlines) =
148blockToCustom (Header level attr inlines) =
164− callFunc "Header" level inlines (attrToMap attr)
149+ callFunc "Header" level (Stringify inlines) (attrToMap attr)
165
150
166blockToCustom (CodeBlock attr str) =
151blockToCustom (CodeBlock attr str) =
167 callFunc "CodeBlock" str (attrToMap attr)
152 callFunc "CodeBlock" str (attrToMap attr)
168
153
169−blockToCustom (BlockQuote blocks) = callFunc "BlockQuote" blocks
154+blockToCustom (BlockQuote blocks) = callFunc "BlockQuote" (Stringify blocks)
170
155
171−blockToCustom (Table capt aligns widths headers rows') =
156+blockToCustom (Table capt aligns widths headers rows) =
157+ let aligns' = map show aligns
158+ capt' = Stringify capt
159+ headers' = map Stringify headers
160+ rows' = map (map Stringify) rows
172− callFunc "Table" capt (map show aligns) widths headers rows'
161+ in callFunc "Table" capt' aligns' widths headers' rows'
173
162
174−blockToCustom (BulletList items) = callFunc "BulletList" items
163+blockToCustom (BulletList items) = callFunc "BulletList" (map Stringify items)
175
164
176blockToCustom (OrderedList (num,sty,delim) items) =
165blockToCustom (OrderedList (num,sty,delim) items) =
177− callFunc "OrderedList" items num (show sty) (show delim)
166+ callFunc "OrderedList" (map Stringify items) num (show sty) (show delim)
178
167
179blockToCustom (DefinitionList items) =
168blockToCustom (DefinitionList items) =
180− callFunc "DefinitionList" items
169+ callFunc "DefinitionList" (map (Stringify *** map Stringify) items)
181
170
182blockToCustom (Div attr items) =
171blockToCustom (Div attr items) =
183− callFunc "Div" items (attrToMap attr)
172+ callFunc "Div" (Stringify items) (attrToMap attr)
184
173
185-- | Convert list of Pandoc block elements to Custom.
174-- | Convert list of Pandoc block elements to Custom.
186blockListToCustom :: [Block] -- ^ List of block elements
175blockListToCustom :: [Block] -- ^ List of block elements
@@ -205,23 +194,23 @@ inlineToCustom Space = callFunc "Space"
205
194
206inlineToCustom SoftBreak = callFunc "SoftBreak"
195inlineToCustom SoftBreak = callFunc "SoftBreak"
207
196
208−inlineToCustom (Emph lst) = callFunc "Emph" lst
197+inlineToCustom (Emph lst) = callFunc "Emph" (Stringify lst)
209
198
210−inlineToCustom (Strong lst) = callFunc "Strong" lst
199+inlineToCustom (Strong lst) = callFunc "Strong" (Stringify lst)
211
200
212−inlineToCustom (Strikeout lst) = callFunc "Strikeout" lst
201+inlineToCustom (Strikeout lst) = callFunc "Strikeout" (Stringify lst)
213
202
214−inlineToCustom (Superscript lst) = callFunc "Superscript" lst
203+inlineToCustom (Superscript lst) = callFunc "Superscript" (Stringify lst)
215
204
216−inlineToCustom (Subscript lst) = callFunc "Subscript" lst
205+inlineToCustom (Subscript lst) = callFunc "Subscript" (Stringify lst)
217
206
218−inlineToCustom (SmallCaps lst) = callFunc "SmallCaps" lst
207+inlineToCustom (SmallCaps lst) = callFunc "SmallCaps" (Stringify lst)
219
208
220−inlineToCustom (Quoted SingleQuote lst) = callFunc "SingleQuoted" lst
209+inlineToCustom (Quoted SingleQuote lst) = callFunc "SingleQuoted" (Stringify lst)
221
210
222−inlineToCustom (Quoted DoubleQuote lst) = callFunc "DoubleQuoted" lst
211+inlineToCustom (Quoted DoubleQuote lst) = callFunc "DoubleQuoted" (Stringify lst)
223
212
224−inlineToCustom (Cite cs lst) = callFunc "Cite" lst cs
213+inlineToCustom (Cite cs lst) = callFunc "Cite" (Stringify lst) (map Stringify cs)
225
214
226inlineToCustom (Code attr str) =
215inlineToCustom (Code attr str) =
227 callFunc "Code" str (attrToMap attr)
216 callFunc "Code" str (attrToMap attr)
@@ -233,17 +222,17 @@ inlineToCustom (Math InlineMath str) =
233 callFunc "InlineMath" str
222 callFunc "InlineMath" str
234
223
235inlineToCustom (RawInline format str) =
224inlineToCustom (RawInline format str) =
236− callFunc "RawInline" format str
225+ callFunc "RawInline" (Stringify format) str
237
226
238inlineToCustom LineBreak = callFunc "LineBreak"
227inlineToCustom LineBreak = callFunc "LineBreak"
239
228
240inlineToCustom (Link attr txt (src,tit)) =
229inlineToCustom (Link attr txt (src,tit)) =
241− callFunc "Link" txt src tit (attrToMap attr)
230+ callFunc "Link" (Stringify txt) src tit (attrToMap attr)
242
231
243inlineToCustom (Image attr alt (src,tit)) =
232inlineToCustom (Image attr alt (src,tit)) =
244− callFunc "Image" alt src tit (attrToMap attr)
233+ callFunc "Image" (Stringify alt) src tit (attrToMap attr)
245
234
246−inlineToCustom (Note contents) = callFunc "Note" contents
235+inlineToCustom (Note contents) = callFunc "Note" (Stringify contents)
247
236
248inlineToCustom (Span attr items) =
237inlineToCustom (Span attr items) =
249− callFunc "Span" items (attrToMap attr)
238+ callFunc "Span" (Stringify items) (attrToMap attr)
test/Tests/Lua.hs+18−13
@@ -10,9 +10,9 @@ import Text.Pandoc.Arbitrary ()
10import Text.Pandoc.Builder (bulletList, divWith, doc, doubleQuoted, emph,
10import Text.Pandoc.Builder (bulletList, divWith, doc, doubleQuoted, emph,
11 header, linebreak, para, plain, rawBlock,
11 header, linebreak, para, plain, rawBlock,
12 singleQuoted, space, str, strong, (<>))
12 singleQuoted, space, str, strong, (<>))
13−import Text.Pandoc.Class (runIOorExplode)
13+import Text.Pandoc.Class (runIOorExplode, setUserDataDir)
14import Text.Pandoc.Definition (Block, Inline, Meta, Pandoc)
14import Text.Pandoc.Definition (Block, Inline, Meta, Pandoc)
15−import Text.Pandoc.Lua (initLuaState, runLuaFilter, luaPackageParams)
15+import Text.Pandoc.Lua (runLuaFilter, runPandocLua)
16
16
17import qualified Foreign.Lua as Lua
17import qualified Foreign.Lua as Lua
18
18
@@ -95,8 +95,9 @@ tests = map (localOption (QuickCheckTests 20))
95
95
96assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
96assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
97assertFilterConversion msg filterPath docIn docExpected = do
97assertFilterConversion msg filterPath docIn docExpected = do
98− docEither <- runIOorExplode $
98+ docEither <- runIOorExplode $ do
99+ setUserDataDir (Just "../data")
99− runLuaFilter (Just "../data") ("lua" </> filterPath) [] docIn
100+ runLuaFilter ("lua" </> filterPath) [] docIn
100 case docEither of
101 case docEither of
101 Left _ -> fail "lua filter failed"
102 Left _ -> fail "lua filter failed"
102 Right docRes -> assertEqual msg docExpected docRes
103 Right docRes -> assertEqual msg docExpected docRes
@@ -105,14 +106,18 @@ roundtripEqual :: (Eq a, Lua.FromLuaStack a, Lua.ToLuaStack a) => a -> IO Bool
105roundtripEqual x = (x ==) <$> roundtripped
106roundtripEqual x = (x ==) <$> roundtripped
106 where
107 where
107 roundtripped :: (Lua.FromLuaStack a, Lua.ToLuaStack a) => IO a
108 roundtripped :: (Lua.FromLuaStack a, Lua.ToLuaStack a) => IO a
108− roundtripped = Lua.runLua $ do
109+ roundtripped = runIOorExplode $ do
109− initLuaState =<< Lua.liftIO (runIOorExplode (luaPackageParams (Just "../data")))
110+ setUserDataDir (Just "../data")
111+ res <- runPandocLua $ do
110− oldSize <- Lua.gettop
112+ oldSize <- Lua.gettop
111− Lua.push x
113+ Lua.push x
112− size <- Lua.gettop
114+ size <- Lua.gettop
113− when (size - oldSize /= 1) $
115+ when (size - oldSize /= 1) $
114− error ("not exactly one additional element on the stack: " ++ show size)
116+ error ("not exactly one additional element on the stack: " ++ show size)
115− res <- Lua.peekEither (-1)
117+ res <- Lua.peekEither (-1)
118+ case res of
119+ Left _ -> error "could not read from stack"
120+ Right y -> return y
116 case res of
121 case res of
117− Left _ -> error "could not read from stack"
122+ Left e -> error (show e)
118 Right y -> return y
123 Right y -> return y