{- |
   Module      : Text.Pandoc.Lua.Packages
   Copyright   : Copyright © 2017-2021 Albert Krewinkel
   License     : GNU GPL, version 2 or above

   Maintainer  : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
   Stability   : alpha

Pandoc module for Lua.
-}
module Text.Pandoc.Lua.Packages
  ( installPandocPackageSearcher
  ) where

import Control.Monad (forM_)
import Foreign.Lua (NumResults)
import Text.Pandoc.Lua.PandocLua (PandocLua, liftPandocLua, loadDefaultModule)

import qualified Foreign.Lua as Lua
import qualified Foreign.Lua.Module.Path as Path
import qualified Foreign.Lua.Module.Text as Text
import qualified Text.Pandoc.Lua.Module.Pandoc as Pandoc
import qualified Text.Pandoc.Lua.Module.MediaBag as MediaBag
import qualified Text.Pandoc.Lua.Module.System as System
import qualified Text.Pandoc.Lua.Module.Types as Types
import qualified Text.Pandoc.Lua.Module.Utils as Utils

-- | Insert pandoc's package loader as the first loader, making it the default.
installPandocPackageSearcher :: PandocLua ()
installPandocPackageSearcher :: PandocLua ()
installPandocPackageSearcher = Lua () -> PandocLua ()
forall a. Lua a -> PandocLua a
liftPandocLua (Lua () -> PandocLua ()) -> Lua () -> PandocLua ()
forall a b. (a -> b) -> a -> b
$ do
  String -> Lua ()
Lua.getglobal' String
"package.searchers"
  Lua ()
shiftArray
  (String -> PandocLua NumResults) -> Lua ()
forall a. ToHaskellFunction a => a -> Lua ()
Lua.pushHaskellFunction String -> PandocLua NumResults
pandocPackageSearcher
  StackIndex -> Integer -> Lua ()
Lua.rawseti (CInt -> StackIndex
Lua.nthFromTop CInt
2) Integer
1
  StackIndex -> Lua ()
Lua.pop StackIndex
1           -- remove 'package.searchers' from stack
 where
  shiftArray :: Lua ()
shiftArray = [Integer] -> (Integer -> Lua ()) -> Lua ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Integer
4, Integer
3, Integer
2, Integer
1] ((Integer -> Lua ()) -> Lua ()) -> (Integer -> Lua ()) -> Lua ()
forall a b. (a -> b) -> a -> b
$ \Integer
i -> do
    StackIndex -> Integer -> Lua ()
Lua.rawgeti (-StackIndex
1) Integer
i
    StackIndex -> Integer -> Lua ()
Lua.rawseti (-StackIndex
2) (Integer
i Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1)

-- | Load a pandoc module.
pandocPackageSearcher :: String -> PandocLua NumResults
pandocPackageSearcher :: String -> PandocLua NumResults
pandocPackageSearcher String
pkgName =
  case String
pkgName of
    String
"pandoc"          -> PandocLua NumResults -> PandocLua NumResults
forall a a. (ToHaskellFunction a, Num a) => a -> PandocLua a
pushWrappedHsFun PandocLua NumResults
Pandoc.pushModule
    String
"pandoc.mediabag" -> PandocLua NumResults -> PandocLua NumResults
forall a a. (ToHaskellFunction a, Num a) => a -> PandocLua a
pushWrappedHsFun PandocLua NumResults
MediaBag.pushModule
    String
"pandoc.path"     -> Lua NumResults -> PandocLua NumResults
forall a a. (ToHaskellFunction a, Num a) => a -> PandocLua a
pushWrappedHsFun Lua NumResults
Path.pushModule
    String
"pandoc.system"   -> Lua NumResults -> PandocLua NumResults
forall a a. (ToHaskellFunction a, Num a) => a -> PandocLua a
pushWrappedHsFun Lua NumResults
System.pushModule
    String
"pandoc.types"    -> Lua NumResults -> PandocLua NumResults
forall a a. (ToHaskellFunction a, Num a) => a -> PandocLua a
pushWrappedHsFun Lua NumResults
Types.pushModule
    String
"pandoc.utils"    -> PandocLua NumResults -> PandocLua NumResults
forall a a. (ToHaskellFunction a, Num a) => a -> PandocLua a
pushWrappedHsFun PandocLua NumResults
Utils.pushModule
    String
"text"            -> Lua NumResults -> PandocLua NumResults
forall a a. (ToHaskellFunction a, Num a) => a -> PandocLua a
pushWrappedHsFun Lua NumResults
Text.pushModule
    String
"pandoc.List"     -> PandocLua NumResults -> PandocLua NumResults
forall a a. (ToHaskellFunction a, Num a) => a -> PandocLua a
pushWrappedHsFun (String -> PandocLua NumResults
loadDefaultModule String
pkgName)
    String
_                 -> PandocLua NumResults
reportPandocSearcherFailure
 where
  pushWrappedHsFun :: a -> PandocLua a
pushWrappedHsFun a
f = Lua a -> PandocLua a
forall a. Lua a -> PandocLua a
liftPandocLua (Lua a -> PandocLua a) -> Lua a -> PandocLua a
forall a b. (a -> b) -> a -> b
$ do
    a -> Lua ()
forall a. ToHaskellFunction a => a -> Lua ()
Lua.pushHaskellFunction a
f
    a -> Lua a
forall (m :: * -> *) a. Monad m => a -> m a
return a
1
  reportPandocSearcherFailure :: PandocLua NumResults
reportPandocSearcherFailure = Lua NumResults -> PandocLua NumResults
forall a. Lua a -> PandocLua a
liftPandocLua (Lua NumResults -> PandocLua NumResults)
-> Lua NumResults -> PandocLua NumResults
forall a b. (a -> b) -> a -> b
$ do
    String -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push (String
"\n\t" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
pkgName String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"is not one of pandoc's default packages")
    NumResults -> Lua NumResults
forall (m :: * -> *) a. Monad m => a -> m a
return (NumResults
1 :: NumResults)