Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose more system functionalities to Lua #5468

Merged
merged 1 commit into from May 4, 2019
Merged

Conversation

tarleb
Copy link
Collaborator

@tarleb tarleb commented Apr 28, 2019

The system Lua module provides utility functions to interact with the
operating- and file system. Like the text module, the system module
must be loaded before use. E.g.

local system = require 'system'
print(system.currentdir())

@tarleb
Copy link
Collaborator Author

tarleb commented Apr 29, 2019

This adds about 40kB to the executable.

@jgm
Copy link
Owner

jgm commented Apr 29, 2019

What happens if you cd to change directory in a lua filter?
Does this run in a subprocess, affecting only the working directory for the subprocess?
Or does this mean that when pandoc exits, you'll be in a different directory (which would be bad to allow, I think).

@tarleb
Copy link
Collaborator Author

tarleb commented Apr 29, 2019

Indeed. The directory doesn't change after pandoc exits, but with chdir.lua containing (require 'system').chdir '/tmp' and filter.sh being any filter in the current directory we get

$ echo test | pandoc --lua-filter=chdir.lua --filter=filter.sh
Error running filter filter.sh:
Could not find executable filter.sh

I'll have to add withCurrentDirectory somewhere in the Lua filter code.

@tarleb
Copy link
Collaborator Author

tarleb commented Apr 29, 2019

The current working directory and environment will now be restored after each Lua invocation.

@tarleb
Copy link
Collaborator Author

tarleb commented Apr 30, 2019

A bit more context for this PR.

  • The main motivation was to get access to Haskell's functions for temporary files and directories. These would be of great help with tempfile-heavy filters like diagram-generator.
  • Environment modifying functions where added to allow configuration of external programs. Filter authors may want to set or override environment variables before calling out to another executable. Example: temporarily overriding TEXMF* variables before calling pdflatex.
  • The rest is merely for completeness.

Other features that I think could be helpful, but aren't implemented (yet):

  • Platform-dependent creation and canonicalization of file paths;
  • turning relative paths into absolute paths and vice versa.

@jgm
Copy link
Owner

jgm commented May 1, 2019

I am still a bit worried about these environment-polluting things; would it make more sense to add functions like with_environment(env, function)and with_working_directory(dir, function) instead of setenv and cd?

@tarleb
Copy link
Collaborator Author

tarleb commented May 1, 2019

That makes sense and seems like the cleaner solution.

My approach will then be to remove the system module and replace it with a pandoc.sys module which contains just the three functions with_environment, with_working_directory, and with_temp_directory. That should strike a good balance between useful features and unnecessary bloat.

@jgm
Copy link
Owner

jgm commented May 1, 2019

I think it should be harmless to keep the "getter" functions exposed: e.g. getenv and get_current_directory.

@tarleb tarleb force-pushed the lua-system-module branch 2 times, most recently from 7aacf6e to 4c544e8 Compare May 2, 2019 06:50
@tarleb
Copy link
Collaborator Author

tarleb commented May 2, 2019

Should be ok now. I left out getenv, as Lua already comes with os.getenv().

doc/lua-filters.md Show resolved Hide resolved
doc/lua-filters.md Outdated Show resolved Hide resolved
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)
@jgm
Copy link
Owner

jgm commented May 3, 2019

Should be ok now. I left out getenv, as Lua already comes with os.getenv().

Confused about this, since there is still environment -- is that different from os.getenv?

@jkr
Copy link
Collaborator

jkr commented May 4, 2019

Confused about this, since there is still environment -- is that different from os.getenv?

os.getenv takes an argument (os.getenv("HOME")) , while according to the docs, environment returns a table of all env vars.

@jgm jgm merged commit 786594b into jgm:master May 4, 2019
@tarleb tarleb deleted the lua-system-module branch May 4, 2019 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants