public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* HowTo access a list from the yaml metadata block with lua?
@ 2020-08-16 11:00 Hendrik T. Voelker
  0 siblings, 0 replies; only message in thread
From: Hendrik T. Voelker @ 2020-08-16 11:00 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

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.


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-08-16 11:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-16 11:00 HowTo access a list from the yaml metadata block with lua? Hendrik T. Voelker

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