declaring arbitrary variables for pandoc conversion using YAML metadata block
up vote
1
down vote
favorite
I've only recently discovered Pandoc, so I'm still getting to used to it a lot of its features. It looks like an incredibly useful tool and I'm excited to find out some applications for it. I've been consulting the User's Guide, and while there is a section on what I'd like to know, I can't seem to get the desired output. I'm not sure if I'm reading the entry correctly.
Put simply, I have a document in .markdown
which acts as a template. From this template, I'd like to produce several other documents (probably .odt
and .docx
). These documents will be mostly identical, apart from a few pieces of information which I'd like to change. What I'd like to know is, is it possible to change these pieces of information by declaring a variable in the YAML metadata at the top of document.
For example, say I had the following in my .markdown
template:
---
key-one: "value-one"
key-two: "value-two"
key-three: "value-three"
---
# DocumentTitle
## DocumentSubtitle
This line contains the first value, called $key-one$
This line contains the second value, called $key-two$
This line contains the third value, called $key-three$
Is there a way that I can get pandoc to replace the 'placeholders' i.e. key-one
, key-two
, etc., with the information that is declared in the YAML metadata? This would result in:
This line contains the first value, called value-one
This line contains the second value, called value-two
This line contains the third value, called value-three
On the User's Guide, it says:
If a variable is not set, pandoc will look for the key in the document’s metadata – which can be set using either YAML metadata blocks or with the --metadata option.
From what I can find on the User's Guide, it seems that I can't declare values arbitrarily in the metadata. I have found some information about Pandoc templates, but I'm not sure how I would have to edit these (or create a custom one) to get the desired output.
Please let me know if anything isn't clear and I will try to be more specific. Thanks in advance.
yaml pandoc odt
add a comment |
up vote
1
down vote
favorite
I've only recently discovered Pandoc, so I'm still getting to used to it a lot of its features. It looks like an incredibly useful tool and I'm excited to find out some applications for it. I've been consulting the User's Guide, and while there is a section on what I'd like to know, I can't seem to get the desired output. I'm not sure if I'm reading the entry correctly.
Put simply, I have a document in .markdown
which acts as a template. From this template, I'd like to produce several other documents (probably .odt
and .docx
). These documents will be mostly identical, apart from a few pieces of information which I'd like to change. What I'd like to know is, is it possible to change these pieces of information by declaring a variable in the YAML metadata at the top of document.
For example, say I had the following in my .markdown
template:
---
key-one: "value-one"
key-two: "value-two"
key-three: "value-three"
---
# DocumentTitle
## DocumentSubtitle
This line contains the first value, called $key-one$
This line contains the second value, called $key-two$
This line contains the third value, called $key-three$
Is there a way that I can get pandoc to replace the 'placeholders' i.e. key-one
, key-two
, etc., with the information that is declared in the YAML metadata? This would result in:
This line contains the first value, called value-one
This line contains the second value, called value-two
This line contains the third value, called value-three
On the User's Guide, it says:
If a variable is not set, pandoc will look for the key in the document’s metadata – which can be set using either YAML metadata blocks or with the --metadata option.
From what I can find on the User's Guide, it seems that I can't declare values arbitrarily in the metadata. I have found some information about Pandoc templates, but I'm not sure how I would have to edit these (or create a custom one) to get the desired output.
Please let me know if anything isn't clear and I will try to be more specific. Thanks in advance.
yaml pandoc odt
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I've only recently discovered Pandoc, so I'm still getting to used to it a lot of its features. It looks like an incredibly useful tool and I'm excited to find out some applications for it. I've been consulting the User's Guide, and while there is a section on what I'd like to know, I can't seem to get the desired output. I'm not sure if I'm reading the entry correctly.
Put simply, I have a document in .markdown
which acts as a template. From this template, I'd like to produce several other documents (probably .odt
and .docx
). These documents will be mostly identical, apart from a few pieces of information which I'd like to change. What I'd like to know is, is it possible to change these pieces of information by declaring a variable in the YAML metadata at the top of document.
For example, say I had the following in my .markdown
template:
---
key-one: "value-one"
key-two: "value-two"
key-three: "value-three"
---
# DocumentTitle
## DocumentSubtitle
This line contains the first value, called $key-one$
This line contains the second value, called $key-two$
This line contains the third value, called $key-three$
Is there a way that I can get pandoc to replace the 'placeholders' i.e. key-one
, key-two
, etc., with the information that is declared in the YAML metadata? This would result in:
This line contains the first value, called value-one
This line contains the second value, called value-two
This line contains the third value, called value-three
On the User's Guide, it says:
If a variable is not set, pandoc will look for the key in the document’s metadata – which can be set using either YAML metadata blocks or with the --metadata option.
From what I can find on the User's Guide, it seems that I can't declare values arbitrarily in the metadata. I have found some information about Pandoc templates, but I'm not sure how I would have to edit these (or create a custom one) to get the desired output.
Please let me know if anything isn't clear and I will try to be more specific. Thanks in advance.
yaml pandoc odt
I've only recently discovered Pandoc, so I'm still getting to used to it a lot of its features. It looks like an incredibly useful tool and I'm excited to find out some applications for it. I've been consulting the User's Guide, and while there is a section on what I'd like to know, I can't seem to get the desired output. I'm not sure if I'm reading the entry correctly.
Put simply, I have a document in .markdown
which acts as a template. From this template, I'd like to produce several other documents (probably .odt
and .docx
). These documents will be mostly identical, apart from a few pieces of information which I'd like to change. What I'd like to know is, is it possible to change these pieces of information by declaring a variable in the YAML metadata at the top of document.
For example, say I had the following in my .markdown
template:
---
key-one: "value-one"
key-two: "value-two"
key-three: "value-three"
---
# DocumentTitle
## DocumentSubtitle
This line contains the first value, called $key-one$
This line contains the second value, called $key-two$
This line contains the third value, called $key-three$
Is there a way that I can get pandoc to replace the 'placeholders' i.e. key-one
, key-two
, etc., with the information that is declared in the YAML metadata? This would result in:
This line contains the first value, called value-one
This line contains the second value, called value-two
This line contains the third value, called value-three
On the User's Guide, it says:
If a variable is not set, pandoc will look for the key in the document’s metadata – which can be set using either YAML metadata blocks or with the --metadata option.
From what I can find on the User's Guide, it seems that I can't declare values arbitrarily in the metadata. I have found some information about Pandoc templates, but I'm not sure how I would have to edit these (or create a custom one) to get the desired output.
Please let me know if anything isn't clear and I will try to be more specific. Thanks in advance.
yaml pandoc odt
yaml pandoc odt
asked Nov 17 at 21:11
jbowman
424
424
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Pandoc templates only contain the header/footer etc. around the document body text, which gets placed where you see $body$
in the template. So templates cannot be used to substitute variables in the document body.
For that, you could use this pandoc filter, save it to say meta-vars.lua
:
local vars = {}
function get_vars (meta)
for k, v in pairs(meta) do
if v.t == 'MetaInlines' then
vars["$" .. k .. "$"] = {table.unpack(v)}
end
end
end
function replace (el)
if vars[el.text] then
return pandoc.Span(vars[el.text])
else
return el
end
end
return {{Meta = get_vars}, {Str = replace}}
And call it with pandoc input.md --lua-filter meta-vars.lua
great, thank you so much - much appreciated! just what i was looking for. currently, it substitutes single variables, but not multiple variables contained in the same<p>
tag. converting to HTML shows thatpandoc.Span
only wraps the final variable in a span tag. it's not a big problem, but maybe the above can be tweaked to change that behaviour... anyway, thanks again!
– jbowman
20 hours ago
glad to help! hm.. what do you mean with "multiple variables contained in the same <p> tag"? in the metadata, or in the body text? the variable-names should not have spaces in them...
– mb21
19 hours ago
sorry, my mistake - what i thought was causing the problem was just a formatting error i think. if i put the placeholders in the markdown file on a single line, then it seems to solve the problem. the issue i'm having now is that thespan
tag automatically adds a space after it, which makes sense, unless you need some punctuation directly after. a string of variables like$author$, $editor$, and $publisher$
would produce the string "author , editor , and publisher" with spaced commas. sorry if this doesn't make much sense - it's hard to explain in a comment.
– jbowman
12 hours ago
@jbowman I think I see what's going on: placeholders need to be separate strings, i.e. separated by spaces or other elements. Try using empty spans as separators like this:$author${}, $editor${},
. Here is an experimental filter to fix this problem.
– tarleb
11 hours ago
@tarleb OK, thank you - i'll definitely take a look into that!
– jbowman
10 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Pandoc templates only contain the header/footer etc. around the document body text, which gets placed where you see $body$
in the template. So templates cannot be used to substitute variables in the document body.
For that, you could use this pandoc filter, save it to say meta-vars.lua
:
local vars = {}
function get_vars (meta)
for k, v in pairs(meta) do
if v.t == 'MetaInlines' then
vars["$" .. k .. "$"] = {table.unpack(v)}
end
end
end
function replace (el)
if vars[el.text] then
return pandoc.Span(vars[el.text])
else
return el
end
end
return {{Meta = get_vars}, {Str = replace}}
And call it with pandoc input.md --lua-filter meta-vars.lua
great, thank you so much - much appreciated! just what i was looking for. currently, it substitutes single variables, but not multiple variables contained in the same<p>
tag. converting to HTML shows thatpandoc.Span
only wraps the final variable in a span tag. it's not a big problem, but maybe the above can be tweaked to change that behaviour... anyway, thanks again!
– jbowman
20 hours ago
glad to help! hm.. what do you mean with "multiple variables contained in the same <p> tag"? in the metadata, or in the body text? the variable-names should not have spaces in them...
– mb21
19 hours ago
sorry, my mistake - what i thought was causing the problem was just a formatting error i think. if i put the placeholders in the markdown file on a single line, then it seems to solve the problem. the issue i'm having now is that thespan
tag automatically adds a space after it, which makes sense, unless you need some punctuation directly after. a string of variables like$author$, $editor$, and $publisher$
would produce the string "author , editor , and publisher" with spaced commas. sorry if this doesn't make much sense - it's hard to explain in a comment.
– jbowman
12 hours ago
@jbowman I think I see what's going on: placeholders need to be separate strings, i.e. separated by spaces or other elements. Try using empty spans as separators like this:$author${}, $editor${},
. Here is an experimental filter to fix this problem.
– tarleb
11 hours ago
@tarleb OK, thank you - i'll definitely take a look into that!
– jbowman
10 hours ago
add a comment |
up vote
2
down vote
accepted
Pandoc templates only contain the header/footer etc. around the document body text, which gets placed where you see $body$
in the template. So templates cannot be used to substitute variables in the document body.
For that, you could use this pandoc filter, save it to say meta-vars.lua
:
local vars = {}
function get_vars (meta)
for k, v in pairs(meta) do
if v.t == 'MetaInlines' then
vars["$" .. k .. "$"] = {table.unpack(v)}
end
end
end
function replace (el)
if vars[el.text] then
return pandoc.Span(vars[el.text])
else
return el
end
end
return {{Meta = get_vars}, {Str = replace}}
And call it with pandoc input.md --lua-filter meta-vars.lua
great, thank you so much - much appreciated! just what i was looking for. currently, it substitutes single variables, but not multiple variables contained in the same<p>
tag. converting to HTML shows thatpandoc.Span
only wraps the final variable in a span tag. it's not a big problem, but maybe the above can be tweaked to change that behaviour... anyway, thanks again!
– jbowman
20 hours ago
glad to help! hm.. what do you mean with "multiple variables contained in the same <p> tag"? in the metadata, or in the body text? the variable-names should not have spaces in them...
– mb21
19 hours ago
sorry, my mistake - what i thought was causing the problem was just a formatting error i think. if i put the placeholders in the markdown file on a single line, then it seems to solve the problem. the issue i'm having now is that thespan
tag automatically adds a space after it, which makes sense, unless you need some punctuation directly after. a string of variables like$author$, $editor$, and $publisher$
would produce the string "author , editor , and publisher" with spaced commas. sorry if this doesn't make much sense - it's hard to explain in a comment.
– jbowman
12 hours ago
@jbowman I think I see what's going on: placeholders need to be separate strings, i.e. separated by spaces or other elements. Try using empty spans as separators like this:$author${}, $editor${},
. Here is an experimental filter to fix this problem.
– tarleb
11 hours ago
@tarleb OK, thank you - i'll definitely take a look into that!
– jbowman
10 hours ago
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Pandoc templates only contain the header/footer etc. around the document body text, which gets placed where you see $body$
in the template. So templates cannot be used to substitute variables in the document body.
For that, you could use this pandoc filter, save it to say meta-vars.lua
:
local vars = {}
function get_vars (meta)
for k, v in pairs(meta) do
if v.t == 'MetaInlines' then
vars["$" .. k .. "$"] = {table.unpack(v)}
end
end
end
function replace (el)
if vars[el.text] then
return pandoc.Span(vars[el.text])
else
return el
end
end
return {{Meta = get_vars}, {Str = replace}}
And call it with pandoc input.md --lua-filter meta-vars.lua
Pandoc templates only contain the header/footer etc. around the document body text, which gets placed where you see $body$
in the template. So templates cannot be used to substitute variables in the document body.
For that, you could use this pandoc filter, save it to say meta-vars.lua
:
local vars = {}
function get_vars (meta)
for k, v in pairs(meta) do
if v.t == 'MetaInlines' then
vars["$" .. k .. "$"] = {table.unpack(v)}
end
end
end
function replace (el)
if vars[el.text] then
return pandoc.Span(vars[el.text])
else
return el
end
end
return {{Meta = get_vars}, {Str = replace}}
And call it with pandoc input.md --lua-filter meta-vars.lua
answered yesterday
mb21
16.9k35881
16.9k35881
great, thank you so much - much appreciated! just what i was looking for. currently, it substitutes single variables, but not multiple variables contained in the same<p>
tag. converting to HTML shows thatpandoc.Span
only wraps the final variable in a span tag. it's not a big problem, but maybe the above can be tweaked to change that behaviour... anyway, thanks again!
– jbowman
20 hours ago
glad to help! hm.. what do you mean with "multiple variables contained in the same <p> tag"? in the metadata, or in the body text? the variable-names should not have spaces in them...
– mb21
19 hours ago
sorry, my mistake - what i thought was causing the problem was just a formatting error i think. if i put the placeholders in the markdown file on a single line, then it seems to solve the problem. the issue i'm having now is that thespan
tag automatically adds a space after it, which makes sense, unless you need some punctuation directly after. a string of variables like$author$, $editor$, and $publisher$
would produce the string "author , editor , and publisher" with spaced commas. sorry if this doesn't make much sense - it's hard to explain in a comment.
– jbowman
12 hours ago
@jbowman I think I see what's going on: placeholders need to be separate strings, i.e. separated by spaces or other elements. Try using empty spans as separators like this:$author${}, $editor${},
. Here is an experimental filter to fix this problem.
– tarleb
11 hours ago
@tarleb OK, thank you - i'll definitely take a look into that!
– jbowman
10 hours ago
add a comment |
great, thank you so much - much appreciated! just what i was looking for. currently, it substitutes single variables, but not multiple variables contained in the same<p>
tag. converting to HTML shows thatpandoc.Span
only wraps the final variable in a span tag. it's not a big problem, but maybe the above can be tweaked to change that behaviour... anyway, thanks again!
– jbowman
20 hours ago
glad to help! hm.. what do you mean with "multiple variables contained in the same <p> tag"? in the metadata, or in the body text? the variable-names should not have spaces in them...
– mb21
19 hours ago
sorry, my mistake - what i thought was causing the problem was just a formatting error i think. if i put the placeholders in the markdown file on a single line, then it seems to solve the problem. the issue i'm having now is that thespan
tag automatically adds a space after it, which makes sense, unless you need some punctuation directly after. a string of variables like$author$, $editor$, and $publisher$
would produce the string "author , editor , and publisher" with spaced commas. sorry if this doesn't make much sense - it's hard to explain in a comment.
– jbowman
12 hours ago
@jbowman I think I see what's going on: placeholders need to be separate strings, i.e. separated by spaces or other elements. Try using empty spans as separators like this:$author${}, $editor${},
. Here is an experimental filter to fix this problem.
– tarleb
11 hours ago
@tarleb OK, thank you - i'll definitely take a look into that!
– jbowman
10 hours ago
great, thank you so much - much appreciated! just what i was looking for. currently, it substitutes single variables, but not multiple variables contained in the same
<p>
tag. converting to HTML shows that pandoc.Span
only wraps the final variable in a span tag. it's not a big problem, but maybe the above can be tweaked to change that behaviour... anyway, thanks again!– jbowman
20 hours ago
great, thank you so much - much appreciated! just what i was looking for. currently, it substitutes single variables, but not multiple variables contained in the same
<p>
tag. converting to HTML shows that pandoc.Span
only wraps the final variable in a span tag. it's not a big problem, but maybe the above can be tweaked to change that behaviour... anyway, thanks again!– jbowman
20 hours ago
glad to help! hm.. what do you mean with "multiple variables contained in the same <p> tag"? in the metadata, or in the body text? the variable-names should not have spaces in them...
– mb21
19 hours ago
glad to help! hm.. what do you mean with "multiple variables contained in the same <p> tag"? in the metadata, or in the body text? the variable-names should not have spaces in them...
– mb21
19 hours ago
sorry, my mistake - what i thought was causing the problem was just a formatting error i think. if i put the placeholders in the markdown file on a single line, then it seems to solve the problem. the issue i'm having now is that the
span
tag automatically adds a space after it, which makes sense, unless you need some punctuation directly after. a string of variables like $author$, $editor$, and $publisher$
would produce the string "author , editor , and publisher" with spaced commas. sorry if this doesn't make much sense - it's hard to explain in a comment.– jbowman
12 hours ago
sorry, my mistake - what i thought was causing the problem was just a formatting error i think. if i put the placeholders in the markdown file on a single line, then it seems to solve the problem. the issue i'm having now is that the
span
tag automatically adds a space after it, which makes sense, unless you need some punctuation directly after. a string of variables like $author$, $editor$, and $publisher$
would produce the string "author , editor , and publisher" with spaced commas. sorry if this doesn't make much sense - it's hard to explain in a comment.– jbowman
12 hours ago
@jbowman I think I see what's going on: placeholders need to be separate strings, i.e. separated by spaces or other elements. Try using empty spans as separators like this:
$author${}, $editor${},
. Here is an experimental filter to fix this problem.– tarleb
11 hours ago
@jbowman I think I see what's going on: placeholders need to be separate strings, i.e. separated by spaces or other elements. Try using empty spans as separators like this:
$author${}, $editor${},
. Here is an experimental filter to fix this problem.– tarleb
11 hours ago
@tarleb OK, thank you - i'll definitely take a look into that!
– jbowman
10 hours ago
@tarleb OK, thank you - i'll definitely take a look into that!
– jbowman
10 hours ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355628%2fdeclaring-arbitrary-variables-for-pandoc-conversion-using-yaml-metadata-block%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown