From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.text.pandoc/24325 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: =?UTF-8?Q?Szabolcs_Horv=C3=A1t?= Newsgroups: gmane.text.pandoc Subject: Re: Separate tables of contents for each section Date: Fri, 31 Jan 2020 23:58:31 -0800 (PST) Message-ID: <2cf07390-db35-422e-887a-bfde62a12a63@googlegroups.com> References: <3d838861-862c-4b15-8d91-a9b37d671736@googlegroups.com> Reply-To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_2182_792040710.1580543911673" Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="19817"; mail-complaints-to="usenet@ciao.gmane.io" To: pandoc-discuss Original-X-From: pandoc-discuss+bncBCG5XQVJVUJBBKO72TYQKGQEELT7LZY-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Sat Feb 01 08:58:37 2020 Return-path: Envelope-to: gtp-pandoc-discuss@m.gmane-mx.org Original-Received: from mail-ot1-f61.google.com ([209.85.210.61]) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ixnfp-00050u-0R for gtp-pandoc-discuss@m.gmane-mx.org; Sat, 01 Feb 2020 08:58:37 +0100 Original-Received: by mail-ot1-f61.google.com with SMTP id p13sf4980334oto.12 for ; Fri, 31 Jan 2020 23:58:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20161025; 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=Dtgz0Tm6orh2exHNK0FZKF6fZ+4MCpY63juYZenejWs=; b=JI+nKz1/P7TyFaTiTRsM+W2AfFNununN4mU6timKbkeAGfOIrpNlPMqQbziqbsVdyZ IVCvFYvILPw0PDEai/ARW0a+s2hGuZKrw24AGpy3hgvlbOD1UqvEXy+nLxAQ9aBPYskx WX26CBWAxwzmbfhqaFjip5KffjQ4HyfYZJa+5ewcD/0smP4vpucasGjlNaQbvguJ9ciq ai1NoLdx754baCIbvR57dDJsTwxVDZv8H4G/CVyZNDij4loAzOPpZ/IlFpCCHJ9lt9MH a8lAhp6+dA2sVSXxdxuTdxgLPp5K+Ue0P4JhLmBwEdDB7UDM4sU1r8WOye4lFomIGskB cVbw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=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=Dtgz0Tm6orh2exHNK0FZKF6fZ+4MCpY63juYZenejWs=; b=iV0VgDyyNLforMLZ4q0eQ56ybk1ZW0atXz2NUD/sloq2Px9GJKjZDl4dfmO1YAyxA9 Nkm48CKNpguR60sG4sqeSlriv0HZA7qUFJSJ5dQGWKunMQ/ubXxovV1qxaiq0ny92nG7 V+hiurhJb694J/CycC1RSHp2zcPt6dVzH/qSqYV5InxEE/2sgs/6GMxdxphMP7jO03Kz gYxn/3Bg+G+Gobs18Nakn3GV2G1aLwf39tJZHD28kUBv3BqqhiFJIFUap8W6bannG4gc 0pd1z7IlhwdfnHC8wXOBD9WOkbAEHn8FlzBJnXNCjymCnV7iBRrIhExdmweo9Cr9WYaa PYgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; 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=Dtgz0Tm6orh2exHNK0FZKF6fZ+4MCpY63juYZenejWs=; b=qAv3xDoOnczHDADDfoK0TzLiJtSTlM8u8Go9Jv1RqaXA8NJ6rwFzATAhqzLAKySm5Z 2Dz39otvsgcBDXLemsRVR77nnqGvjW6IAgo1LeLZyIxqtGL6G+OrGGyogV7zO6Dd0Q75 h6f+LjaSXUWsFHnZKnRVx10LZ7GKCr2CrnMxP9DIClzfINepLWdeVDWn1WQ/9s43sAp1 XlFVv+23xe8RGaPE87cIlmHk8fL8tUPArKsnRzJ5QXzRimRHjMbzoYgVDsVLAhnhBRqW k/Ktr/9Ob7xvk4d6TBEjDkv+K/OZ3Gv+9PahFox4NR+CAjFbob81/6MuzHnvPqmCwKgT NYvA== Original-Sender: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org X-Gm-Message-State: APjAAAVKbe09P7u9Tt+NeAa0raR+jMinY3VyucnwbOIp4zoGdMPGZJAP I9sVvtCqGCR5LjMKbiGEMU0= X-Google-Smtp-Source: APXvYqz1x8BwF1v+xx9EjAeIenlN/2gBs7buGp73YQnhrdDgRjTHr3bsWDiCgXOUD1zb8h2ofzqOdQ== X-Received: by 2002:aca:318f:: with SMTP id x137mr9128626oix.100.1580543915962; Fri, 31 Jan 2020 23:58:35 -0800 (PST) X-BeenThere: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-Received: by 2002:aca:40e:: with SMTP id 14ls950439oie.4.gmail; Fri, 31 Jan 2020 23:58:33 -0800 (PST) X-Received: by 2002:aca:560b:: with SMTP id k11mr7223474oib.53.1580543912291; Fri, 31 Jan 2020 23:58:32 -0800 (PST) In-Reply-To: X-Original-Sender: szhorvat-Re5JQEeQqe8AvxtiuMwx3w@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:24325 Archived-At: ------=_Part_2182_792040710.1580543911673 Content-Type: multipart/alternative; boundary="----=_Part_2183_1992348913.1580543911674" ------=_Part_2183_1992348913.1580543911674 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Thank you, this worked perfectly after a few minor code fixes and after=20 prepending # to the identifier. For the convenience of those, who, like me, don't really know any Lua, I=20 attach the fixed code. local chapter_level =3D 2 local toc_level =3D 3 local headings =3D {} local current_chapter =3D nil local function collect_headings (head) if head.level =3D=3D chapter_level then local id =3D head.identifier current_chapter =3D { chapter =3D id, toc =3D {}, } headings[id] =3D current_chapter elseif head.level =3D=3D toc_level then if current_chapter then local toc =3D current_chapter.toc toc[#toc+1] =3D head end end return nil end local function build_toc (heads) local toc =3D {} for _,head in ipairs(heads) do local entry =3D { pandoc.Plain{ pandoc.Link( head.content:clone(), -- text '#' .. head.identifier, -- target "", -- empty title pandoc.Attr( "", -- empty identifier {'local-toc-link'} -- class ) ) } } toc[#toc+1] =3D entry end return pandoc.Div( { pandoc.BulletList(toc) }, pandoc.Attr( "", {'local-toc'} ) ) end local function insert_toc (head) if head.level =3D=3D chapter_level then local id =3D head.identifier if headings[id] then local toc =3D build_toc( headings[id].toc ) return {head,toc} end end return nil end return { { Header =3D collect_headings }, { Header =3D 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=20 > document in linear order. If this is the case in the latest version of=20 > Pandoc it should be doable with a two-pass filter: first a pass which=20 > collects the headings and then a pass which inserts the TsoC. If you want= =20 > hierarchical section numbering you probably also need to do a first pando= c=20 > run which inserts the numbering and then a second pandoc run with the=20 > 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 =E2=80=94 chan= ge the=20 > chapter_level and toc_level variables to match! > Warning 4: Assumes that each section/chapter has a unique identifier! > ) > > local chapter_level =3D 2 > local toc_level =3D 3 > local headings =3D {} > local current_chapter =3D nil > > local function collect_headings (head) > if head.level =3D=3D chapter_level then > local id =3D head.identifier > current_chapter =3D { > chapter =3D id > toc =3D {} > } > headings[id] =3D current_chapter > elseif head.level =3D toc_level then > if current_chapter then > local toc =3D current_chapter.toc > toc[#toc+1] =3D head > end > end > return nil > end > > local function build_toc (heads) > local toc =3D {} > for _,head in ipairs(heads) do > local entry =3D { > pandoc.Plain{ > pandoc.Link( > head.content:clone(), -- text > head.identifier, -- target > "", -- empty title > pandoc.Attr( > "", -- empty identifier > {'local-toc-link'} -- class > ) > ) > } > } > toc[#toc+1] =3D entry > end > return pandoc.Div( > { pandoc.BulletList(toc) }, > pandoc.Attr( "", {'local-toc'} ) > ) > end > > > local function insert_toc (head) > if head.level =3D chapter_level then > local id =3D head.identifier > if headings[id] then > local toc =3D build_toc( > headings[id].toc > ) > return {head,toc} > end > end > return nil > end > > return { > { Header =3D collect_headings }, > { Header =3D insert_toc }, > } > > > Den fre 31 jan. 2020 19:27Szabolcs Horv=C3=A1t >=20 > 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=20 >> level-two or level-three heading? >> >> I am looking to create something similar to the organization of this=20 >> page: https://igraph.org/c/doc/igraph-Visitors.html#idm209455316208 >> >> Szabolcs >> >> --=20 >> You received this message because you are subscribed to the Google Group= s=20 >> "pandoc-discuss" group. >> To unsubscribe from this group and stop receiving emails from it, send a= n=20 >> email to pandoc-...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org . >> To view this discussion on the web visit=20 >> https://groups.google.com/d/msgid/pandoc-discuss/3d838861-862c-4b15-8d91= -a9b37d671736%40googlegroups.com=20 >> >> . >> > --=20 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 e= mail 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. ------=_Part_2183_1992348913.1580543911674 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Thank you, this worked perfectly after a few minor code fi= xes and after prepending # to the identifier.

