public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
From: "Hendrik T. Voelker" <basicbaer-Ls1Wi4Wg87BM7kwft8N7nw@public.gmane.org>
To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
Subject: HowTo access a list from the yaml metadata block with lua?
Date: Sun, 16 Aug 2020 13:00:09 +0200	[thread overview]
Message-ID: <8550f903-252c-912e-a817-90c8fb05f675@basicbaer.de> (raw)

Hi All,

I have some problems accessing meta data in a lua filter.

Here an example document, reduced to the relevant elements:

---8<---[rezept.md]

---
kitchen_notes: [anstellgut,garkorb,ruebensirup]
...

::: {#kitchen-notes}
:::

--->8---

What I want to do, is reading the list of kitchen notes given in the
metadata block and include the named documents in the given DIV block.

So far I succeeded in reading, parsing and appending markdown files to the
DIV block, but I am unable to correctly read the list entries.

My idea was to have the list in the local variable notes as a result, so
that i can iterate over it when processing the DIV block. But simply
assigning m.kitchen_notes or m.kitchen_notes.meta_values to that list
variable, either directly or as list element, does not work. It seems that
there are lists inside lists inside list ...

Disclaimer: I don't really know lua, just fiddling around with it to get it
done the way I want it. But I have had to learn the hard way that my way of
thinking isn't always what the package or language developers had in mind.

Anyway, this here is what I have tried:

---8<--- [kitchen-notes-filter.lua]
--[[
--
-- Filter to include kitchen notes
--
--]]

-- pandoc's List type
local List = require 'pandoc.List'

-- List of kitchen-notes to add
local notes = nil

--[[
  Read meta data and extract kitchen-notes list
  if there is one
]]
function read_meta_data (m)
  if (m.kitchen_notes)
    then
      notes = List:new(m.kitchen_notes.meta_values)
      print ("DEBUG:read_meta_data: type is " .. type (notes))
      for i,v in pairs (notes) do
        -- print ("DEBUG:read_meta_data: v = '" .. v .. "'")
        print ("DEBUG:read_meta_data: type(v) = ".. type(v))
        for a,b in ipairs (v) do
          print ("DEBUG:read_meta_data: b = '" .. b .. "'")
        end
      end
  end

  return m
end

--[[
  Extends given DIV block with a heading
]]
function extend_div (d)

  local blocks = List:new( { pandoc.Header(2, 'Küchen Notizen') } )

  for _, note in pairs(notes) do
    print ("Adding note '" .. note .. "'")
    local fh = io.open (note .. '.kn.md')
    blocks:extend (pandoc.read(fh:read '*a').blocks)
    fh:close()
  end

  d.content = blocks

  return d

end

--[[
  Selects DIV with name of #kitchen-block
]]
function kitchen_notes_div (d)

  if (d.identifier == 'kitchen-notes')
    then
      if (notes)
        then
          d = extend_div (d)
        else
          d = nil
      end
  end

  return d

end

-- return callback pointers
return {
  { Meta = read_meta_data },
  { Div = kitchen_notes_div }
}
--->8---

Any useful hint would be much appreciated.

TIA

Hendrik

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/8550f903-252c-912e-a817-90c8fb05f675%40basicbaer.de.


                 reply	other threads:[~2020-08-16 11:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=8550f903-252c-912e-a817-90c8fb05f675@basicbaer.de \
    --to=basicbaer-ls1wi4wg87bm7kwft8n7nw@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).