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

Create output filename based on input filename (and path) #3250

Closed
nichtich opened this issue Nov 22, 2016 · 9 comments
Closed

Create output filename based on input filename (and path) #3250

nichtich opened this issue Nov 22, 2016 · 9 comments

Comments

@nichtich
Copy link
Contributor

As discussed on pandoc-discuss, a method to map input filename to output filename would be nice. This

pandoc path/to/file.md -o path/to/file.pdf

could be abbreviated as any of

pandoc path/to/file.md -o {}.pdf
pandoc path/to/file.md -t pdf -o {}

and maybe also

pandoc path/to/file.md -o .pdf
@jgm
Copy link
Owner

jgm commented Nov 22, 2016

This feels like needless complexity to me. I think pandoc
should function like a regular unix tool, without too many
unpredictable surprises.

One can always create a wrapper, or even

F=path/to/file pandoc $F.md -o $F.pdf

Note also that there is not always an input filename,
and sometimes there is more than one input filename.

@jgm
Copy link
Owner

jgm commented Nov 22, 2016

An even better approach is to use a Makefile with
a rule like

%.pdf: %.md
        pandoc $< -o $@

Then you just need to type

make path/to/my.pdf

@ickc
Copy link
Contributor

ickc commented Nov 22, 2016

On the other hand, some tools similar to pandoc does this, some examples are mmd and latexmk:

multimarkdown file.md # equiv. to multimarkdown file.md > file.html
latexmk -pdf file.tex # output to file.pdf

A syntatic sugar in pandoc can be helpful, something like

pandoc -t pdf file.md
pandoc -t html -o "" file.md

For {}, I am afraid it might interfere the use of find and xargs.

The first example is output like pdf, docx, epub that probably is not for stdin. The second example has an empty string for -o flag. May be only the later is needed for simplicity and generalization. Basically it's like if the output file is an empty string, pandoc will try its best to figure out what the output string should be. In case of multi-files input, it might use the first filename or just emit error. In the case of stdin input, either error or to stdout.

@ickc
Copy link
Contributor

ickc commented Nov 22, 2016

And if the use of -o flag like this is too "un-Unix", may be use another flag, say, -i for this. kind of like the -i in sed. (although it is not strictly in-place when the IO formats are not the same)

@ickc
Copy link
Contributor

ickc commented Nov 22, 2016

Note that the original moltivation in pandoc discuss is that it might be useful for other (especially new) users, because this ends up in something I need to do in my pandoc.sh or makefile like above. Note that a similar trick can be done by bash parameter expansion. So in the end it is just for a syntatic sugar that the end users might soon or later need to build themselves. However, I figure if the feature is not here for a decade, probably there's a reason for that. So if it seems to introduce too much complexity or can cause potential confusion, I think you should feel free to close it.

@anishpatel
Copy link

F=path/to/file pandoc $F.md -o $F.pdf

This does not work because the shell would expand $F before it gets set.

An even better approach is to use a Makefile with a rule like

This requires using a Makefile. When writing one-off documents, there isn't a need to create a Makefile or using some other build system.

Note that a similar trick can be done by bash parameter expansion.

I couldn't figure out what parameter expansion would work in this case. I tried $_, but that takes the argument from the last command rather than from the same command.

The best shell workaround I could come up with is:

pandoc -s -o foo.{docx,md}

Though does not feel clean to me as I like to write in the order of input → output.


curl is a very widely used command, and I appreciate their -O flag which automatically specifies the output file name from the URL, whereas -o requires a filename. I think it would be nice if Pandoc also worked like this too.

@tarleb
Copy link
Collaborator

tarleb commented Apr 14, 2020

You may have missed this detail mentioned above, which makes it impossible to port curls -O behavior to pandoc:

Note also that there is not always an input filename, and sometimes there is more than one input filename.

As for alternatives: how about ( F=foo.md; pandoc $F -o ${F%.*}.pdf )

@dotancohen
Copy link

dotancohen commented Jun 27, 2024

Note also that there is not always an input filename, and sometimes there is more than one input filename.

And in those circumstances, pandoc could generate an error.

There is already precedent for option dependencies, for instance the -o flag is mandatory when the -t flag is "odt" but not mandatory for some other file types such as md.

curl is a very widely used command, and I appreciate their -O flag which automatically specifies the output file name from the URL, whereas -o requires a filename.

I actually tried -O in Pandoc and expected it to work. I fully support the proposal that the -O flag, when possible, deduce the output filename from the input filename - it is consistent with other tools.

@jgm
Copy link
Owner

jgm commented Jun 27, 2024

One thing I like about pandoc's current behavior is that it won't overwrite files unless you explicitly specify them on the command line. With -O, this guarantee disappears. pandoc file.md would overwrite file.html, even though you don't specify file.html and don't even specify -t html.

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

No branches or pull requests

6 participants