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

Lua filter can't be used on pandoc-citeproc output #4196

Closed
jkr opened this issue Dec 25, 2017 · 18 comments
Closed

Lua filter can't be used on pandoc-citeproc output #4196

jkr opened this issue Dec 25, 2017 · 18 comments

Comments

@jkr
Copy link
Collaborator

jkr commented Dec 25, 2017

Native lua filters are used before external filters. This means that you can't use a lua filter on the output of other filters.

This is an issue for me because I have some filters that fix up pandoc-citeproc output (removing repeated parenthetical citations in MLA output). While my use is a bit niche, I can imagine more mainstream use-cases: a filter styling the refs Div that pandoc-citeproc produces.

Any thoughts on the best way to deal with this? Should pandoc-citeproc be a special case? I can always do two passes with a JSON intermediate, but that seems to go against the spirit of using the lua filters in the first place. (Of course, for the time being, I can keep using my old haskell filter for this special case.)

@jkr
Copy link
Collaborator Author

jkr commented Dec 25, 2017

Another example that I came across while converting my filters. One targeted LaTeX style uses a list environment for its works-cited list, so I need a filter that can run through the bibliography entries and put an \item in front of them (as well as wrapping the whole thing in \begin{citedlist}/\end{citedlist}). So, again, this is a filter that needs to run on the other side of pandoc-citeproc.

@jgm
Copy link
Owner

jgm commented Dec 25, 2017 via email

@tarleb
Copy link
Collaborator

tarleb commented Dec 25, 2017 via email

@tarleb
Copy link
Collaborator

tarleb commented Dec 25, 2017 via email

@jkr
Copy link
Collaborator Author

jkr commented Dec 26, 2017

@tarleb: I see that you're starting work on a run_json_filter approach. This seems a bit more convoluted (from a user's point of view) than John's suggestion (filters in any order), but since I don't understand the difficulties of the implementation, I'm in no position to object.

What I would suggest, though, if you go with this approach, you package a pandoc-citeproc.lua filter with pandoc-citeproc, that just runs pandoc.run_json_filter(pandoc-citeproc). This would allow us to maintain some composibility, that would be lost if we had to call one filter from another.

Anecdotal justification: I use filters in a pipeline, and I like to be able to mix and match (e.g., pandoc-citeproc -> fix citations -> get rid of small caps, but sometimes just pandoc-citeproc -> get rid of small caps, and sometimes just fix citations -> get rid of small caps). It would be cumbersome to have to build one filter into another, and I couldn't compose them as easily.

@tarleb
Copy link
Collaborator

tarleb commented Dec 26, 2017

Oh, please do object! I wasn't convinced that adding a function to lua counts as a solution either, and I agree with you that jgm's solution is preferable. I'll work on the run_json_filter some more, but will scratch the claim that it could solve this issue.

@mb21
Copy link
Collaborator

mb21 commented Dec 26, 2017

I may be missing something, but as a user, I’d prefer just having them run in the order specified by —filter

@jkr
Copy link
Collaborator Author

jkr commented Dec 26, 2017 via email

@jkr
Copy link
Collaborator Author

jkr commented Dec 26, 2017 via email

@mb21
Copy link
Collaborator

mb21 commented Dec 26, 2017

you're suggesting putting all filters (JSON and lua) under the same
option tag

Yes.

pandoc looks in different places for the two sorts of filters

So --lua-filter looks in $DATADIR. But --filter looks first in $PATH and if it doesn't find anything looks in $DATADIR (says the MANUAL). So as a user, I could use --filter to order all my non-lua and lua filters. The case where I have a lua-filter on my PATH and I need to order them should be pretty rare (and the user should then probably just remove the lua-filter from the PATH).

@tarleb
Copy link
Collaborator

tarleb commented Dec 26, 2017

Out of curiosity, […] is there a reason why applyLuaFilters and applyFilters have to be separated by extractMedia and applyTransforms?

Initially, Lua filters were run after JSON filters; this changed when we added the pandoc.mediabag module, which requires that maybe return extractMedia (optExtractMedia opts) is run after Lua filters are done. I suppose the relative position of applyTransforms wasn't changed, which is why it's run between both filter runs.

@tarleb
Copy link
Collaborator

tarleb commented Dec 26, 2017

You could have a lua JSON filter (I believe @tarleb wrote a library last year) in your path, so it would be hard to distinguish the two.

I wrote this useful monstrosity, which isn't really a filter in the classical sense but a custom writer which outputs JSON. I don't think there is a lua port of pandocfilters as of now. It should be safe to implement @mb21's proposal. I still don't like special cases, but it might make sense in this case.

@jgm
Copy link
Owner

jgm commented Dec 26, 2017

I think it's probably still worth keeping the --lua-filter vs --filter distinction; that tells pandoc whether to treat a lua file as a pandoc lua filter or a regular json filter. But respecting the command line order of --lua-filter and --filter would be good for many reasons, and would solve this issue.

We need to make sure we don't run into trouble with the mediabag stuff, but that shouldn't be a big obstacle.

@jgm
Copy link
Owner

jgm commented Dec 26, 2017

I've made some changes, I'll put up code to a branch soon.

jgm added a commit that referenced this issue Dec 26, 2017
* Previously we ran all lua filters before JSON filters.
* Now we run filters in the order they are presented on the
  command line, whether lua or JSON.
* The type of `applyFilters` has changed (incompatible API change).
* `applyLuaFilters` has been removed (incompatible API change).
* Bump version to 2.1.

See #4196.
@jgm
Copy link
Owner

jgm commented Dec 26, 2017

See the filter-changes branch.
Note that this contains incompatible API changes in Text.Pandoc.App and would have to appear as pandoc 2.1. I'll probably release some fixes in the 2.0 series first.

@tarleb
Copy link
Collaborator

tarleb commented Dec 26, 2017

@jgm what are your thoughts on #4197, do you see this as a desirable feature? If so, should this be part of the 2.0 series, or should I rebase it vs your filter-changes branch?

@jgm
Copy link
Owner

jgm commented Dec 27, 2017

@tarleb I guess I don't see a compelling reason to add #4197, since we now have a cleaner way of interleaving lua filters and regular json filters. But if you think there's some task that would require running a json filter from a lua filter, perhaps you could describe the use case in more detail in the PR?

jgm added a commit that referenced this issue Dec 29, 2017
* Previously we ran all lua filters before JSON filters.
* Now we run filters in the order they are presented on the
  command line, whether lua or JSON.
* The type of `applyFilters` has changed (incompatible API change).
* `applyLuaFilters` has been removed (incompatible API change).
* Bump version to 2.1.

See #4196.
@jgm
Copy link
Owner

jgm commented Dec 29, 2017

Closed by 8b575db

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants