"Fossies" - the Fresh Open Source Software Archive

Member "pandoc-2.7.3/src/Text/Pandoc/Lua/Packages.hs" (12 Jun 2019, 3239 Bytes) of package /linux/www/pandoc-2.7.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Haskell source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "Packages.hs": 2.7.2_vs_2.7.3.

    1 {-# LANGUAGE FlexibleContexts #-}
    2 {-# LANGUAGE NoImplicitPrelude #-}
    3 {-# LANGUAGE ScopedTypeVariables #-}
    4 {- |
    5    Module      : Text.Pandoc.Lua.Packages
    6    Copyright   : Copyright © 2017-2019 Albert Krewinkel
    7    License     : GNU GPL, version 2 or above
    8 
    9    Maintainer  : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
   10    Stability   : alpha
   11 
   12 Pandoc module for lua.
   13 -}
   14 module Text.Pandoc.Lua.Packages
   15   ( LuaPackageParams (..)
   16   , installPandocPackageSearcher
   17   ) where
   18 
   19 import Prelude
   20 import Control.Monad (forM_)
   21 import Data.ByteString (ByteString)
   22 import Foreign.Lua (Lua, NumResults, liftIO)
   23 import Text.Pandoc.Class (readDataFile, runIO, setUserDataDir)
   24 
   25 import qualified Foreign.Lua as Lua
   26 import Text.Pandoc.Lua.Module.Pandoc as Pandoc
   27 import Text.Pandoc.Lua.Module.MediaBag as MediaBag
   28 import Text.Pandoc.Lua.Module.System as System
   29 import Text.Pandoc.Lua.Module.Types as Types
   30 import Text.Pandoc.Lua.Module.Utils as Utils
   31 
   32 -- | Parameters used to create lua packages/modules.
   33 data LuaPackageParams = LuaPackageParams
   34   { luaPkgDataDir :: Maybe FilePath
   35   }
   36 
   37 -- | Insert pandoc's package loader as the first loader, making it the default.
   38 installPandocPackageSearcher :: LuaPackageParams -> Lua ()
   39 installPandocPackageSearcher luaPkgParams = do
   40   Lua.getglobal' "package.searchers"
   41   shiftArray
   42   Lua.pushHaskellFunction (pandocPackageSearcher luaPkgParams)
   43   Lua.rawseti (Lua.nthFromTop 2) 1
   44   Lua.pop 1           -- remove 'package.searchers' from stack
   45  where
   46   shiftArray = forM_ [4, 3, 2, 1] $ \i -> do
   47     Lua.rawgeti (-1) i
   48     Lua.rawseti (-2) (i + 1)
   49 
   50 -- | Load a pandoc module.
   51 pandocPackageSearcher :: LuaPackageParams -> String -> Lua NumResults
   52 pandocPackageSearcher pkgParams pkgName =
   53   case pkgName of
   54     "pandoc"          -> let datadir = luaPkgDataDir pkgParams
   55                          in pushWrappedHsFun (Pandoc.pushModule datadir)
   56     "pandoc.mediabag" -> pushWrappedHsFun MediaBag.pushModule
   57     "pandoc.system"   -> pushWrappedHsFun System.pushModule
   58     "pandoc.types"    -> pushWrappedHsFun Types.pushModule
   59     "pandoc.utils"    -> let datadir = luaPkgDataDir pkgParams
   60                          in pushWrappedHsFun (Utils.pushModule datadir)
   61     _ -> searchPureLuaLoader
   62  where
   63   pushWrappedHsFun f = do
   64     Lua.pushHaskellFunction f
   65     return 1
   66   searchPureLuaLoader = do
   67     let filename = pkgName ++ ".lua"
   68     modScript <- liftIO (dataDirScript (luaPkgDataDir pkgParams) filename)
   69     case modScript of
   70       Just script -> pushWrappedHsFun (loadStringAsPackage pkgName script)
   71       Nothing -> do
   72         Lua.push ("\n\tno file '" ++ filename ++ "' in pandoc's datadir")
   73         return 1
   74 
   75 loadStringAsPackage :: String -> ByteString -> Lua NumResults
   76 loadStringAsPackage pkgName script = do
   77   status <- Lua.dostring script
   78   if status == Lua.OK
   79     then return (1 :: NumResults)
   80     else do
   81       msg <- Lua.popValue
   82       Lua.raiseError ("Error while loading `" <> pkgName <> "`.\n" <> msg)
   83 
   84 -- | Get the ByteString representation of the pandoc module.
   85 dataDirScript :: Maybe FilePath -> FilePath -> IO (Maybe ByteString)
   86 dataDirScript datadir moduleFile = do
   87   res <- runIO $ setUserDataDir datadir >> readDataFile moduleFile
   88   return $ case res of
   89     Left _ -> Nothing
   90     Right s -> Just s