From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.text.pandoc/29836 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: "be...-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org" Newsgroups: gmane.text.pandoc Subject: Re: Lua filter for section numbers in internal references Date: Wed, 22 Dec 2021 22:59:40 -0800 (PST) Message-ID: References: <186dfc25-8ebd-43ef-8a5b-0fae22106712n@googlegroups.com> <87sfukn12g.fsf@zeitkraut.de> Reply-To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_7510_1801608579.1640242780504" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="13711"; mail-complaints-to="usenet@ciao.gmane.io" To: pandoc-discuss Original-X-From: pandoc-discuss+bncBDQ6N25ANMIN3PEQRYDBUBARW3C5W-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Thu Dec 23 07:59:44 2021 Return-path: Envelope-to: gtp-pandoc-discuss@m.gmane-mx.org Original-Received: from mail-ot1-f58.google.com ([209.85.210.58]) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1n0I4m-0003S2-5z for gtp-pandoc-discuss@m.gmane-mx.org; Thu, 23 Dec 2021 07:59:44 +0100 Original-Received: by mail-ot1-f58.google.com with SMTP id 44-20020a9d032f000000b00589a5dad19csf1924426otv.3 for ; Wed, 22 Dec 2021 22:59:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20210112; h=sender:date:from:to:message-id:in-reply-to:references:subject :mime-version:x-original-sender:reply-to:precedence:mailing-list :list-id:list-post:list-help:list-archive:list-subscribe :list-unsubscribe; bh=BCNeUCOkE6f1GRNZORJSybVAdR6svBmSmqTjRTZBtuM=; b=fln+7jvOXYxtM5LKS/jLzcdXNbEM9OfsyyJPHCSbfCLdTbezfKj9+a5Gweg4ci/R6o Rbln6gU74qm7DRec5uLnPu4tSY5qEoOUTC3UvLwzEpQ17hfe0qzmtiUTwEL+huwDO89A ZYZudMQxmPdc2QtMiBmqLTWpiLrUKI4DlvBlHhbGemOHDjI5fLXNpyNQTiLwRVH423xX vEiOeVgN7p9jqmZBq1f2FQL7H1VVxw6Cj5jsavQSVqAsrYqbDY16FBhrgFxXKcFM2UP1 JY7a40vyhWn7yuvIYugpXAZhArTIjL7OkDI76YGiXn7A9o4TguG6Ck/iGWFHgZuARGSu sMQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=sender:x-gm-message-state:date:from:to:message-id:in-reply-to :references:subject:mime-version:x-original-sender:reply-to :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post :list-help:list-archive:list-subscribe:list-unsubscribe; bh=BCNeUCOkE6f1GRNZORJSybVAdR6svBmSmqTjRTZBtuM=; b=3tupMWHRy96Rqj+ot7sp8qCmo2yKUibX77P0t7B/vPfEP8VAI9I+DC86o4lkRRUhWn /vT1AKzbz+MI5WtHMAoNtjuLtNaMjKFCVJbSMkyHNJ6bEfvpcYp35q4yOpQcy2lzpCbA 5xnTUWHHoIRaIzh3i1X1BJN+jgl4Jz40LgE7cwL3OCSvjJiwG24bOl/U7GAQ2LbR5ToE wQrAGY1SxnCBHVuQSuaiRgoU5AiCtcJ4OpyJoqKwW7QtCl/M2KpaN+6ARK8kXRjYDyGw y9yhNuyxLTvffdanN1g/h0WbJnT7WJmiEod0gOUHkAcrbdEWCyPKGj+UAYCGeOxEM2tn j6Nw== Original-Sender: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org X-Gm-Message-State: AOAM530tcecFP8LAwFLgFGN7yUaJeKYKsZizLS9gkWhWe+k/3IVwz8fY faT2oj84hV6ARQVbM7yNWFI= X-Google-Smtp-Source: ABdhPJxoKPrcUsLf3Z53b2f330AEv8Le/NIJf1JRkDGlmX6pHHBGeqT2g+0kPxqyCr02MfuNNA/YlQ== X-Received: by 2002:a05:6808:2004:: with SMTP id q4mr853927oiw.56.1640242783086; Wed, 22 Dec 2021 22:59:43 -0800 (PST) X-BeenThere: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-Received: by 2002:aca:ded5:: with SMTP id v204ls996602oig.10.gmail; Wed, 22 Dec 2021 22:59:41 -0800 (PST) X-Received: by 2002:aca:1115:: with SMTP id 21mr811790oir.137.1640242781172; Wed, 22 Dec 2021 22:59:41 -0800 (PST) In-Reply-To: <87sfukn12g.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org> X-Original-Sender: berg-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org Precedence: list Mailing-list: list pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org; contact pandoc-discuss+owners-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org List-ID: X-Google-Group-Id: 1007024079513 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , Xref: news.gmane.io gmane.text.pandoc:29836 Archived-At: ------=_Part_7510_1801608579.1640242780504 Content-Type: multipart/alternative; boundary="----=_Part_7511_1441502162.1640242780504" ------=_Part_7511_1441502162.1640242780504 Content-Type: text/plain; charset="UTF-8" Wow, thanks for that Albert! I wasn't aware that it was your code on stackoverflow. I yet need to understand what it does exactly but I see the key thing is obviously to look at div.content[1] in order to get the link title. On Wednesday, December 22, 2021 at 3:53:21 PM UTC+1 Albert Krewinkel wrote: > be...-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 > -- 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/d7ed1a43-0312-408d-a08a-94d1988837een%40googlegroups.com. ------=_Part_7511_1441502162.1640242780504 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Wow, thanks for that Albert! I wasn't aware that it was your code on stacko= verflow. I yet need to understand what it does exactly but I see the key th= ing is obviously to look at div.content[1] in order to get the link title.<= br>
On= Wednesday, December 22, 2021 at 3:53:21 PM UTC+1 Albert Krewinkel wrote:
be...-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org <be...-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 sectio= n 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 =3D {}

function populate_section_numbers (doc)
function populate (blocks)
for _, div in pairs(blocks) do
if div.t =3D=3D 'Div' and div.attributes.number then
local header =3D div.content[1]
if header and header.t =3D=3D 'Header' then
sections['#' .. div.attr.identifier] =3D {
number =3D 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) ~=3D '#' or #link.classes > 0 or = #link.title > 0 then
return nil
end
local section =3D sections[link.target]
if section then
link.content =3D {pandoc.Str(section.number .. '\u{A0}')} .= . link.content
return link
end
end

return {
{Pandoc =3D populate_section_numbers},
{Link =3D resolve_section_ref}
}
```


--
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 &= quot;pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an e= mail to pand= oc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To view this discussion on the web visit https://groups.google.com/d= /msgid/pandoc-discuss/d7ed1a43-0312-408d-a08a-94d1988837een%40googlegroups.= com.
------=_Part_7511_1441502162.1640242780504-- ------=_Part_7510_1801608579.1640242780504--