pandoc / lua-filters

A collection of lua filters for pandoc
MIT License
611 stars 166 forks source link

Feature request: Relative file import in include-files filter #102

Closed gabyx closed 3 years ago

gabyx commented 4 years ago

Would it make sense to add an attribute relative-to-current-dir=true or something such that, all paths or better all relative paths are resolved to the current file path we are currently at (maybe not so easy to distinguish platform independent, relative vs absolute paths). Therefore maybe, just replace a variable "${current-dir}/b/c.md" in all paths, which would give a deterministic and consistent way of relative imports :)?

@tarleb: What do you think?

In a/b.md

``` { .include}
${current-dir}/b/c.md
```

or In a/b.md

``` { .include relative-to-current-dir=true}
./b/c.md
```
tarleb commented 4 years ago

This is a good idea, but poses some problems. Lua, by default does not include the tools to deal with path modifications. Paths are platform dependent, with path separators \ on Windows and / on Linux and Mac. There are two possibilities to support path modifying functions such as dirname or is_relative:

  1. Write a pure Lua implementation, or
  2. expose the path handling library used by pandoc.

Neither is especially difficult, but both options require more work than I'm willing/able to invest right now.

Patches would be most welcome.

gabyx commented 4 years ago

I can certainly do a PR. What do you mean with pure Lua? Isn’t there a std lib for path modifications? The path handling library by pandoc is haskell, right? With a python filter this would be really easy to acomplish, but I dont know about the walk_**** functions...

tarleb commented 4 years ago

Great!

Lua aims to run on any platform that can be targeted with ANSI C. The latter has no concept of paths, so neither has Lua. Path libraries for Lua exist, but are all written in C, and thereby not an option. The necessary path manipulations should still be doable by pure string manipulations.

The path library used in pandoc is filepath. If you are interested in how this can be exposed to Lua, check hslua-module-system, which exposes selected system and file related functions. Exposing filepath would best be done by adding the necessary bindings to that package.

gabyx commented 4 years ago

Probably these functions are the only ones which give enough access to do almost if not all path handling issues in filters:

Any help and review is kindly welcome. I will try next week what I can do, I think I can directly build and test the hslua module.

gabyx commented 4 years ago

What would be the next step to test a merged PR: https://github.com/hslua/hslua-module-system/pull/1 with this include-filter. How to we get these changes here?

Can we just version check the pandoc version in the include-files filter (is it possible?), such that we error out if somebody use a older pandoc version with no lua file manipulations functions...?

gabyx commented 4 years ago

Got it: :-) See https://github.com/jgm/pandoc/pull/6565

lmunch commented 3 years ago

@gabyx I think latest changes to include-files will solve this issue. Please check.

gabyx commented 3 years ago

@lmunch Thats really greate to hear: Is the doc already up to date, because I ve not seen any explanation on the feature?

lmunch commented 3 years ago

@gabyx See the Recursive transclusion section. It's default behaviour now, so no feature flag.

gabyx commented 3 years ago

@lmunch : Thanks for the effort! Its great! Sorry I was to impatient to read carefully!

lmunch commented 3 years ago

Resolved with #173