From: Albert Krewinkel <albert+pandoc-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
Subject: Re: Lua filter for section numbers in internal references
Date: Wed, 22 Dec 2021 15:53:11 +0100 [thread overview]
Message-ID: <87sfukn12g.fsf@zeitkraut.de> (raw)
In-Reply-To: <186dfc25-8ebd-43ef-8a5b-0fae22106712n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
be...-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org <berg-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org> writes:
> I am trying to make a Lua filter that will replace the normal link text in
> internal links with section numbers. I want to use internal links in my
> markdown document like this:
>
> See the [Quick introduction].
>
> So, the links are given by the full title of the respective section they
> refer to.
Here's a quick rewrite of my old code that should do it.
If there are internal links that should not be modified, simply add a
title or a class to that link, and the filter will leave it alone.
``` lua
local sections = {}
function populate_section_numbers (doc)
function populate (blocks)
for _, div in pairs(blocks) do
if div.t == 'Div' and div.attributes.number then
local header = div.content[1]
if header and header.t == 'Header' then
sections['#' .. div.attr.identifier] = {
number = div.attributes.number,
}
populate(div.content)
end
end
end
end
populate(pandoc.utils.make_sections(true, nil, doc.blocks))
end
function resolve_section_ref (link)
-- don't edit non-internal links or those with a title or a class.
if link.target:sub(1, 1) ~= '#' or #link.classes > 0 or #link.title > 0 then
return nil
end
local section = sections[link.target]
if section then
link.content = {pandoc.Str(section.number .. '\u{A0}')} .. link.content
return link
end
end
return {
{Pandoc = populate_section_numbers},
{Link = resolve_section_ref}
}
```
--
Albert Krewinkel
GPG: 8eed e3e2 e8c5 6f18 81fe e836 388d c0b2 1f63 1124
next prev parent reply other threads:[~2021-12-22 14:53 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-22 9:16 be...-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org
[not found] ` <186dfc25-8ebd-43ef-8a5b-0fae22106712n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2021-12-22 11:12 ` Bastien DUMONT
2021-12-22 12:07 ` be...-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org
2021-12-22 12:12 ` be...-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org
[not found] ` <c8df6f45-5119-4d7c-b95c-ce1ecfc57a64n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2021-12-22 13:46 ` Bastien DUMONT
2021-12-22 14:53 ` Albert Krewinkel [this message]
[not found] ` <87sfukn12g.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
2021-12-23 6:59 ` be...-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org
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=87sfukn12g.fsf@zeitkraut.de \
--to=albert+pandoc-9eawchwdxg8hfhg+jk9f0w@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).