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

Specify template in YAML #1958

Closed
davepwsmith opened this issue Feb 19, 2015 · 8 comments
Closed

Specify template in YAML #1958

davepwsmith opened this issue Feb 19, 2015 · 8 comments

Comments

@davepwsmith
Copy link

I can't seem to specify the template in the YAML metadata, as I can for example with the bibliography and CSL style. This would be very useful, as I could then basically customize my pandoc command for each file, without having to remember which arguments to use for specific projects. Am I doing something wrong (template: paper) or is this simply not possible?

@nkalvi
Copy link

nkalvi commented Feb 19, 2015

It is not possible in pandoc 'directly' as far as I know (I'm quite new to pandoc).

But R markdown (which uses pandoc) seems to have what you want:

http://rmarkdown.rstudio.com/html_document_format.html

Custom Templates

You can also replace the underlying pandoc template using the template option:

---
title: "Habits"
output:
  html_document:
    template: quarterly_report.html
---

Consult the documentation on pandoc templates for additional details on templates. You can also study the default HTML template as an example.

@davepwsmith
Copy link
Author

Interesting, but that would add a completely unnecessary piece of software to a workflow that already has its quirks, and so probably isn't desirable. I have no need for the statistical features of R, and so would prefer to stick with using pandoc directly.

@jgm
Copy link
Owner

jgm commented Feb 19, 2015

Template variables aren't well suited for things like path names, because (recall) everything is interpreted as Markdown.

However, YAML fields ending with _ are ignored and contain any kind of content.

So, you could do this: in your documents, use the YAML metadata field template_ to specify the template:

---
title: My title
template_:  my_fancy_template.html
...

Then, extract this using sed in your pandoc command line:

pandoc --template=$(sed -ne '/^template_:/{s/^template_: *//p;q}' $SOURCE) $SOURCE -s

where $SOURCE is the filename. You could automate this
with a small wrapper script or Makefile.

@michaelstepner
Copy link

@jgm, I had the same question as @davepwsmith. Thanks for providing a solution!

I wanted to make the command more flexible by having it automatically detect whether there is a "template_" YAML field. The following command only adds a --template option to pandoc if it finds the YAML field in the source file:

pandoc $(sed -n 's/^template_:[ ][ ]*\(.*\)/\1/p' $SOURCE | sed 's/^\(..*\)$/template=\1/') $SOURCE -s

I then integrated this into my Sublime Text build system (after fighting with escape characters for a while). For anyone interested, my ST3 build system is:

{
    "shell_cmd": "pandoc \\$(sed -n 's/^template_:[ ][ ]*\\(.*\\)/\\1/p' \"$file\" | sed 's/^\\(..*\\)\\$/--template=\\1/') --filter pandoc-citeproc -o \"${file_path}/${file_base_name}.pdf\" \"$file\" && open -a Preview \"${file_path}/${file_base_name}.pdf\"",
    "selector": "text.html.markdown",
    "path": "/usr/texbin:$PATH"
}

This will convert a Markdown file to PDF, using a custom template file if and only if one is specified using a "template_" YAML field.

@jtkiley
Copy link

jtkiley commented Feb 21, 2017

I agree with this idea, and it would be a huge quality of life improvement for me. As it stands, I end up having to create a Makefile for individual documents or copy/paste the template path (when I only ever use a handful of templates). If I could specify the template in YAML (like I can bibs and CSLs, both of which have the same path issue), it would make many things I do (e.g., academic papers, recommendation letters, envelopes, instructions, course materials) much easier.

I appreciate the workaround, and I'll try to figure out an equivalent to that ST3 build in Atom, but it would still be nice to see this built in normally.

@jgm
Copy link
Owner

jgm commented Feb 22, 2017

OK, I'll reopen this for further consideration. But since the source document might be used to generate content in different formats, at the very least this would need to be parameterized on the output format as it is in RMarkdown. See also #2139.

@jgm jgm reopened this Feb 22, 2017
@uliska
Copy link
Contributor

uliska commented Jan 22, 2018

at the very least this would need to be parameterized on the output format

Would it be an option to make this decision depend on whether the given template has an extension?

So

---
template: my-letter.latex
---

would look for my-letter.latex
while

---
template: my-letter
---

would look for a template with a file extension matching the output format?

(I'm commenting on this because I found this issue looking for exactly this functionlatily (so this comment is a +1 too)).

@mb21
Copy link
Collaborator

mb21 commented May 4, 2018

Closing in favour of #4627.

@mb21 mb21 closed this as completed May 4, 2018
arumoy-shome added a commit to arumoy-shome/dotfiles that referenced this issue Aug 7, 2021
Stole this solution from
jgm/pandoc#1958 (comment). This
commit introduces a __get_template function. It returns
`--template=<template>` where <template> is extracted from the SRCFILE
when present. Else it return `--standalone` ie. pandoc uses the default
template.
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

7 participants