public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* section ids disappear with lua filter
@ 2022-01-15 13:26 denis.maier-NSENcxR/0n0
       [not found] ` <fbcc804201f74110818df9ab10d70635-NSENcxR/0n0@public.gmane.org>
  0 siblings, 1 reply; 3+ messages in thread
From: denis.maier-NSENcxR/0n0 @ 2022-01-15 13:26 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

[-- Attachment #1: Type: text/plain, Size: 2626 bytes --]

Hi,

I use this lua filter to move references after a special heading to the document metadata :

```lua
-- 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

function flatten_divs(div)
    if div.classes[1] == 'section' then
        return div.content
    end
end

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

The filter works, but it has the side effect that it also removes all section ids.

Input:

```md
---
lang: en
---

# Introduction

Bla Bla

Bla Bla


# Bibliography {#literatur}

Test Test

Test Test
```

pandoc input.md

```
<h1 id="introduction">Introduction</h1>
<p>Bla Bla</p>
<p>Bla Bla</p>
<h1 id="literatur">Bibliography</h1>
<p>Test Test</p>
<p>Test Test</p>```

pandoc input.md -L heading-refs-to-meta.lua

```
<h1>Introduction</h1>
<p>Bla Bla</p>
<p>Bla Bla</p>
```

Any ideas how I can preserve the ids?

Best,
Denis

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

[-- Attachment #2: Type: text/html, Size: 13137 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: section ids disappear with lua filter
       [not found] ` <fbcc804201f74110818df9ab10d70635-NSENcxR/0n0@public.gmane.org>
@ 2022-01-15 13:33   ` Albert Krewinkel
       [not found]     ` <87fsppdsrf.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
  0 siblings, 1 reply; 3+ messages in thread
From: Albert Krewinkel @ 2022-01-15 13:33 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

<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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* AW: section ids disappear with lua filter
       [not found]     ` <87fsppdsrf.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
@ 2022-01-15 14:12       ` denis.maier-NSENcxR/0n0
  0 siblings, 0 replies; 3+ messages in thread
From: denis.maier-NSENcxR/0n0 @ 2022-01-15 14:12 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

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.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-01-15 14:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-15 13:26 section ids disappear with lua filter 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       ` AW: " denis.maier-NSENcxR/0n0

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