public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
From: BPJ <bpj-J3H7GcXPSITLoDKTGw+V6w@public.gmane.org>
To: bpj <bpj-J3H7GcXPSITLoDKTGw+V6w@public.gmane.org>
Cc: pandoc-discuss <pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
Subject: Re: Separate tables of contents for each section
Date: Sat, 1 Feb 2020 12:42:06 +0100	[thread overview]
Message-ID: <CADAJKhBP2hzpg6wLOdN649H3pgNf3JOf=_Nd-v1cmAqiVPdpQQ@mail.gmail.com> (raw)
In-Reply-To: <CADAJKhCEvixP6A4yZfmpuvoJj+PaZ=2KRQNdXLwhEveTzV0m9g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>

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

Having done a diff I see there were some easy-to-make but embarrassing
omissions. Again sorry for posting untested code and thanks for posting the
fixed version!


Den lör 1 feb. 2020 12:17BPJ <bpj-J3H7GcXPSITLoDKTGw+V6w@public.gmane.org> skrev:

> Oops, sorry for forgetting to prepend the hash mark to the identifier — a
> stupid omission!
> I'm glad it worked after that fix.
>
>
> Den lör 1 feb. 2020 08:59Szabolcs Horvát <szhorvat-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> skrev:
>
>> Thank you, this worked perfectly after a few minor code fixes and after
>> prepending # to the identifier.
>>
>> For the convenience of those, who, like me, don't really know any Lua, I
>> attach the fixed code.
>>
>> local chapter_level = 2
>> local toc_level = 3
>> local headings = {}
>> local current_chapter = nil
>>
>> local function collect_headings (head)
>> if head.level == chapter_level then
>> local id = head.identifier
>> current_chapter = {
>> chapter = id,
>> toc = {},
>> }
>> headings[id] = current_chapter
>> elseif head.level == toc_level then
>> if current_chapter then
>> local toc = current_chapter.toc
>> toc[#toc+1] = head
>> end
>> end
>> return nil
>> end
>>
>> local function build_toc (heads)
>> local toc = {}
>> for _,head in ipairs(heads) do
>> local entry = {
>> pandoc.Plain{
>> pandoc.Link(
>> head.content:clone(), -- text
>> '#' .. head.identifier, -- target
>> "", -- empty title
>> pandoc.Attr(
>> "", -- empty identifier
>> {'local-toc-link'} -- class
>> )
>> )
>> }
>> }
>> toc[#toc+1] = entry
>> end
>> return pandoc.Div(
>> { pandoc.BulletList(toc) },
>> pandoc.Attr( "", {'local-toc'} )
>> )
>> end
>>
>>
>> local function insert_toc (head)
>> if head.level == chapter_level then
>> local id = head.identifier
>> if headings[id] then
>> local toc = build_toc(
>> headings[id].toc
>> )
>> return {head,toc}
>> end
>> end
>> return nil
>> end
>>
>> return {
>> { Header = collect_headings },
>> { Header = insert_toc },
>> }
>>
>>
>> On Friday, 31 January 2020 21:31:31 UTC+1, BP wrote:
>>>
>>> I believe a change has been made so that Lua filters now traverse the
>>> document in linear order. If this is the case in the latest version of
>>> Pandoc it should be doable with a two-pass filter: first a pass which
>>> collects the headings and then a pass which inserts the TsoC. If you want
>>> hierarchical section numbering you probably also need to do a first pandoc
>>> run which inserts the numbering and then a second pandoc run with the
>>> filter to insert the TsoC.  Something like this:
>>>
>>> (Warning 1: Untested code!
>>>   Warning 2: Make sure not to overwrite any existing file!
>>>   Warning 3: Assumes that all chapters are heading level 2 — change the
>>> chapter_level and toc_level variables to match!
>>>    Warning 4: Assumes that each section/chapter has a unique identifier!
>>> )
>>>
>>> local chapter_level = 2
>>> local toc_level = 3
>>> local headings = {}
>>> local current_chapter = nil
>>>
>>> local function collect_headings (head)
>>>   if head.level == chapter_level then
>>>     local id = head.identifier
>>>     current_chapter = {
>>>       chapter = id
>>>       toc = {}
>>>     }
>>>     headings[id] = current_chapter
>>>   elseif head.level = toc_level then
>>>     if current_chapter then
>>>       local toc = current_chapter.toc
>>>       toc[#toc+1] = head
>>>     end
>>>   end
>>>   return nil
>>> end
>>>
>>> local function build_toc (heads)
>>>   local toc = {}
>>>   for _,head in ipairs(heads) do
>>>     local entry = {
>>>       pandoc.Plain{
>>>         pandoc.Link(
>>>           head.content:clone(), -- text
>>>           head.identifier, -- target
>>>           "", -- empty title
>>>           pandoc.Attr(
>>>             "", -- empty identifier
>>>             {'local-toc-link'} -- class
>>>           )
>>>         )
>>>       }
>>>     }
>>>     toc[#toc+1] = entry
>>>   end
>>>   return pandoc.Div(
>>>     { pandoc.BulletList(toc) },
>>>     pandoc.Attr( "", {'local-toc'} )
>>>   )
>>> end
>>>
>>>
>>> local function insert_toc (head)
>>>   if head.level = chapter_level then
>>>     local id = head.identifier
>>>     if headings[id] then
>>>       local toc = build_toc(
>>>         headings[id].toc
>>>       )
>>>       return {head,toc}
>>>     end
>>>   end
>>>   return nil
>>> end
>>>
>>> return {
>>>   { Header = collect_headings },
>>>   { Header = insert_toc },
>>> }
>>>
>>>
>>> Den fre 31 jan. 2020 19:27Szabolcs Horvát <szho...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> skrev:
>>>
>>>> Hello everyone,
>>>>
>>>> I am converting a Markdown file to HTML.
>>>>
>>>> I can insert a table of contents at the beginning using the --toc
>>>> option.
>>>>
>>>> Is it possible to insert additional tables of contents under each
>>>> level-two or level-three heading?
>>>>
>>>> I am looking to create something similar to the organization of this
>>>> page: https://igraph.org/c/doc/igraph-Visitors.html#idm209455316208
>>>>
>>>> Szabolcs
>>>>
>>>> --
>>>> 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-...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/pandoc-discuss/3d838861-862c-4b15-8d91-a9b37d671736%40googlegroups.com
>>>> <https://groups.google.com/d/msgid/pandoc-discuss/3d838861-862c-4b15-8d91-a9b37d671736%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/2cf07390-db35-422e-887a-bfde62a12a63%40googlegroups.com
>> <https://groups.google.com/d/msgid/pandoc-discuss/2cf07390-db35-422e-887a-bfde62a12a63%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/CADAJKhBP2hzpg6wLOdN649H3pgNf3JOf%3D_Nd-v1cmAqiVPdpQQ%40mail.gmail.com.

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

      parent reply	other threads:[~2020-02-01 11:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-31 18:26 Szabolcs Horvát
     [not found] ` <3d838861-862c-4b15-8d91-a9b37d671736-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2020-01-31 20:31   ` BPJ
     [not found]     ` <CADAJKhBgy0rDop2EXuK_JP6YN6kTtwQ5UpU3HRwonZJh_Wapig-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2020-02-01  7:58       ` Szabolcs Horvát
     [not found]         ` <2cf07390-db35-422e-887a-bfde62a12a63-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2020-02-01 11:17           ` BPJ
     [not found]             ` <CADAJKhCEvixP6A4yZfmpuvoJj+PaZ=2KRQNdXLwhEveTzV0m9g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2020-02-01 11:42               ` BPJ [this message]

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='CADAJKhBP2hzpg6wLOdN649H3pgNf3JOf=_Nd-v1cmAqiVPdpQQ@mail.gmail.com' \
    --to=bpj-j3h7gcxpsitlodktgw+v6w@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).