git.iko.soy/typeable/pandoc
on master help sign in
Lua: add `pandoc.system` module (#5468)
786594bAlbert Krewinkel
data/pandoc.lua+1
@@ -27,6 +27,7 @@ local M = {}
27-- Re-export bundled modules
27-- Re-export bundled modules
28M.List = require 'pandoc.List'
28M.List = require 'pandoc.List'
29M.mediabag = require 'pandoc.mediabag'
29M.mediabag = require 'pandoc.mediabag'
30+M.system = require 'pandoc.system'
30M.utils = require 'pandoc.utils'
31M.utils = require 'pandoc.utils'
31M.text = require 'text'
32M.text = require 'text'
32
33
doc/lua-filters.md+108
@@ -2618,3 +2618,111 @@ Parameters:
2618
2618
2619Returns: a new list containing all items for which \`test\`
2619Returns: a new list containing all items for which \`test\`
2620was true.
2620was true.
2621+
2622+# Module pandoc.system
2623+
2624+Access to system information and functionality.
2625+
2626+## Static Fields {#system-fields}
2627+
2628+### arch {#system-arch}
2629+
2630+The machine architecture on which the program is running.
2631+
2632+### os {#system-os}
2633+
2634+The operating system on which the program is running.
2635+
2636+## Functions {#system-functions}
2637+
2638+### environment {#system-environment}
2639+
2640+`environment ()`
2641+
2642+Retrieve the entire environment as a string-indexed table.
2643+
2644+Returns:
2645+
2646+- A table mapping environment variables names to their string value
2647+ (table).
2648+
2649+### get\_working\_directory {#system-get_working_directory}
2650+
2651+`get_working_directory ()`
2652+
2653+Obtain the current working directory as an absolute path.
2654+
2655+Returns:
2656+
2657+- The current working directory (string).
2658+
2659+### with\_environment {#system-with_environment}
2660+
2661+`with_environment (environment, callback)`
2662+
2663+Run an action within a custom environment. Only the environment
2664+variables given by `environment` will be set, when `callback` is
2665+called. The original environment is restored after this function
2666+finishes, even if an error occurs while running the callback
2667+action.
2668+
2669+Parameters:
2670+
2671+`environment`
2672+: Environment variables and their values to be set before
2673+ running `callback`. (table with string keys and string
2674+ values)
2675+
2676+`callback`
2677+: Action to execute in the custom environment (function)
2678+
2679+Returns:
2680+
2681+- The result(s) of the call to `callback`
2682+
2683+### with\_temporary\_directory {#system-with_temporary_directory}
2684+
2685+`with_temporary_directory ([parent_dir,] templ, callback)`
2686+
2687+Create and use a temporary directory inside the given directory.
2688+The directory is deleted after the callback returns.
2689+
2690+Parameters:
2691+
2692+`parent_dir`
2693+: Parent directory to create the directory in (string). If this
2694+ parameter is omitted, the system's canonical temporary
2695+ directory is used.
2696+
2697+`templ`
2698+: Directory name template (string).
2699+
2700+`callback`
2701+: Function which takes the name of the temporary directory as its
2702+ first argument (function).
2703+
2704+Returns:
2705+
2706+- The result of the call to `callback`.
2707+
2708+### with\_working\_directory {#system-with_working_directory}
2709+
2710+`with_working_directory (directory, callback)`
2711+
2712+Run an action within a different directory. This function will
2713+change the working directory to `directory`, execute `callback`,
2714+then switch back to the original working directory, even if an
2715+error occurs while running the callback action.
2716+
2717+Parameters:
2718+
2719+`directory`
2720+: Directory in which the given `callback` should be executed
2721+ (string)
2722+
2723+`callback`
2724+: Action to execute in the given directory (function)
2725+
2726+Returns:
2727+
2728+- The result(s) of the call to `callback`
pandoc.cabal+2
@@ -394,6 +394,7 @@ library
394 blaze-markup >= 0.8 && < 0.9,
394 blaze-markup >= 0.8 && < 0.9,
395 vector >= 0.10 && < 0.13,
395 vector >= 0.10 && < 0.13,
396 hslua >= 1.0.1 && < 1.1,
396 hslua >= 1.0.1 && < 1.1,
397+ hslua-module-system >= 0.2 && < 0.3,
397 hslua-module-text >= 0.2 && < 0.3,
398 hslua-module-text >= 0.2 && < 0.3,
398 binary >= 0.5 && < 0.11,
399 binary >= 0.5 && < 0.11,
399 SHA >= 1.6 && < 1.7,
400 SHA >= 1.6 && < 1.7,
@@ -591,6 +592,7 @@ library
591 Text.Pandoc.Lua.Marshaling.ReaderOptions,
592 Text.Pandoc.Lua.Marshaling.ReaderOptions,
592 Text.Pandoc.Lua.Module.MediaBag,
593 Text.Pandoc.Lua.Module.MediaBag,
593 Text.Pandoc.Lua.Module.Pandoc,
594 Text.Pandoc.Lua.Module.Pandoc,
595+ Text.Pandoc.Lua.Module.System,
594 Text.Pandoc.Lua.Module.Utils,
596 Text.Pandoc.Lua.Module.Utils,
595 Text.Pandoc.Lua.Packages,
597 Text.Pandoc.Lua.Packages,
596 Text.Pandoc.Lua.Util,
598 Text.Pandoc.Lua.Util,
src/Text/Pandoc/Lua/Module/System.hsnew file+34
@@ -0,0 +1,34 @@
1+{- |
2+ Module : Text.Pandoc.Lua.Module.System
3+ Copyright : © 2019 Albert Krewinkel
4+ License : GNU GPL, version 2 or above
5+
6+ Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
7+ Stability : alpha
8+
9+Pandoc's system Lua module.
10+-}
11+module Text.Pandoc.Lua.Module.System
12+ ( pushModule
13+ ) where
14+
15+import Prelude
16+import Foreign.Lua (Lua, NumResults)
17+import Foreign.Lua.Module.System (arch, env, getwd, os,
18+ with_env, with_tmpdir, with_wd)
19+import Text.Pandoc.Lua.Util (addFunction, addField)
20+
21+import qualified Foreign.Lua as Lua
22+
23+-- | Push the pandoc.system module on the Lua stack.
24+pushModule :: Lua NumResults
25+pushModule = do
26+ Lua.newtable
27+ addField "arch" arch
28+ addField "os" os
29+ addFunction "environment" env
30+ addFunction "get_current_directory" getwd
31+ addFunction "with_environment" with_env
32+ addFunction "with_temp_directory" with_tmpdir
33+ addFunction "with_working_directory" with_wd
34+ return 1
src/Text/Pandoc/Lua/Packages.hs+2
@@ -25,6 +25,7 @@ import Text.Pandoc.Class (readDataFile, runIO, setUserDataDir)
25import qualified Foreign.Lua as Lua
25import qualified Foreign.Lua as Lua
26import Text.Pandoc.Lua.Module.Pandoc as Pandoc
26import Text.Pandoc.Lua.Module.Pandoc as Pandoc
27import Text.Pandoc.Lua.Module.MediaBag as MediaBag
27import Text.Pandoc.Lua.Module.MediaBag as MediaBag
28+import Text.Pandoc.Lua.Module.System as System
28import Text.Pandoc.Lua.Module.Utils as Utils
29import Text.Pandoc.Lua.Module.Utils as Utils
29
30
30-- | Parameters used to create lua packages/modules.
31-- | Parameters used to create lua packages/modules.
@@ -52,6 +53,7 @@ pandocPackageSearcher pkgParams pkgName =
52 "pandoc" -> let datadir = luaPkgDataDir pkgParams
53 "pandoc" -> let datadir = luaPkgDataDir pkgParams
53 in pushWrappedHsFun (Pandoc.pushModule datadir)
54 in pushWrappedHsFun (Pandoc.pushModule datadir)
54 "pandoc.mediabag" -> pushWrappedHsFun MediaBag.pushModule
55 "pandoc.mediabag" -> pushWrappedHsFun MediaBag.pushModule
56+ "pandoc.system" -> pushWrappedHsFun System.pushModule
55 "pandoc.utils" -> let datadir = luaPkgDataDir pkgParams
57 "pandoc.utils" -> let datadir = luaPkgDataDir pkgParams
56 in pushWrappedHsFun (Utils.pushModule datadir)
58 in pushWrappedHsFun (Utils.pushModule datadir)
57 _ -> searchPureLuaLoader
59 _ -> searchPureLuaLoader
stack.yaml+1
@@ -14,6 +14,7 @@ extra-deps:
14- pandoc-citeproc-0.16.2
14- pandoc-citeproc-0.16.2
15- ipynb-0.1
15- ipynb-0.1
16- cmark-gfm-0.2.0
16- cmark-gfm-0.2.0
17+- hslua-module-system-0.2.0
17ghc-options:
18ghc-options:
18 "$locals": -Wall -fno-warn-unused-do-bind -Wincomplete-record-updates -Wnoncanonical-monad-instances -Wnoncanonical-monadfail-instances -Wincomplete-uni-patterns -Widentities -Wcpp-undef -fhide-source-paths
19 "$locals": -Wall -fno-warn-unused-do-bind -Wincomplete-record-updates -Wnoncanonical-monad-instances -Wnoncanonical-monadfail-instances -Wincomplete-uni-patterns -Widentities -Wcpp-undef -fhide-source-paths
19resolver: lts-13.17
20resolver: lts-13.17
test/Tests/Lua.hs+5
@@ -185,6 +185,11 @@ tests = map (localOption (QuickCheckTests 20))
185 Lua.liftIO . assertEqual "no accessor" (("hi", ["moin"], []) :: Attr)
185 Lua.liftIO . assertEqual "no accessor" (("hi", ["moin"], []) :: Attr)
186 =<< Lua.peek Lua.stackTop
186 =<< Lua.peek Lua.stackTop
187
187
188+ , testCase "module `pandoc.system` is present" . runLua' $ do
189+ Lua.getglobal' "pandoc.system"
190+ ty <- Lua.ltype Lua.stackTop
191+ Lua.liftIO $ assertEqual "module should be a table" Lua.TypeTable ty
192+
188 , testCase "informative error messages" . runLua' $ do
193 , testCase "informative error messages" . runLua' $ do
189 Lua.pushboolean True
194 Lua.pushboolean True
190 err <- Lua.peekEither Lua.stackTop
195 err <- Lua.peekEither Lua.stackTop