|
| 1 | +--- manual.lua - pandoc filter for the Cppcheck manuals |
| 2 | +--- |
| 3 | +--- Run it after include-files.lua: |
| 4 | +--- |
| 5 | +--- pandoc --lua-filter=filters/include-files.lua --lua-filter=filters/manual.lua ... |
| 6 | +--- |
| 7 | +--- 1. Conditional content, using the Quarto syntax |
| 8 | +--- (https://quarto.org/docs/authoring/conditional.html) with profiles: |
| 9 | +--- |
| 10 | +--- ::: {.content-visible when-profile="premium"} |
| 11 | +--- Only in the premium manual |
| 12 | +--- ::: |
| 13 | +--- |
| 14 | +--- ::: {.content-hidden when-profile="premium"} |
| 15 | +--- Not in the premium manual |
| 16 | +--- ::: |
| 17 | +--- |
| 18 | +--- `unless-profile` is also supported. The profile is set by the metadata |
| 19 | +--- field `profile`, see manual-opensource.yaml and manual-premium.yaml. |
| 20 | +--- |
| 21 | +--- 2. Included files: a Div with class `.included-file` and an identifier made |
| 22 | +--- from the file path (see make-appendix.sh) wraps an included file. |
| 23 | +--- - Only the first heading of the file is numbered and listed in the TOC. |
| 24 | +--- - Heading identifiers are prefixed with the Div identifier. |
| 25 | +--- - Links to the file (e.g. "[x](checkers/uninitvar.md)") point to the Div. |
| 26 | +--- |
| 27 | +--- 3. Raw html `<br/>` is replaced by a line break, raw html is dropped in |
| 28 | +--- pdf output. |
| 29 | +--- |
| 30 | +--- 4. Appendices: level 1 headings with class `.appendix` are appendices. In |
| 31 | +--- LaTeX output `\appendix` is added before the first one, so the chapter |
| 32 | +--- heading says "Appendix A". In other formats the headings in the |
| 33 | +--- appendix are numbered "Appendix A", "A.1", "A.2", ... |
| 34 | + |
| 35 | +local profile = nil |
| 36 | + |
| 37 | +local function file_id(target) |
| 38 | + return 'file-' .. target:gsub('[^%w]', '-') |
| 39 | +end |
| 40 | + |
| 41 | +local function get_profile(meta) |
| 42 | + if meta.profile then |
| 43 | + profile = pandoc.utils.stringify(meta.profile) |
| 44 | + end |
| 45 | +end |
| 46 | + |
| 47 | +local function conditional(div) |
| 48 | + local visible |
| 49 | + if div.classes:includes('content-visible') then |
| 50 | + visible = true |
| 51 | + elseif div.classes:includes('content-hidden') then |
| 52 | + visible = false |
| 53 | + else |
| 54 | + return nil |
| 55 | + end |
| 56 | + for key, _ in pairs(div.attributes) do |
| 57 | + if key ~= 'when-profile' and key ~= 'unless-profile' then |
| 58 | + error('manual.lua: unsupported condition "' .. key .. '"') |
| 59 | + end |
| 60 | + end |
| 61 | + if not profile then |
| 62 | + error('manual.lua: conditional content found but no profile is set, use --metadata-file=manual-<profile>.yaml') |
| 63 | + end |
| 64 | + local when = div.attributes['when-profile'] |
| 65 | + local unless = div.attributes['unless-profile'] |
| 66 | + local match = (when == nil or when == profile) and (unless == nil or unless ~= profile) |
| 67 | + if match == visible then |
| 68 | + return div.content |
| 69 | + end |
| 70 | + return {} |
| 71 | +end |
| 72 | + |
| 73 | +local function included_file(div) |
| 74 | + if not div.classes:includes('included-file') then |
| 75 | + return nil |
| 76 | + end |
| 77 | + local first = true |
| 78 | + return pandoc.walk_block(div, { |
| 79 | + Header = function (h) |
| 80 | + -- Each included file is parsed separately, make the identifiers unique |
| 81 | + if h.identifier ~= '' then |
| 82 | + h.identifier = div.identifier .. '-' .. h.identifier |
| 83 | + end |
| 84 | + if first then |
| 85 | + first = false |
| 86 | + else |
| 87 | + h.classes:insert('unnumbered') |
| 88 | + h.classes:insert('unlisted') |
| 89 | + end |
| 90 | + return h |
| 91 | + end |
| 92 | + }) |
| 93 | +end |
| 94 | + |
| 95 | +-- Appendix numbering, used for formats other than LaTeX |
| 96 | +local appendix_count = 0 |
| 97 | +local appendix_letter = nil |
| 98 | +local section_numbers = {} |
| 99 | + |
| 100 | +local function appendix(h) |
| 101 | + if h.level == 1 then |
| 102 | + if not h.classes:includes('appendix') then |
| 103 | + appendix_letter = nil |
| 104 | + return nil |
| 105 | + end |
| 106 | + appendix_count = appendix_count + 1 |
| 107 | + if FORMAT:match('latex') then |
| 108 | + if appendix_count == 1 then |
| 109 | + return { pandoc.RawBlock('latex', '\\appendix'), h } |
| 110 | + end |
| 111 | + return nil |
| 112 | + end |
| 113 | + appendix_letter = string.char(string.byte('A') + appendix_count - 1) |
| 114 | + section_numbers = {} |
| 115 | + h.attributes['number'] = 'Appendix ' .. appendix_letter |
| 116 | + return h |
| 117 | + end |
| 118 | + if appendix_letter and not h.classes:includes('unnumbered') then |
| 119 | + section_numbers[h.level] = (section_numbers[h.level] or 0) + 1 |
| 120 | + for level = h.level + 1, 6 do |
| 121 | + section_numbers[level] = nil |
| 122 | + end |
| 123 | + local number = appendix_letter |
| 124 | + for level = 2, h.level do |
| 125 | + number = number .. '.' .. (section_numbers[level] or 0) |
| 126 | + end |
| 127 | + h.attributes['number'] = number |
| 128 | + return h |
| 129 | + end |
| 130 | +end |
| 131 | + |
| 132 | +local function document(doc) |
| 133 | + local file_ids = {} |
| 134 | + doc:walk({ |
| 135 | + Div = function (div) |
| 136 | + if div.classes:includes('included-file') then |
| 137 | + file_ids[div.identifier] = true |
| 138 | + end |
| 139 | + end |
| 140 | + }) |
| 141 | + |
| 142 | + return doc:walk({ |
| 143 | + Link = function (link) |
| 144 | + local id = file_id(link.target) |
| 145 | + if file_ids[id] then |
| 146 | + link.target = '#' .. id |
| 147 | + return link |
| 148 | + end |
| 149 | + end, |
| 150 | + RawInline = function (raw) |
| 151 | + if raw.format == 'html' and raw.text:match('^<br%s*/?>$') then |
| 152 | + return pandoc.LineBreak() |
| 153 | + end |
| 154 | + end, |
| 155 | + Header = appendix |
| 156 | + }) |
| 157 | +end |
| 158 | + |
| 159 | +return { |
| 160 | + { Meta = get_profile }, |
| 161 | + { Div = conditional }, |
| 162 | + { Div = included_file }, |
| 163 | + { Pandoc = document }, |
| 164 | +} |
0 commit comments