title: Pandoc: in a .md file, how to modifiy the YAML metadata block and put the modified block in markdown format using a lua filter
body: <p>I want to transform some parts of the metadata in the YAML header of a block-empty .md file before passing it to a template file. That is, the workflow would be this:</p>
  <pre><code>source_YAML_md_file -&gt; lua_filter -&gt; transformed_YAML_md_file -&gt; template -&gt; xml_file
  </code></pre>
  <p>The lua_filter is meant to perform shuffling on subquestion items (which consist of 3 subitems: 'answer', 'stem' and 'key') and then to assign them the list order number (contents of subitem 'key').</p>
  <p>I don't know how to do the lua filter part.</p>
  <ol>
  <li>YAML sample file: <code>source_YAML.md</code></li>
  </ol>
  <pre><code>---
  category: matching
  
  name: test question
  
  questiontext_header: Test Header
  
  questiontext_body: |
    * Blah, blah, blah.
    * More stuff.
  
  shuffle: T
    
  keys: T
  
  subquestion:
  - key: 1
    stem: |
      Old:
      
        * MacDonald
          + Had
          + a Farm.   
        * E-I-E-I-O
        
    answer: |
      * And on his farm
      * he had a cow 
    
  - key: 2
    stem: |
      With a moo moo here
      
    answer: |
      and a moo moo there
    
  - key: 3
    stem: |
      Twinkle, twinkle,
      
    answer: |
      * Little
      * Star 
  
    
  - key: 4
    stem: |
      How I wonder 
      
    answer: |
      what you are! 
  ---
  </code></pre>
  <ol start="2">
  <li><code>shuffle_filter.lua</code> file:</li>
  </ol>
  <pre><code>function shuffle(list)
      math.randomseed(os.time())
      for i = #list, 2, -1 do
          local j = math.random(i)
          list[i], list[j] = list[j], list[i]
      end
  end
  
  local vars = {}
  
  function get_vars (m)
    for k, v in pairs(m) do 
      print(k, v, #v) 
      if v == m.subquestion and type(v) == 'table' and v.tag == 'MetaList' then
        print('SUBQUESTION')
        for a, b in pairs(v) do
          print('ab___', a, b)
          for c, d in pairs(b) do
            print('cd______', c, d)
          end
        end
        print('shuffling...')
        shuffle(m.subquestion)
        print('modified after shuffling:')
        for a, b in pairs(m.subquestion) do 
          print('ab___', a, b.t, b, #b)
          for c, d in pairs(b) do
            print('__cd____', '', c, d.t, d, #d) 
            for e, f in pairs(d) do
              if f.t == 'Str' then
                print('changing &quot;key&quot; index:')
                print('           ', 'former index:', '', pandoc.utils.stringify(f))
                f = pandoc.MetaInlines(tostring(a))
                print('____ef_____', '', '', e, f.t, f, d[e])
                print('           ', 'current index:', '', pandoc.utils.stringify(f), '\n\n')
              end
            end
          end
        end
      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}}
  -- return {Meta = get_vars}
  </code></pre>
  <p>This horrible code is what I've been experimenting with in order to check if shuffling and changing 'key' contents were possible. It seems they are, but my problem is how to put the modified results into a modified .md file. (The 'code' is based on <a href="https://pandoc.org/lua-filters.html#replacing-placeholders-with-their-metadata-value" rel="nofollow noreferrer">https://pandoc.org/lua-filters.html#replacing-placeholders-with-their-metadata-value</a> ; I used the commented replace function to perform &quot;debugging&quot; using printS here and there... <em>Mea culpa!)</em></p>
  <p>I'm aware of the -s standalone option in:</p>
  <pre><code>$ pandoc --lua-filter=shuffle_filter.lua -f markdown -t markdown -s  source_YAML.md
  </code></pre>
  <p>But this renders the original YAML stuff, not the modified one.</p>
  <p>Thanks in advance for your help.</p>