title: Using a lua filter, how do you convert a Table into json or native text?
body: <p>I want to replace Table blocks within a markdown document, with RawBlocks which will eventually contain latex.</p>
  
  <p>I can find the table blocks in a document using:</p>
  
  <pre><code>function Table(table)
  </code></pre>
  
  <p>However I need to convert each table block to json or native text, so it can be fed to:</p>
  
  <pre><code>local function table2latex(t)
      local latex = pandoc.pipe('pandoc',{'-f','native','-t','latex'},t)
      ...
  </code></pre>
  
  <p>I can not find any examples that do this kind of conversion, having scoured the pandoc documentation and other sites such as github and stackoverflow.</p>
  
  <p>Is it possible or will I need to write a bespoke embedded writer?</p>
  
  <p>EDIT</p>
  
  <p>Following on. The answer below led to the following script:</p>
  
  <pre><code>-- file: boxtables.lua
  --
  -- This filter extracts tables and converts them to latex
  -- The latex is then altered by a sed script to "box" each cell
  -- using "|" and '\hline' as column and row delimiters.
  -- The original document containing the table(s) must have
  -- 'header-includes' containing '\usepackage{longtable,booktabs)}'
  -- The user should have the `jq`and 'sed' utilities installed and the shell
  -- script 'table2latex.sh' executable in the current working directory
  --
  -- #!/bin/sh
  -- latex="$(pandoc -f json -t latex)"
  -- tmpl='{blocks:[{t:"RawBlock",c:["latex",$tab]}],"pandoc-api-version":[1,17,3],meta:{}}'
  -- jq -n --arg tab "$latex" $tmpl
  --
  -- The lua filter can be invoked:
  --    pandoc --lua-filter boxtables.lua -o documentContainingTable.pdf documentContainingTable.md
  --
  -- N.B. 'header-includes' and other details may be set on the command line or as a YAML header e.g.
  --
  -- ---
  -- geometry: landscape, scale=0.9, centering
  -- header-includes:
  --   - \usepackage{longtable,booktabs}
  -- ---
  --
  function table (tab)
    -- create a new pandoc document containing only the table
    local dummy_doc = pandoc.Pandoc(tab)
    -- use table2latex.sh to convert the doc to latex
    local tex_doc = pandoc.utils.run_json_filter(dummy_doc, 'table2latex.sh')
    -- only a single block containing the table will be returned
    return tex_doc.blocks
  end
  
  function rawblock (rb)
    local oldlatex = rb.text
    local sed = [[/^\\begin{longtable}/{s/@{}/&amp;\n/;:a;ta;/\n@{}/!s/\n\(.\)/|\1\n/;ta;s/\n/|/};s/toprule/hline/;/mid\|bottom/d;/tabularnewline/a\\\hline]]
    local newlatex = pandoc.pipe('sed',{sed},oldlatex)
    return pandoc.RawBlock('latex', newlatex)
  end
  
  return {{Table = table},{RawBlock = rawblock}}
  </code></pre>