git.iko.soy/typeable/pandoc
on master help sign in
Lua: add `pandoc.system` module (#5468)

The `system` Lua module provides utility functions to interact with the operating- and file system. E.g. print(pandoc.system.get_current_directory()) or pandoc.system.with_temporary_directory('tikz', function (dir) -- write and compile a TikZ file with pdflatex end)

commit786594b23be4a757e95776f782527c8a98fbaab5 authorAlbert Krewinkel <albert@zeitkraut.de> dateSat 4 May 2019, 07:06 (7 years ago) parent4f260c9
7 files changed+153
data/pandoc.lua+1 doc/lua-filters.md+108 pandoc.cabal+2 src/Text/Pandoc/Lua/Module/System.hs+34 src/Text/Pandoc/Lua/Packages.hs+2 stack.yaml+1 test/Tests/Lua.hs+5
data/pandoc.lua+1
@@ -27,6 +27,7 @@ local M = {}
2727-- Re-export bundled modules
2828M.List = require 'pandoc.List'
2929M.mediabag = require 'pandoc.mediabag'
30+M.system = require 'pandoc.system'
3031M.utils = require 'pandoc.utils'
3132M.text = require 'text'
3233
doc/lua-filters.md+108
@@ -2618,3 +2618,111 @@ Parameters:
26182618
26192619Returns: a new list containing all items for which \`test\`
26202620was 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
394394 blaze-markup >= 0.8 && < 0.9,
395395 vector >= 0.10 && < 0.13,
396396 hslua >= 1.0.1 && < 1.1,
397+ hslua-module-system >= 0.2 && < 0.3,
397398 hslua-module-text >= 0.2 && < 0.3,
398399 binary >= 0.5 && < 0.11,
399400 SHA >= 1.6 && < 1.7,
@@ -591,6 +592,7 @@ library
591592 Text.Pandoc.Lua.Marshaling.ReaderOptions,
592593 Text.Pandoc.Lua.Module.MediaBag,
593594 Text.Pandoc.Lua.Module.Pandoc,
595+ Text.Pandoc.Lua.Module.System,
594596 Text.Pandoc.Lua.Module.Utils,
595597 Text.Pandoc.Lua.Packages,
596598 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)
2525import qualified Foreign.Lua as Lua
2626import Text.Pandoc.Lua.Module.Pandoc as Pandoc
2727import Text.Pandoc.Lua.Module.MediaBag as MediaBag
28+import Text.Pandoc.Lua.Module.System as System
2829import Text.Pandoc.Lua.Module.Utils as Utils
2930
3031-- | Parameters used to create lua packages/modules.
@@ -52,6 +53,7 @@ pandocPackageSearcher pkgParams pkgName =
5253 "pandoc" -> let datadir = luaPkgDataDir pkgParams
5354 in pushWrappedHsFun (Pandoc.pushModule datadir)
5455 "pandoc.mediabag" -> pushWrappedHsFun MediaBag.pushModule
56+ "pandoc.system" -> pushWrappedHsFun System.pushModule
5557 "pandoc.utils" -> let datadir = luaPkgDataDir pkgParams
5658 in pushWrappedHsFun (Utils.pushModule datadir)
5759 _ -> searchPureLuaLoader
stack.yaml+1
@@ -14,6 +14,7 @@ extra-deps:
1414- pandoc-citeproc-0.16.2
1515- ipynb-0.1
1616- cmark-gfm-0.2.0
17+- hslua-module-system-0.2.0
1718ghc-options:
1819 "$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
1920resolver: lts-13.17
test/Tests/Lua.hs+5
@@ -185,6 +185,11 @@ tests = map (localOption (QuickCheckTests 20))
185185 Lua.liftIO . assertEqual "no accessor" (("hi", ["moin"], []) :: Attr)
186186 =<< Lua.peek Lua.stackTop
187187
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+
188193 , testCase "informative error messages" . runLua' $ do
189194 Lua.pushboolean True
190195 err <- Lua.peekEither Lua.stackTop