File: fix-anchors.lua

package info (click to toggle)
dte 1.10-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,152 kB
  • sloc: ansic: 28,421; sh: 94; awk: 56; makefile: 13; sed: 1
file content (37 lines) | stat: -rw-r--r-- 917 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
-- This Pandoc filter modifies headings, so that the link anchors
-- for dterc commands don't include trailing junk like flags and
-- parameters. For example "include--bq-file" becomes "include".

local function get_canonical_id(header)
    return header.content[1].content[1].text
end

local section

local function Header(header)
    local level = assert(header.level)
    local id = header.attr.identifier

    if level == 2 then
        section = assert(id)
        return header
    end

    if level == 3 and section == "special-variables" then
        header.attr.identifier = id:upper()
        return header
    end

    if level == 3 and id:find("%-") then
        local ok, new_id = pcall(get_canonical_id, header)
        if ok and #new_id < #id and new_id == id:sub(1, #new_id) then
            header.attr.identifier = new_id
        end
    end

    return header
end

return {
    {Header = Header}
}