public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
From: <denis.maier-NSENcxR/0n0@public.gmane.org>
To: <pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
Subject: AW: section ids disappear with lua filter
Date: Sat, 15 Jan 2022 14:12:42 +0000	[thread overview]
Message-ID: <86b1124244074f9f800babffde11db08@unibe.ch> (raw)
In-Reply-To: <87fsppdsrf.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>

Thanks, that was it. I now have this filter:

```
-- local pprint = require('pprint')

references = {}

function getRefsFromDiv (div)
    if div.identifier == 'literatur' then
        pandoc.walk_block(div, {
            Para = function(el)
                table.insert(references, el.content)
            end})
        return {} -- remove from main body
    end
end

function addRefsToMeta (meta)
	if not next(references) then
	   return meta
	else
		meta.biblio = references
		if not meta.bibliotitle then
			if pandoc.utils.stringify(meta['lang']) == 'en'  then
				meta.bibliotitle = "References"
			else 
				meta.bibliotitle = "Literatur"
			end
		end
		-- pprint(references)
		return meta
	end
end

function put_in_divs(doc)
    local blocks = {}
    for i,el in pairs(doc.blocks) do
        table.insert(blocks, el)   
    end
    local newblocks = pandoc.utils.make_sections(nil, 1, blocks)
    return pandoc.Pandoc(newblocks, doc.meta)
end

-- Helper functions for flatten_divs
-- Returns true iff a div is a section div.
local function is_section_div (div)
  return div.t == 'Div'
    and div.classes[1] == 'section'
    and div.attributes.number
end

local function section_header (div)
  local header = div.content and div.content[1]
  local is_header = is_section_div(div)
    and header
    and header.t == 'Header'
  return is_header and header or nil
end

--- Remove remaining section divs
local function flatten_divs (div)
  local header = section_header(div)
  if not header then
    return nil
  else
    header.identifier = div.identifier
    header.attributes.number = nil
    div.content[1] = header
    return div.content
  end
end

return {
    { Pandoc = put_in_divs },
    { Div = getRefsFromDiv },
    { Meta = addRefsToMeta },
    { Div = flatten_divs},
  }
```

Best,
Denis

> -----Ursprüngliche Nachricht-----
> Von: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <pandoc-
> discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> Im Auftrag von Albert Krewinkel
> Gesendet: Samstag, 15. Januar 2022 14:33
> An: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
> Betreff: Re: section ids disappear with lua filter
> 
> <denis.maier-NSENcxR/0n0@public.gmane.org> writes:
> 
> > I use this lua filter to move references after a special heading to
> > the document metadata :
> >
> > [...]
> >
> > The filter works, but it has the side effect that it also removes all
> > section ids.
> 
> The cause is that pandoc moves the IDs from the headings to the section
> divs; they must be transferred back. See the link below for an example.
> https://github.com/pandoc/lua-filters/blob/master/section-refs/section-
> refs.lua#L93-L104
> 
> 
> --
> Albert Krewinkel
> GPG: 8eed e3e2 e8c5 6f18 81fe  e836 388d c0b2 1f63 1124
> 
> --
> 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/87fsppdsrf.fsf%40zeitkraut.de.

-- 
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/86b1124244074f9f800babffde11db08%40unibe.ch.


      parent reply	other threads:[~2022-01-15 14:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-15 13:26 denis.maier-NSENcxR/0n0
     [not found] ` <fbcc804201f74110818df9ab10d70635-NSENcxR/0n0@public.gmane.org>
2022-01-15 13:33   ` Albert Krewinkel
     [not found]     ` <87fsppdsrf.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
2022-01-15 14:12       ` denis.maier-NSENcxR/0n0 [this message]

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=86b1124244074f9f800babffde11db08@unibe.ch \
    --to=denis.maier-nsencxr/0n0@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).