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 = 2local toc_level = 3local headings = {}local current_chapter = nillocal function collect_headings (head)if head.level == chapter_level thenlocal id = head.identifiercurrent_chapter = {chapter = id,toc = {},}headings[id] = current_chapterelseif head.level == toc_level thenif current_chapter thenlocal toc = current_chapter.toctoc[#toc+1] = headendendreturn nilendlocal function build_toc (heads)local toc = {}for _,head in ipairs(heads) dolocal entry = {pandoc.Plain{pandoc.Link(head.content:clone(), -- text'#' .. head.identifier, -- target"", -- empty titlepandoc.Attr("", -- empty identifier{'local-toc-link'} -- class))}}toc[#toc+1] = entryendreturn pandoc.Div({ pandoc.BulletList(toc) },pandoc.Attr( "", {'local-toc'} ))endlocal function insert_toc (head)if head.level == chapter_level thenlocal id = head.identifierif headings[id] thenlocal toc = build_toc(headings[id].toc)return {head,toc}endendreturn nilendreturn {{ 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 = 2local toc_level = 3local headings = {}local current_chapter = nillocal function collect_headings (head)if head.level == chapter_level thenlocal id = head.identifiercurrent_chapter = {chapter = idtoc = {}}headings[id] = current_chapterelseif head.level = toc_level thenif current_chapter thenlocal toc = current_chapter.toctoc[#toc+1] = headendendreturn nilendlocal function build_toc (heads)local toc = {}for _,head in ipairs(heads) dolocal entry = {pandoc.Plain{pandoc.Link(head.content:clone(), -- texthead.identifier, -- target"", -- empty titlepandoc.Attr("", -- empty identifier{'local-toc-link'} -- class))}}toc[#toc+1] = entryendreturn pandoc.Div({ pandoc.BulletList(toc) },pandoc.Attr( "", {'local-toc'} ))endlocal function insert_toc (head)if head.level = chapter_level thenlocal id = head.identifierif headings[id] thenlocal toc = build_toc(headings[id].toc)return {head,toc}endendreturn nilendreturn {{ 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#idm209455316208Szabolcs
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-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/3d838861-862c-4b15-8d91-a9b37d671736%40googlegroups.com.
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@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/2cf07390-db35-422e-887a-bfde62a12a63%40googlegroups.com.