For the co= nvenience of those, who, like me, don't really know any Lua, I attach t= he fixed code.

local chapter_level =3D 2
local toc_level =3D 3
local headings =3D {}
local current_chapter =3D nil

local function collect_h= eadings (head)
if head.then
local id =3D head= .identifier
current_ch= apter =3D {
chapter =3D id,
toc =3D {},
}
headings[id] =3D current_chapter
elseif head.level =3D=3D toc_level then
if current_chapter= then
local toc =3D current_chapter.toc
toc[#toc+1] =3D head
end
end
return nil
end

<= div>local function build_toc (heads)
local toc =3D {}
for _,head in ipairs(heads) = do
local entry = =3D {
pandoc.Plain{<= /div>
pandoc.Link(
=
head.content:clone(), -- = text
'#'= .. head.identifier, -- target
"", -- e= mpty title
pandoc."= ", -- empty identifier
{'local-toc-link&#= 39;} -- class
= )
)
}
}
= toc[#toc+1] =3D entry
end
return pandoc.Div(<= /div>
{ pandoc.BulletList(to= c) },
pandoc.Attr( "", {'local-toc'} )
)
end


local function insert_toc (head)
if head.level =3D=3D chapter_level then
local id =3D head.identifier
if headings[id] then
local toc =3D build_t= oc(
headings[id].t= oc
)
return {head,toc}
end=
end
= return nil
end
return {
{ Header = =3D collect_headings },
{ Header =3D insert_toc },
}<= /div>

On Friday, 31 January 2020 21:31:31 UTC+1, BP wrot= e:
I believe = a change has been made so that Lua filters now traverse the document in lin= ear 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 an= d then a pass which inserts the TsoC. If you want hierarchical section numb= ering you probably also need to do a first pandoc run which inserts the num= bering and then a second pandoc run with the filter to insert the TsoC.=C2= =A0 Something like this:

(Warn= ing 1: Untested code!
=C2=A0 Warning 2: Make sure no= t to overwrite any existing file!
=C2=A0 Warning 3: = Assumes that all chapters are heading level 2 =E2=80=94 change the chapter_= level and toc_level variables to match!
=C2=A0 =C2= =A0Warning 4: Assumes that each section/chapter has a unique identifier!
)

<= div dir=3D"auto">local chapter_level =3D 2
local toc= _level =3D 3
local headings =3D {}
local current_chapter =3D nil

local function collect_headings (head)
= =C2=A0 if head.level =3D=3D chapter_level then
=C2= =A0 =C2=A0 local id =3D head.identifier
=C2=A0 =C2= =A0 current_chapter =3D {
=C2=A0 =C2=A0 =C2=A0 chapt= er =3D id
=C2=A0 =C2=A0 =C2=A0 toc =3D {}
=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 headings[= id] =3D current_chapter
=C2=A0 elseif head.level =3D= toc_level then
=C2=A0 =C2=A0 if current_chapter the= n
=C2=A0 =C2=A0 =C2=A0 local toc =3D current_chapter= .toc
=C2=A0 =C2=A0 =C2=A0 toc[#toc+1] =3D head
=
=C2=A0 =C2=A0 end
=C2=A0 end
=
=C2=A0 return nil
end

local function build_toc (heads)
=C2=A0 local toc =3D {}
=C2=A0 f= or _,head in ipairs(heads) do
=C2=A0 =C2=A0 local en= try =3D {
=C2=A0 =C2=A0 =C2=A0 pandoc.Plain{
=C2=A0 =C2=A0 =C2=A0 =C2=A0 pandoc.Link(
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 head.content:clone(), -- text
=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 head.identifier, -- ta= rget
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ""= , -- empty title
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = pandoc.Attr(
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 "", -- empty identifier
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 {'local-toc-link'} -- class
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 )
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 )
=C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 t= oc[#toc+1] =3D entry
=C2=A0 end
=C2=A0 return pandoc.Div(
=C2=A0 =C2=A0 { pandoc.= BulletList(toc) },
=C2=A0 =C2=A0 pandoc.Attr( "= ", {'local-toc'} )
=C2=A0 )
end


local function insert_toc (head)
=C2=A0 if head.level =3D chapter_level then
=C2=A0 = =C2=A0 local id =3D head.identifier
=C2=A0 =C2=A0 if= headings[id] then
=C2=A0 =C2=A0 =C2=A0 local toc = =3D build_toc(
=C2=A0 =C2=A0 =C2=A0 =C2=A0 headings[= id].toc
=C2=A0 =C2=A0 =C2=A0 )
=C2=A0 =C2=A0 =C2=A0 return {head,toc}
=C2=A0 =C2= =A0 end
=C2=A0 end
=C2=A0 ret= urn nil
end

return {
=C2=A0 { Header =3D collect_head= ings },
=C2=A0 { Header =3D insert_toc },
}


Den fre 31 jan. 2020 19:27Szabolcs Horv= =C3=A1t <szho...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> skrev:
Hello everyone,

I am converting a = Markdown file to HTML.

I can insert a table of con= tents 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 somet= hing similar to the organization of this page:=C2=A0https://igraph.org/c/doc/igraph-Visitors.html#idm20= 9455316208

Szabolcs

--
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 pandoc-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pan= doc-discuss/3d838861-862c-4b15-8d91-a9b37d671736%40googlegroups.c= om.

--
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/2cf07390-db35-422e-887a-bfde62a12a63%40googlegroups.co= m.
------=_Part_2183_1992348913.1580543911674-- ------=_Part_2182_792040710.1580543911673--