* Filter to put subdivided bibliography in metadata
@ 2022-04-06 15:50 denis.maier-NSENcxR/0n0
[not found] ` <c48fb5ae64fd4c07b238c22659d60011-NSENcxR/0n0@public.gmane.org>
0 siblings, 1 reply; 2+ messages in thread
From: denis.maier-NSENcxR/0n0 @ 2022-04-06 15:50 UTC (permalink / raw)
To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw
[-- Attachment #1: Type: text/plain, Size: 3553 bytes --]
Hi,
I'm currently using the filter below to put references in a special section into the metadata in order to move them to a different location in the template. This works ok for simple cases, i.e. one bibliography under one heading, like so :
```md
# Literatur
A bibliographic reference
Another bibliographic reference
```
However, for a current project I'll need to support subdivided bibliographies. Something like this:
```md
# Bibliography
## Primary sources
A bibliographic reference
Another bibliographic reference
## Scholarly articles
A bibliographic reference
Another bibliographic reference
```
This here would probably be a good result:
```yaml
biblio:
- title: Primary sources
references:
- A bibliographic reference
- Another bibliographic reference
- title: Scholarly articles
references:
- A bibliographic reference
- Another bibliographic reference
```
I'm not sure how I should tackle that problem. Any hints?
Best,
Denis
```lua
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 functtions 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},
}
```
--
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/c48fb5ae64fd4c07b238c22659d60011%40unibe.ch.
[-- Attachment #2: Type: text/html, Size: 15323 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
* AW: Filter to put subdivided bibliography in metadata
[not found] ` <c48fb5ae64fd4c07b238c22659d60011-NSENcxR/0n0@public.gmane.org>
@ 2022-04-07 9:03 ` denis.maier-NSENcxR/0n0
0 siblings, 0 replies; 2+ messages in thread
From: denis.maier-NSENcxR/0n0 @ 2022-04-07 9:03 UTC (permalink / raw)
To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw
[-- Attachment #1: Type: text/plain, Size: 6185 bytes --]
Nevermind,
I think I’ve come up with a working solution for my use case. I’ve changed the input to this:
```md
Blabla
# References {sec-type="ref-list"}
## Sources {sec-type="ref-list"}
A reference
Another reference
## Articles {sec-type="ref-list"}
A reference
Another reference
```
My target is JATS XML. As I need to do some post-processing with xslt anyway, I can handle this there with a couple of xslt templates :
```xsl
<xsl:template match="//body/sec[@sec-type = 'ref-list']" mode="rename">
<ref-list>
<xsl:apply-templates/>
</ref-list>
</xsl:template>
<xsl:template match="//sec[@sec-type = 'ref-list']/sec[@sec-type = 'ref-list']">
<ref-list>
<xsl:apply-templates/>
</ref-list>
</xsl:template>
<xsl:template match="//sec[@sec-type = 'ref-list']/p">
<ref>
<mixed-citation>
<xsl:apply-templates/>
</mixed-citation>
</ref>
</xsl:template>
<xsl:template match="//body/sec[@sec-type = 'ref-list']"></xsl:template>
<xsl:template match="back">
<xsl:copy>
<xsl:apply-templates select="//body/sec[@sec-type = 'ref-list']" mode="rename"/>
<xsl:apply-templates select= "@*|node()"/>
</xsl:copy>
</xsl:template>
```
Best,
Denis
Von: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> Im Auftrag von denis.maier-NSENcxR/0n0@public.gmane.org
Gesendet: Mittwoch, 6. April 2022 17:51
An: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
Betreff: Filter to put subdivided bibliography in metadata
Hi,
I’m currently using the filter below to put references in a special section into the metadata in order to move them to a different location in the template. This works ok for simple cases, i.e. one bibliography under one heading, like so :
```md
# Literatur
A bibliographic reference
Another bibliographic reference
```
However, for a current project I’ll need to support subdivided bibliographies. Something like this:
```md
# Bibliography
## Primary sources
A bibliographic reference
Another bibliographic reference
## Scholarly articles
A bibliographic reference
Another bibliographic reference
```
This here would probably be a good result:
```yaml
biblio:
- title: Primary sources
references:
- A bibliographic reference
- Another bibliographic reference
- title: Scholarly articles
references:
- A bibliographic reference
- Another bibliographic reference
```
I’m not sure how I should tackle that problem. Any hints?
Best,
Denis
```lua
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 functtions 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},
}
```
--
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<mailto:pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/c48fb5ae64fd4c07b238c22659d60011%40unibe.ch<https://groups.google.com/d/msgid/pandoc-discuss/c48fb5ae64fd4c07b238c22659d60011%40unibe.ch?utm_medium=email&utm_source=footer>.
--
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/36ca1cb8aa6e4d47b06e36e20bbb6a2b%40unibe.ch.
[-- Attachment #2: Type: text/html, Size: 24109 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-04-07 9:03 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-06 15:50 Filter to put subdivided bibliography in metadata denis.maier-NSENcxR/0n0
[not found] ` <c48fb5ae64fd4c07b238c22659d60011-NSENcxR/0n0@public.gmane.org>
2022-04-07 9:03 ` 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).