forked from hyginn/CSB195
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_quarto.lua
More file actions
84 lines (68 loc) · 2.23 KB
/
_quarto.lua
File metadata and controls
84 lines (68 loc) · 2.23 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
-- _quarto.lua
-- Place functions that should execute every time a quarto document
-- is rendered in this project into this file. If you want to include a whole
-- directory of functions, you can require() it by placing code here, like:
-- local util = require('./src/lua/utils')
-- copyBox function
-- Transform fenced code blocks {.copyBox label="…"} into a styled container
-- with a copy button that copies only the <pre> payload.
-- Usage in .qmd:
-- ```{.copyBox label="<your label>t"}
-- Your literal text...
-- ```
-- -------- Helpers (top-level, local) --------
local function is_html()
-- Prefer Quarto's helper when available; fallback to Pandoc's FORMAT
if quarto and quarto.doc then
return quarto.doc.is_format("html")
end
return FORMAT and FORMAT:match("html")
end
local function html_escape(s)
-- Escape raw characters for safe inclusion inside <pre>…</pre>
return (s:gsub("&", "&")
:gsub("<", "<")
:gsub(">", ">"))
end
local function has_class(el, cls)
local classes = el.classes or (el.attr and el.attr.classes) or {}
-- Pandoc's List often has :includes; otherwise, fall back to a manual scan
if classes.includes and classes:includes(cls) then return true end
for _, c in ipairs(classes) do
if c == cls then return true end
end
return false
end
local function get_attr(el, key)
local attrs = el.attributes or (el.attr and el.attr.attributes) or {}
return attrs[key]
end
-- -------- Core transformer --------
local function handle_copybox(el)
if not is_html() then return nil end
if not has_class(el, "copyBox") then return nil end
local label = get_attr(el, "label") or "Copy"
local payload = el.text or "" -- CodeBlock.text is a STRING
local button = [[
<button class="copyButton" type="button" aria-label="Copy" onclick="copyContent(this)">
<i class="bi bi-clipboard"></i>
</button>
]]
local html = string.format([[
<div class="copyBox" label="%s">
%s
<pre>%s</pre>
</div>]],
pandoc.utils.stringify(label),
button,
html_escape(payload)
)
return pandoc.RawBlock("html", html)
end
-- -------- Expose handlers --------
return {
CodeBlock = function(el)
return handle_copybox(el) -- return nil for non-matching blocks
end
}
-- [END]