joostkremers / pandoc-mode

An Emacs minor mode for interacting with Pandoc.
https://joostkremers.github.io/pandoc-mode/
177 stars 14 forks source link

Give priority to variables defined in YAML over global settings #103

Open newhallroad opened 3 years ago

newhallroad commented 3 years ago

If I am not mistaken, variables defined in settings always take priority over conflicting variables defined in a YAML block in the file itself. I assume this is because the settings in practice get sent to pandoc as CLI options, so pandoc gives priority to them.

However, this creates an unexpected behavior. Say I have a global setting that sets a csl or bibliography option for all html output. I would expect that setting to be a default, but if I set a specific value for one of those variables in a YAML block, then that should take priority.

Is there a way to change this? Perhaps implement behavior to ignore global or even file settings if that specific variable has been set in YAML in the file itself, or in an accompanying YAML file?

As always, huge thanks for your generous work.

joostkremers commented 3 years ago

If I am not mistaken, variables defined in settings always take priority over conflicting variables defined in a YAML block in the file itself. I assume this is because the settings in practice get sent to pandoc as CLI options, so pandoc gives priority to them.

Yes, that's a correct assessment.

However, this creates an unexpected behavior. Say I have a global setting that sets a csl or bibliography option for all html output. I would expect that setting to be a default, but if I set a specific value for one of those variables in a YAML block, then that should take priority.

Yes, from a user POV that makes sense.

It's a historically grown problem... When I started pandoc-mode, Pandoc didn't provide any way to include options within the document itself or to provide default options. The recommended way to process a document was with a Makefile. However, I wanted to be able to define a default set of options so I wouldn't have to drag a Makefile around every time I created a new document. So I implemented project and global settings files. Later, I also added file-local variables so settings could be stored in the document itself.

John MacFarlane has since added YAML blocks and defaults files to specify settings. They serve the same purpose but I never got round to adding support for them in pandoc-mode.

Is there a way to change this? Perhaps implement behavior to ignore global or even file settings if that specific variable has been set in YAML in the file itself, or in an accompanying YAML file?

It's possible, and I agree it should be handled in some way, but it would be a non-trivial change for which I currently lack the time. :slightly_frowning_face:

So my recommendation would be to use one method and stick with it. Either use the functionality pandoc-mode provides, i.e., global settings files for defaults, which you can then override with local settings files or with file-local variables in the document itself, or use the functionality Pandoc provides, i.e., a defaults file, which can be overridden with CLI options or with a YAML file or header block.

Of course, pandoc-mode settings files (or file-local variables) are tied to Emacs and pandoc-mode, so YAML blocks and a defaults file have an advantage there. OTOH when I last looked into them, I got the impression that not all settings can be specified in them, so pandoc-mode seems to have an advantage there. (Though I might be mistaken about this, it's been a while since I looked at them.)

If I ever find the time, I'll add support for YAML blocks and defaults files. For now, I probably need to update the documentation.

joostkremers commented 3 years ago

I should add, of course, that if you use YAML metadata or a defaults file, the settings in them don't show up in the hydra menu and you cannot change them there. In that case, there may not be much point in using pandoc-mode. markdown-mode also provides commands to call an external Markdown processor, which may be sufficient in that case.

newhallroad commented 3 years ago

That all makes perfect sense. Thank you for the suggestions.

hugoroy commented 3 years ago

Hello If the only option configured in pandoc-mode is to set the defaults file to a .yaml file the directory, then wouldn't creating a pdf with pandoc-mode be equivalent to running "pandoc inputfilename -o outputfilename.pdf --defaults defaults-file.yaml"? I am confused as I obtain different results with pandoc-mode creating a pdf and with simply running the command above.

joostkremers commented 3 years ago

If the only option configured in pandoc-mode is to set the defaults file to a .yaml file the directory, then wouldn't creating a pdf with pandoc-mode be equivalent to running "pandoc inputfilename -o outputfilename.pdf --defaults defaults-file.yaml"? I am confused as I obtain different results with pandoc-mode creating a pdf and with simply running the command above.

You can check the pandoc-mode output buffer (C-c / V) to see what command is sent to pandoc from Emacs. It may not be exactly equivalent. Hard to say without more details.

hugoroy commented 3 years ago

Hi Joos, I couldn't see that with C-c / V but I could with the log buffer. Sorry I didn't think about this. It seems the defaults file in the pandoc-mode setting is not passed as an argument to pandoc (expecting -d). I will open an issue (unless you can also reproducte?)

==================================================
Wed Aug 18 23:50:38 2021

Calling pandoc with:

pandoc --read=markdown --write=latex --output=/home/hugo/filepath/filename.pdf

filename.md: Error in pandoc process