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.










share|improve this question


























    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.










    share|improve this question
























      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.










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 17 at 21:11









      jbowman

      424




      424
























          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






          share|improve this answer





















          • 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










          • 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










          • @tarleb OK, thank you - i'll definitely take a look into that!
            – jbowman
            10 hours ago













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          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

























          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






          share|improve this answer





















          • 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










          • 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










          • @tarleb OK, thank you - i'll definitely take a look into that!
            – jbowman
            10 hours ago

















          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






          share|improve this answer





















          • 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










          • 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










          • @tarleb OK, thank you - i'll definitely take a look into that!
            – jbowman
            10 hours ago















          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






          share|improve this answer












          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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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 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










          • 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










          • @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












          • 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












          • @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




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Saint-Aignan (Tarn-et-Garonne)

          Volksrepublik China

          How to test boost logger output in unit testing?