public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
From: Albert Krewinkel <albert+pandoc-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
Subject: Re: Pandoc filters to produces multiple document reading lines of a CSV
Date: Thu, 13 Oct 2022 14:54:30 +0200	[thread overview]
Message-ID: <87fsfr3l71.fsf@zeitkraut.de> (raw)
In-Reply-To: <9cf8d9c7-2789-4a28-b66e-d2c393880b3cn-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>

Hi Gianluca,

Gianluca Carbone <gianlucarbone96-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:

> I want to obtain multiple documents in output (markdown documents)
> which uses a specifc template already defined and working by me.
>
> The input file, must be a CSV file with an header for example:
> // CSV file \\
> name, surname, date, location
> Josh, White, 2022-10-12, Rome
> Kate, Brown, 2021-09-30, London
>
> I want in output in this case 2 documents:
>
>  1. One with content filled by the values of first record of CSV
>  2. One with content filled by the values of the second record of CSV
>
> How can I do something like this ?

Not sure I understand this correctly, but I assume you have a template
that contains things like

    ${name} ${surname}, born ${date} in ${location}

and you'd like to use that with a CSV table, using the header label as
variable name? If that's correct, then the following custom writer
should help:

``` lua
local stringify = pandoc.utils.stringify

function Writer (doc, opts)
  assert(doc.blocks[1] and doc.blocks[1].t == 'Table', 'expected table')
  -- table does not have colspans, footer, etc. Convert to simple table
  local tbl = pandoc.utils.to_simple_table(doc.blocks[1])
  local result = pandoc.List{}
  for _, row in ipairs(tbl.rows) do
    local meta = pandoc.Meta(doc.meta)   -- clone
    for i, cell in ipairs(tbl.header) do
      meta[stringify(cell)] = row[i]
    end
    local rowdoc = pandoc.Pandoc({}, meta)
    result:insert(pandoc.write(rowdoc, 'markdown', opts))
  end
  return table.concat(result, '\f')
end
```

Save the code to a file `meta-from-table.lua` and call pandoc with

    pandoc --to=meta-from-table.lua ...

where `...` are your other options, including the template. The output
is built from all rows, with a line separator character between each
record.

Hope that helps,

Albert

-- 
Albert Krewinkel
GPG: 8eed e3e2 e8c5 6f18 81fe  e836 388d c0b2 1f63 1124


  parent reply	other threads:[~2022-10-13 12:54 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-13 10:36 Gianluca Carbone
     [not found] ` <9cf8d9c7-2789-4a28-b66e-d2c393880b3cn-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2022-10-13 12:54   ` Albert Krewinkel [this message]
     [not found]     ` <87fsfr3l71.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
2022-10-27  8:44       ` Gianluca Carbone

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87fsfr3l71.fsf@zeitkraut.de \
    --to=albert+pandoc-9eawchwdxg8hfhg+jk9f0w@public.gmane.org \
    --cc=pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).