Skip to content

Commit 49cd864

Browse files
danmarclaude
andcommitted
manual: add files missing from the previous commit
Lua filters, make-appendix.sh and the metadata files for the open source and premium manuals. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a56645a commit 49cd864

6 files changed

Lines changed: 352 additions & 0 deletions

File tree

‎man/filters/include-files.LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Pandoc Extensions
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎man/filters/include-files.lua‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
--- include-files.lua – filter to include Markdown files
2+
---
3+
--- Copyright: © 2019–2021 Albert Krewinkel
4+
--- License: MIT – see LICENSE file for details
5+
6+
-- Module pandoc.path is required and was added in version 2.12
7+
PANDOC_VERSION:must_be_at_least '2.12'
8+
9+
local pandoc = require 'pandoc'
10+
local List = require 'pandoc.List'
11+
local path = require 'pandoc.path'
12+
local system = require 'pandoc.system'
13+
14+
--- Get include auto mode
15+
local include_auto = false
16+
local function get_vars (meta)
17+
if meta['include-auto'] then
18+
include_auto = true
19+
end
20+
end
21+
22+
--- Keep last heading level found
23+
local last_heading_level = 0
24+
local function update_last_level(header)
25+
last_heading_level = header.level
26+
end
27+
28+
--- Update contents of included file
29+
local function update_contents(blocks, shift_by, include_path)
30+
local update_contents_filter = {
31+
-- Shift headings in block list by given number
32+
Header = function (header)
33+
if shift_by then
34+
header.level = header.level + shift_by
35+
end
36+
return header
37+
end,
38+
-- If link paths are relative then prepend include file path
39+
Link = function (link)
40+
if path.is_relative(link.target) and string.sub(path.filename(link.target), 1, 1) ~= '#' and not string.find(link.target, "https?://") then
41+
link.target = path.normalize(path.join({include_path, link.target}))
42+
end
43+
return link
44+
end,
45+
-- If image paths are relative then prepend include file path
46+
Image = function (image)
47+
if path.is_relative(image.src) then
48+
image.src = path.normalize(path.join({include_path, image.src}))
49+
end
50+
return image
51+
end,
52+
-- Update path for include-code-files.lua filter style CodeBlocks
53+
CodeBlock = function (cb)
54+
if cb.attributes.include and path.is_relative(cb.attributes.include) then
55+
cb.attributes.include =
56+
path.normalize(path.join({include_path, cb.attributes.include}))
57+
end
58+
return cb
59+
end
60+
}
61+
62+
return pandoc.walk_block(pandoc.Div(blocks), update_contents_filter).content
63+
end
64+
65+
--- Filter function for code blocks
66+
local transclude
67+
function transclude (cb)
68+
-- ignore code blocks which are not of class "include".
69+
if not cb.classes:includes 'include' then
70+
return
71+
end
72+
73+
-- Markdown is used if this is nil.
74+
local format = cb.attributes['format']
75+
76+
-- Attributes shift headings
77+
local shift_heading_level_by = 0
78+
local shift_input = cb.attributes['shift-heading-level-by']
79+
if shift_input then
80+
shift_heading_level_by = tonumber(shift_input)
81+
else
82+
if include_auto then
83+
-- Auto shift headings
84+
shift_heading_level_by = last_heading_level
85+
end
86+
end
87+
88+
--- keep track of level before recusion
89+
local buffer_last_heading_level = last_heading_level
90+
91+
local blocks = List:new()
92+
for line in cb.text:gmatch('[^\n]+') do
93+
if line:sub(1,2) ~= '//' then
94+
local fh = io.open(line)
95+
if not fh then
96+
io.stderr:write("Cannot open file " .. line .. " | Skipping includes\n")
97+
else
98+
-- read file as the given format with global reader options
99+
local contents = pandoc.read(
100+
fh:read '*a',
101+
format,
102+
PANDOC_READER_OPTIONS
103+
).blocks
104+
last_heading_level = 0
105+
-- recursive transclusion
106+
contents = system.with_working_directory(
107+
path.directory(line),
108+
function ()
109+
return pandoc.walk_block(
110+
pandoc.Div(contents),
111+
{ Header = update_last_level, CodeBlock = transclude }
112+
)
113+
end).content
114+
--- reset to level before recursion
115+
last_heading_level = buffer_last_heading_level
116+
blocks:extend(update_contents(contents, shift_heading_level_by,
117+
path.directory(line)))
118+
fh:close()
119+
end
120+
end
121+
end
122+
return blocks
123+
end
124+
125+
return {
126+
{ Meta = get_vars },
127+
{ Header = update_last_level, CodeBlock = transclude }
128+
}

‎man/filters/manual.lua‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+
}

‎man/make-appendix.sh‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
# Generate a markdown file with an appendix chapter that includes the given
3+
# markdown files. The chapter heading gets "Appendix A" etc, see
4+
# filters/manual.lua. The result is meant to be passed to pandoc after the manual,
5+
# together with filters/include-files.lua and filters/manual.lua.
6+
#
7+
# Usage:
8+
# ./make-appendix.sh TITLE FILE... > appendix.md
9+
#
10+
# Example:
11+
# ./make-appendix.sh "Checkers" checkers/*.md > appendix-checkers.md
12+
# pandoc manual.md appendix-checkers.md --metadata-file=manual-premium.yaml \
13+
# --lua-filter=filters/include-files.lua --lua-filter=filters/manual.lua \
14+
# -o manual-premium.pdf
15+
16+
if [ $# -lt 2 ]; then
17+
echo "Usage: $0 TITLE FILE..." >&2
18+
exit 1
19+
fi
20+
21+
title=$1
22+
shift
23+
24+
printf '# %s {.appendix}\n\n' "$title"
25+
# Sort case insensitive, independent of locale
26+
printf '%s\n' "$@" | LC_ALL=C sort -f | while read -r f; do
27+
# The identifier must match file_id() in filters/manual.lua
28+
id=file-$(printf '%s' "$f" | sed 's/[^A-Za-z0-9]/-/g')
29+
printf '::: {#%s .included-file}\n\n```{.include shift-heading-level-by=1}\n%s\n```\n\n:::\n\n' "$id" "$f"
30+
done

‎man/manual-opensource.yaml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Metadata for the open source manual: pandoc --metadata-file=manual-opensource.yaml
2+
profile: opensource
3+
title: Cppcheck manual
4+
subtitle: Version 2.23 dev
5+
author: Cppcheck team

‎man/manual-premium.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Metadata for the Cppcheck Premium manual: pandoc --metadata-file=manual-premium.yaml
2+
profile: premium
3+
title: Cppcheck Premium manual
4+
author: Cppcheck team, Cppcheck Solutions AB

0 commit comments

Comments
 (0)