public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* Lua filter for section numbers in internal references
@ 2021-12-22  9:16 be...-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org
       [not found] ` <186dfc25-8ebd-43ef-8a5b-0fae22106712n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 7+ messages in thread
From: be...-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org @ 2021-12-22  9:16 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 2019 bytes --]

Hi,

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. I found a recipe at stackoverflow 
(https://stackoverflow.com/questions/54128461/how-to-use-latex-section-numbers-in-pandoc-cross-reference) 
which does this if the link is given like this:

  See the [](#quick-introduction)

This is the Lua code:

local make_sections = (require 'pandoc.utils').make_sections
local section_numbers = {}

function populate_section_numbers (doc)
  function populate (elements)
    for _, el in pairs(elements) do
      if el.t == 'Div' and el.attributes.number then
        section_numbers['#' .. el.attr.identifier] = el.attributes.number
        populate(el.content)
      end
    end
  end
  populate(make_sections(true, nil, doc.blocks))        
end

function resolve_section_ref (link)
  if #link.content > 0 or link.target:sub(1, 1) ~= '#' then
    return nil
  end
  local section_number = pandoc.Str(section_numbers[link.target])
  return pandoc.Link({section_number}, link.target, link.title, link.attr)
end

return {
  {Pandoc = populate_section_numbers},
  {Link = resolve_section_ref}
}



While I do understand what it does, I cannot find out how to change it to 
my case. How can I retrieve the part written in [] instead of the 
attributes when scanning the document for those internal links? Or is the 
not even possible? 

Any idea is appreciated.


Torsten

-- 
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/186dfc25-8ebd-43ef-8a5b-0fae22106712n%40googlegroups.com.

[-- Attachment #1.2: Type: text/html, Size: 2916 bytes --]

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

* Re: Lua filter for section numbers in internal references
       [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
  2021-12-22 14:53   ` Albert Krewinkel
  1 sibling, 2 replies; 7+ messages in thread
From: Bastien DUMONT @ 2021-12-22 11:12 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

