git.iko.soy/typeable/pandoc
on master help sign in
Lua modules: move to dedicated submodule

The Haskell module defining the Lua `pandoc` module is moved to Text.Pandoc.Lua.Module.Pandoc. Change: minor

commitab3c5065847f60f0c3f3b097331a28d27716dc8d authorAlbert Krewinkel <albert@zeitkraut.de> dateThu 21 Dec 2017, 21:37 (9 years ago) parent6ec7e39
5 files changed+38−35
pandoc.cabal+1−1 src/Text/Pandoc/Lua.hs+5−1 src/Text/Pandoc/Lua/PandocModule.hs → src/Text/Pandoc/Lua/Module/Pandoc.hs+9−29 src/Text/Pandoc/Lua/Packages.hs+3−2 src/Text/Pandoc/Lua/Util.hs+20−2
pandoc.cabal+1−1
@@ -523,8 +523,8 @@ library
523523 Text.Pandoc.Readers.Org.Shared,
524524 Text.Pandoc.Lua.Filter,
525525 Text.Pandoc.Lua.Init,
526+ Text.Pandoc.Lua.Module.Pandoc,
526527 Text.Pandoc.Lua.Packages,
527− Text.Pandoc.Lua.PandocModule,
528528 Text.Pandoc.Lua.StackInstances,
529529 Text.Pandoc.Lua.Util,
530530 Text.Pandoc.CSS,
src/Text/Pandoc/Lua.hs+5−1
@@ -39,7 +39,7 @@ import Text.Pandoc.Class (PandocIO)
3939import Text.Pandoc.Definition (Pandoc)
4040import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter)
4141import Text.Pandoc.Lua.Init (runPandocLua)
42−import Text.Pandoc.Lua.PandocModule (pushPandocModule) -- TODO: remove
42+import Text.Pandoc.Lua.Module.Pandoc (pushModule) -- TODO: remove
4343import qualified Foreign.Lua as Lua
4444
4545-- | Run the Lua filter in @filterPath@ for a transformation to target
@@ -81,3 +81,7 @@ pushGlobalFilter = do
8181
8282runAll :: [LuaFilter] -> Pandoc -> Lua Pandoc
8383runAll = foldr ((>=>) . walkMWithLuaFilter) return
84+
85+-- | DEPRECATED: Push the pandoc module to the Lua Stack.
86+pushPandocModule :: Maybe FilePath -> Lua Lua.NumResults
87+pushPandocModule = pushModule
src/Text/Pandoc/Lua/PandocModule.hs → src/Text/Pandoc/Lua/Module/Pandoc.hsrenamed+9−29
@@ -17,7 +17,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1717-}
1818{-# LANGUAGE FlexibleContexts #-}
1919{- |
20− Module : Text.Pandoc.Lua.PandocModule
20+ Module : Text.Pandoc.Lua.Module.Pandoc
2121 Copyright : Copyright © 2017 Albert Krewinkel
2222 License : GNU GPL, version 2 or above
2323
@@ -26,8 +26,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2626
2727Pandoc module for lua.
2828-}
29−module Text.Pandoc.Lua.PandocModule
30− ( pushPandocModule
29+module Text.Pandoc.Lua.Module.Pandoc
30+ ( pushModule
3131 , pushMediaBagModule
3232 ) where
3333
@@ -44,7 +44,8 @@ import Text.Pandoc.Class (CommonState (..), fetchItem, putCommonState,
4444import Text.Pandoc.Definition (Block, Inline)
4545import Text.Pandoc.Lua.Filter (walkInlines, walkBlocks, LuaFilter)
4646import Text.Pandoc.Lua.StackInstances ()
47−import Text.Pandoc.Lua.Util (addFunction, addValue, loadScriptFromDataDir)
47+import Text.Pandoc.Lua.Util (OrNil (toMaybe), addFunction, addValue,
48+ loadScriptFromDataDir, raiseError)
4849import Text.Pandoc.Walk (Walkable)
4950import Text.Pandoc.MIME (MimeType)
5051import Text.Pandoc.Options (ReaderOptions (readerExtensions))
@@ -58,8 +59,8 @@ import qualified Text.Pandoc.MediaBag as MB
5859
5960-- | Push the "pandoc" on the lua stack. Requires the `list` module to be
6061-- loaded.
61−pushPandocModule :: Maybe FilePath -> Lua NumResults
62−pushPandocModule datadir = do
62+pushModule :: Maybe FilePath -> Lua NumResults
63+pushModule datadir = do
6364 loadScriptFromDataDir datadir "pandoc.lua"
6465 addFunction "read" readDoc
6566 addFunction "pipe" pipeFn
@@ -91,10 +92,6 @@ readDoc content formatSpecOrNil = do
9192 Right pd -> (1 :: NumResults) <$ Lua.push pd -- success, push Pandoc
9293 Left s -> raiseError (show s) -- error while reading
9394 _ -> raiseError "Only string formats are supported at the moment."
94− where
95− raiseError s = do
96− Lua.push s
97− fromIntegral <$> Lua.lerror
9895
9996--
10097-- MediaBag submodule
@@ -122,12 +119,8 @@ pipeFn :: String
122119pipeFn command args input = do
123120 (ec, output) <- liftIO $ pipeProcess Nothing command args input
124121 case ec of
125− ExitSuccess -> do
126− Lua.push output
127− return 1
128− ExitFailure n -> do
129− Lua.push (PipeError command n output)
130− fromIntegral <$> Lua.lerror
122+ ExitSuccess -> 1 <$ Lua.push output
123+ ExitFailure n -> raiseError (PipeError command n output)
131124
132125data PipeError = PipeError
133126 { pipeErrorCommand :: String
@@ -218,16 +211,3 @@ fetch commonState mbRef src = do
218211 Lua.push $ fromMaybe "" mimeType
219212 Lua.push bs
220213 return 2 -- returns 2 values: contents, mimetype
221−
222−--
223−-- Helper types
224−--
225−
226−newtype OrNil a = OrNil { toMaybe :: Maybe a }
227−
228−instance FromLuaStack a => FromLuaStack (OrNil a) where
229− peek idx = do
230− noValue <- Lua.isnoneornil idx
231− if noValue
232− then return (OrNil Nothing)
233− else OrNil . Just <$> Lua.peek idx
src/Text/Pandoc/Lua/Packages.hs+3−2
@@ -38,10 +38,11 @@ import Data.IORef (IORef)
3838import Foreign.Lua (Lua, NumResults, liftIO)
3939import Text.Pandoc.Class (CommonState, readDataFile, runIO, setUserDataDir)
4040import Text.Pandoc.MediaBag (MediaBag)
41−import Text.Pandoc.Lua.PandocModule (pushPandocModule, pushMediaBagModule)
41+import Text.Pandoc.Lua.Module.Pandoc (pushMediaBagModule)
4242import Text.Pandoc.Lua.Util (dostring')
4343
4444import qualified Foreign.Lua as Lua
45+import Text.Pandoc.Lua.Module.Pandoc as Pandoc
4546
4647-- | Parameters used to create lua packages/modules.
4748data LuaPackageParams = LuaPackageParams
@@ -72,7 +73,7 @@ pandocPackageSearcher :: LuaPackageParams -> String -> Lua NumResults
7273pandocPackageSearcher luaPkgParams pkgName =
7374 case pkgName of
7475 "pandoc" -> let datadir = luaPkgDataDir luaPkgParams
75− in pushWrappedHsFun (pushPandocModule datadir)
76+ in pushWrappedHsFun (Pandoc.pushModule datadir)
7677 "pandoc.mediabag" -> let st = luaPkgCommonState luaPkgParams
7778 mbRef = luaPkgMediaBag luaPkgParams
7879 in pushWrappedHsFun (pushMediaBagModule st mbRef)
src/Text/Pandoc/Lua/Util.hs+20−2
@@ -36,6 +36,8 @@ module Text.Pandoc.Lua.Util
3636 , getRawInt
3737 , setRawInt
3838 , addRawInt
39+ , raiseError
40+ , OrNil (..)
3941 , PushViaCall
4042 , pushViaCall
4143 , pushViaConstructor
@@ -45,8 +47,8 @@ module Text.Pandoc.Lua.Util
4547
4648import Control.Monad (when)
4749import Data.ByteString.Char8 (unpack)
48−import Foreign.Lua (FromLuaStack (..), ToHaskellFunction, Lua, NumArgs,
49− StackIndex, ToLuaStack (..), getglobal')
50+import Foreign.Lua (FromLuaStack (..), NumResults, Lua, NumArgs, StackIndex,
51+ ToLuaStack (..), ToHaskellFunction, getglobal')
5052import Foreign.Lua.Api (Status, call, pop, rawget, rawgeti, rawset, rawseti)
5153import Text.Pandoc.Class (readDataFile, runIOorExplode, setUserDataDir)
5254
@@ -99,6 +101,22 @@ setRawInt idx key value = do
99101addRawInt :: ToLuaStack a => Int -> a -> Lua ()
100102addRawInt = setRawInt (-1)
101103
104+raiseError :: ToLuaStack a => a -> Lua NumResults
105+raiseError e = do
106+ Lua.push e
107+ fromIntegral <$> Lua.lerror
108+
109+-- | Newtype wrapper intended to be used for optional Lua values. Nesting this
110+-- type is strongly discouraged and will likely lead to a wrong result.
111+newtype OrNil a = OrNil { toMaybe :: Maybe a }
112+
113+instance FromLuaStack a => FromLuaStack (OrNil a) where
114+ peek idx = do
115+ noValue <- Lua.isnoneornil idx
116+ if noValue
117+ then return (OrNil Nothing)
118+ else OrNil . Just <$> Lua.peek idx
119+
102120-- | Helper class for pushing a single value to the stack via a lua function.
103121-- See @pushViaCall@.
104122class PushViaCall a where