Pando c: отдельное оглавление для каждого раздела - PullRequest
0 голосов
/ 31 января 2020

Я конвертирую Markdown в HTML с помощью Pando c. При --toc Pando c генерирует оглавление и вставляет его под первым заголовком H1 (из которых только один).

Я бы хотел, чтобы у него была отдельная дополнительная оглавление для каждого подзаголовка. В частности, я хотел бы, чтобы под каждым H3 было небольшое локальное оглавление.

Может ли Pando c сделать это, и если да, то как?

1 Ответ

0 голосов
/ 01 февраля 2020

Я получил ответ на список рассылки pando c -discuss в виде Lua фильтра.

Цитата от автора решения:

  • Предупреждение. Предполагается, что все главы имеют заголовок уровня 2 - измените переменные chapter_level и toc_level для соответствия!
  • Предупреждение. Предполагается, что каждый раздел / глава имеет уникальный идентификатор!
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 },
}
...