It seems that you can use pandoc-crossref for such case: see the manual here (https://github.com/lierdakil/pandoc-crossref/blob/master/docs/index.md).

To answer the technical part of the question, `pandoc -t native <<< 'See the [Quick introduction].'` shows that "[Quick introduction]" is parsed as a normal string. You can create a span instead by adding a class: [Quick introduction]{.sectionref}. But even then, it will be simplier to parse its content if it is limited to one string (like "Quick-introduction").

Le Wednesday 22 December 2021 à 01:16:01AM, be...-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org a écrit :
> Hi,
> 
> 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. I found a recipe at stackoverflow (https://stackoverflow.com/questions/
> 54128461/how-to-use-latex-section-numbers-in-pandoc-cross-reference) which does
> this if the link is given like this:
> 
>   See the [](#quick-introduction)
> 
> This is the Lua code:
> 
> local make_sections = (require 'pandoc.utils').make_sections
> local section_numbers = {}
> 
> function populate_section_numbers (doc)
>   function populate (elements)
>     for _, el in pairs(elements) do
>       if el.t == 'Div' and el.attributes.number then
>         section_numbers['#' .. el.attr.identifier] = el.attributes.number
>         populate(el.content)
>       end
>     end
>   end
>   populate(make_sections(true, nil, doc.blocks))        
> end
> 
> function resolve_section_ref (link)
>   if #link.content > 0 or link.target:sub(1, 1) ~= '#' then
>     return nil
>   end
>   local section_number = pandoc.Str(section_numbers[link.target])
>   return pandoc.Link({section_number}, link.target, link.title, link.attr)
> end
> 
> return {
>   {Pandoc = populate_section_numbers},
>   {Link = resolve_section_ref}
> }
> 
> 
> 
> While I do understand what it does, I cannot find out how to change it to my
> case. How can I retrieve the part written in [] instead of the attributes when
> scanning the document for those internal links? Or is the not even possible? 
> 
> Any idea is appreciated.
> 
> 
> Torsten
> 
> --
> 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 [1]pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To view this discussion on the web visit [2]https://groups.google.com/d/msgid/
> pandoc-discuss/186dfc25-8ebd-43ef-8a5b-0fae22106712n%40googlegroups.com.
> 
> References:
> 
> [1] mailto:pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
> [2] https://groups.google.com/d/msgid/pandoc-discuss/186dfc25-8ebd-43ef-8a5b-0fae22106712n%40googlegroups.com?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/YcMIB/cIBo3JwTa5%40localhost.


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

* Re: Lua filter for section numbers in internal references
  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
  1 sibling, 0 replies; 7+ messages in thread
From: be...-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org @ 2021-12-22 12:07 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 4120 bytes --]

Thanks for your ideas. I had considered pandoc-crossref also but didn't 
want to introduce a new dependency without a real need. If it can be 
accomplished using a simple and small Lua filter, I would prefer this.

I also observed that the native output creates a normal string. However, 
using Pandoc's make_section function, the references are delivered as DIVs 
with a number attribute. This is easy 

On Wednesday, December 22, 2021 at 12:13:38 PM UTC+1 Bastien Dumont wrote:

> It seems that you can use pandoc-crossref for such case: see the manual 
> here (
> https://github.com/lierdakil/pandoc-crossref/blob/master/docs/index.md).
>
> To answer the technical part of the question, `pandoc -t native <<< 'See 
> the [Quick introduction].'` shows that "[Quick introduction]" is parsed as 
> a normal string. You can create a span instead by adding a class: [Quick 
> introduction]{.sectionref}. But even then, it will be simplier to parse its 
> content if it is limited to one string (like "Quick-introduction").
>
> Le Wednesday 22 December 2021 à 01:16:01AM, be...-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org a 
> écrit :
> > Hi,
> > 
> > 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. I found a recipe at stackoverflow (
> https://stackoverflow.com/questions/
> > 54128461/how-to-use-latex-section-numbers-in-pandoc-cross-reference) 
> which does
> > this if the link is given like this:
> > 
> > See the [](#quick-introduction)
> > 
> > This is the Lua code:
> > 
> > local make_sections = (require 'pandoc.utils').make_sections
> > local section_numbers = {}
> > 
> > function populate_section_numbers (doc)
> > function populate (elements)
> > for _, el in pairs(elements) do
> > if el.t == 'Div' and el.attributes.number then
> > section_numbers['#' .. el.attr.identifier] = el.attributes.number
> > populate(el.content)
> > end
> > end
> > end
> > populate(make_sections(true, nil, doc.blocks)) 
> > end
> > 
> > function resolve_section_ref (link)
> > if #link.content > 0 or link.target:sub(1, 1) ~= '#' then
> > return nil
> > end
> > local section_number = pandoc.Str(section_numbers[link.target])
> > return pandoc.Link({section_number}, link.target, link.title, link.attr)
> > end
> > 
> > return {
> > {Pandoc = populate_section_numbers},
> > {Link = resolve_section_ref}
> > }
> > 
> > 
> > 
> > While I do understand what it does, I cannot find out how to change it 
> to my
> > case. How can I retrieve the part written in [] instead of the 
> attributes when
> > scanning the document for those internal links? Or is the not even 
> possible? 
> > 
> > Any idea is appreciated.
> > 
> > 
> > Torsten
> > 
> > --
> > 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 [1]pandoc-discus...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> > To view this discussion on the web visit [2]
> https://groups.google.com/d/msgid/
> > pandoc-discuss/186dfc25-8ebd-43ef-8a5b-0fae22106712n%40googlegroups.com.
> > 
> > References:
> > 
> > [1] mailto:pandoc-discus...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
> > [2] 
> https://groups.google.com/d/msgid/pandoc-discuss/186dfc25-8ebd-43ef-8a5b-0fae22106712n%40googlegroups.com?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/503bf9a0-73f8-49f4-a9a6-abbf01a24650n%40googlegroups.com.

[-- Attachment #1.2: Type: text/html, Size: 6767 bytes --]

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

* Re: Lua filter for section numbers in internal references
  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>
  1 sibling, 1 reply; 7+ messages in thread
From: be...-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org @ 2021-12-22 12:12 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 4287 bytes --]

Thanks for your ideas. I had considered pandoc-crossref also but didn't 
want to introduce a new dependency without a real need. If it can be 
accomplished using a simple and small Lua filter, I would prefer this.

I also observed that the native output creates a normal string. However, 
using Pandoc's make_section function, the references are delivered as DIVs 
with a number attribute. This is easy to process. I just need access to the 
text string inside the square brackets. So, how to access the text within a 
DIV? I think this is the thing I need to know ...

On Wednesday, December 22, 2021 at 12:13:38 PM UTC+1 Bastien Dumont wrote:

> It seems that you can use pandoc-crossref for such case: see the manual 
> here (
> https://github.com/lierdakil/pandoc-crossref/blob/master/docs/index.md).
>
> To answer the technical part of the question, `pandoc -t native <<< 'See 
> the [Quick introduction].'` shows that "[Quick introduction]" is parsed as 
> a normal string. You can create a span instead by adding a class: [Quick 
> introduction]{.sectionref}. But even then, it will be simplier to parse its 
> content if it is limited to one string (like "Quick-introduction").
>
> Le Wednesday 22 December 2021 à 01:16:01AM, be...-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org a 
> écrit :
> > Hi,
> > 
> > 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. I found a recipe at stackoverflow (
> https://stackoverflow.com/questions/
> > 54128461/how-to-use-latex-section-numbers-in-pandoc-cross-reference) 
> which does
> > this if the link is given like this:
> > 
> > See the [](#quick-introduction)
> > 
> > This is the Lua code:
> > 
> > local make_sections = (require 'pandoc.utils').make_sections
> > local section_numbers = {}
> > 
> > function populate_section_numbers (doc)
> > function populate (elements)
> > for _, el in pairs(elements) do
> > if el.t == 'Div' and el.attributes.number then
> > section_numbers['#' .. el.attr.identifier] = el.attributes.number
> > populate(el.content)
> > end
> > end
> > end
> > populate(make_sections(true, nil, doc.blocks)) 
> > end
> > 
> > function resolve_section_ref (link)
> > if #link.content > 0 or link.target:sub(1, 1) ~= '#' then
> > return nil
> > end
> > local section_number = pandoc.Str(section_numbers[link.target])
> > return pandoc.Link({section_number}, link.target, link.title, link.attr)
> > end
> > 
> > return {
> > {Pandoc = populate_section_numbers},
> > {Link = resolve_section_ref}
> > }
> > 
> > 
> > 
> > While I do understand what it does, I cannot find out how to change it 
> to my
> > case. How can I retrieve the part written in [] instead of the 
> attributes when
> > scanning the document for those internal links? Or is the not even 
> possible? 
> > 
> > Any idea is appreciated.
> > 
> > 
> > Torsten
> > 
> > --
> > 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 [1]pandoc-discus...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> > To view this discussion on the web visit [2]
> https://groups.google.com/d/msgid/
> > pandoc-discuss/186dfc25-8ebd-43ef-8a5b-0fae22106712n%40googlegroups.com.
> > 
> > References:
> > 
> > [1] mailto:pandoc-discus...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
> > [2] 
> https://groups.google.com/d/msgid/pandoc-discuss/186dfc25-8ebd-43ef-8a5b-0fae22106712n%40googlegroups.com?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/c8df6f45-5119-4d7c-b95c-ce1ecfc57a64n%40googlegroups.com.

[-- Attachment #1.2: Type: text/html, Size: 6949 bytes --]

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

* Re: Lua filter for section numbers in internal references
       [not found]       ` <c8df6f45-5119-4d7c-b95c-ce1ecfc57a64n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2021-12-22 13:46         ` Bastien DUMONT
  0 siblings, 0 replies; 7+ messages in thread
From: Bastien DUMONT @ 2021-12-22 13:46 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

Have you looked at this? https://pandoc.org/lua-filters.html#type-div

Le Wednesday 22 December 2021 à 04:12:29AM, be...-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org a écrit :
> Thanks for your ideas. I had considered pandoc-crossref also but didn't want to
> introduce a new dependency without a real need. If it can be accomplished using
> a simple and small Lua filter, I would prefer this.
> 
> I also observed that the native output creates a normal string. However, using
> Pandoc's make_section function, the references are delivered as DIVs with a
> number attribute. This is easy to process. I just need access to the text
> string inside the square brackets. So, how to access the text within a DIV? I
> think this is the thing I need to know ...
> 
> On Wednesday, December 22, 2021 at 12:13:38 PM UTC+1 Bastien Dumont wrote:
> 
>     It seems that you can use pandoc-crossref for such case: see the manual
>     here ([1]https://github.com/lierdakil/pandoc-crossref/blob/master/docs/
>     index.md).
> 
>     To answer the technical part of the question, `pandoc -t native <<< 'See
>     the [Quick introduction].'` shows that "[Quick introduction]" is parsed as
>     a normal string. You can create a span instead by adding a class: [Quick
>     introduction]{.sectionref}. But even then, it will be simplier to parse its
>     content if it is limited to one string (like "Quick-introduction").
> 
>     Le Wednesday 22 December 2021 à 01:16:01AM, be...@typoscriptics.de a écrit
>     :
>     > Hi,
>     >
>     > 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. I found a recipe at stackoverflow ([2]https://stackoverflow.com/
>     questions/
>     > 54128461/how-to-use-latex-section-numbers-in-pandoc-cross-reference)
>     which does
>     > this if the link is given like this:
>     >
>     > See the [](#quick-introduction)
>     >
>     > This is the Lua code:
>     >
>     > local make_sections = (require 'pandoc.utils').make_sections
>     > local section_numbers = {}
>     >
>     > function populate_section_numbers (doc)
>     > function populate (elements)
>     > for _, el in pairs(elements) do
>     > if el.t == 'Div' and el.attributes.number then
>     > section_numbers['#' .. el.attr.identifier] = el.attributes.number
>     > populate(el.content)
>     > end
>     > end
>     > end
>     > populate(make_sections(true, nil, doc.blocks))
>     > end
>     >
>     > function resolve_section_ref (link)
>     > if #link.content > 0 or link.target:sub(1, 1) ~= '#' then
>     > return nil
>     > end
>     > local section_number = pandoc.Str(section_numbers[link.target])
>     > return pandoc.Link({section_number}, link.target, link.title, link.attr)
>     > end
>     >
>     > return {
>     > {Pandoc = populate_section_numbers},
>     > {Link = resolve_section_ref}
>     > }
>     >
>     >
>     >
>     > While I do understand what it does, I cannot find out how to change it to
>     my
>     > case. How can I retrieve the part written in [] instead of the attributes
>     when
>     > scanning the document for those internal links? Or is the not even
>     possible?
>     >
>     > Any idea is appreciated.
>     >
>     >
>     > Torsten
>     >
>     > --
>     > 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 [1]pandoc-discus...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
>     > To view this discussion on the web visit [2][3]https://groups.google.com/
>     d/msgid/
>     > pandoc-discuss/186dfc25-8ebd-43ef-8a5b-0fae22106712n%[4]
>     40googlegroups.com.
>     >
>     > References:
>     >
>     > [1] mailto:pandoc-discus...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
>     > [2] [5]https://groups.google.com/d/msgid/pandoc-discuss/
>     186dfc25-8ebd-43ef-8a5b-0fae22106712n%40googlegroups.com?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 [6]pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To view this discussion on the web visit [7]https://groups.google.com/d/msgid/
> pandoc-discuss/c8df6f45-5119-4d7c-b95c-ce1ecfc57a64n%40googlegroups.com.
> 
> References:
> 
> [1] https://github.com/lierdakil/pandoc-crossref/blob/master/docs/index.md
> [2] https://stackoverflow.com/questions/
> [3] https://groups.google.com/d/msgid/
> [4] http://40googlegroups.com/
> [5] https://groups.google.com/d/msgid/pandoc-discuss/186dfc25-8ebd-43ef-8a5b-0fae22106712n%40googlegroups.com?utm_medium=email&utm_source=footer
> [6] mailto:pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
> [7] https://groups.google.com/d/msgid/pandoc-discuss/c8df6f45-5119-4d7c-b95c-ce1ecfc57a64n%40googlegroups.com?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/YcMsUFst47Y3iiza%40localhost.


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

* Re: Lua filter for section numbers in internal references
       [not found] ` <186dfc25-8ebd-43ef-8a5b-0fae22106712n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  2021-12-22 11:12   ` Bastien DUMONT
@ 2021-12-22 14:53   ` Albert Krewinkel
       [not found]     ` <87sfukn12g.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
  1 sibling, 1 reply; 7+ messages in thread
From: Albert Krewinkel @ 2021-12-22 14:53 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

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


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

* Re: Lua filter for section numbers in internal references
       [not found]     ` <87sfukn12g.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
@ 2021-12-23  6:59       ` be...-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org
  0 siblings, 0 replies; 7+ messages in thread
From: be...-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org @ 2021-12-23  6:59 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 2359 bytes --]

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

[-- Attachment #1.2: Type: text/html, Size: 3327 bytes --]

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

end of thread, other threads:[~2021-12-23  6:59 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-22  9:16 Lua filter for section numbers in internal references 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
     [not found]     ` <87sfukn12g.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
2021-12-23  6:59       ` be...-GbY3e145aRm8w6eVIr4Tmg@public.gmane.org

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