From daef24f02269f1e0041fb222d9bb87cda4537088 Mon Sep 17 00:00:00 2001
From: Albert Krewinkel <albert@zeitkraut.de>
Date: Fri, 15 Jul 2022 21:22:33 +0200
Subject: [PATCH] Lua: extend pandoc.system module. (#8184)

The module now has the additional functions `list_directory`,
`make_directory`, and `remove_directory`. This makes it easier to write
cross-platform scripts that need to inspect or modify the file system.
---
 doc/lua-filters.md                   | 54 ++++++++++++++++++++++++++++
 src/Text/Pandoc/Lua/Module/System.hs |  5 ++-
 2 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/doc/lua-filters.md b/doc/lua-filters.md
index 5f533ecfe..b4ac31479 100644
--- a/doc/lua-filters.md
+++ b/doc/lua-filters.md
@@ -4423,6 +4423,60 @@ Returns:
 
 -   The current working directory (string).
 
+### list\_directory {#pandoc.system.list_directory}
+
+`list_directory ([directory])`
+
+List the contents of a directory.
+
+Parameters:
+
+`directory`
+:   Path of the directory whose contents should be listed
+    (string). Defaults to `.`.
+
+Returns:
+
+-   A table of all entries in `directory` without the special
+    entries `.` and `..`. (list of strings)
+
+### make\_directory {#pandoc.system.make_directory}
+
+`make_directory (dirname [, create_parent])`
+
+Create a new directory which is initially empty, or as near to
+empty as the operating system allows. The function throws an
+error if the directory cannot be created, e.g., if the parent
+directory does not exist or if a directory of the same name is
+already present.
+
+If the optional second parameter is provided and truthy, then all
+directories, including parent directories, are created as
+necessary.
+
+Parameters:
+
+`dirname`
+:   name of the new directory (string)
+
+`create_parent`
+:   create parent directories if necessary (boolean)
+
+### remove\_directory {#pandoc.system.remove_directory}
+
+`remove_directory (dirname [, recursive])`
+
+Remove an existing, empty directory. If `recursive` is given,
+then delete the directory and its contents recursively.
+
+Parameters:
+
+`dirname`
+:   name of the directory to delete (string)
+
+`recursive`
+:   delete content recursively (boolean)
+
 ### with\_environment {#pandoc.system.with_environment}
 
 `with_environment (environment, callback)`
diff --git a/src/Text/Pandoc/Lua/Module/System.hs b/src/Text/Pandoc/Lua/Module/System.hs
index fc0e2303f..70ef1b315 100644
--- a/src/Text/Pandoc/Lua/Module/System.hs
+++ b/src/Text/Pandoc/Lua/Module/System.hs
@@ -16,7 +16,7 @@ module Text.Pandoc.Lua.Module.System
 
 import HsLua
 import HsLua.Module.System
-  (arch, env, getwd, os, with_env, with_tmpdir, with_wd)
+  (arch, env, getwd, ls, mkdir, os, rmdir, with_env, with_tmpdir, with_wd)
 
 -- | Push the pandoc.system module on the Lua stack.
 documentedModule :: LuaError e => Module e
@@ -30,6 +30,9 @@ documentedModule = Module
   , moduleFunctions =
       [ setName "environment" env
       , setName "get_working_directory" getwd
+      , setName "list_directory" ls
+      , setName "make_directory" mkdir
+      , setName "remove_directory" rmdir
       , setName "with_environment" with_env
       , setName "with_temporary_directory" with_tmpdir
       , setName "with_working_directory" with_wd