|
2 | 2 | -- Copyright (C) 2021-2022 Posit Software, PBC |
3 | 3 |
|
4 | 4 | function _callout_main() |
5 | | - local calloutidx = 1 |
6 | | - |
7 | 5 | local function calloutType(div) |
8 | 6 | for _, class in ipairs(div.attr.classes) do |
9 | 7 | if _quarto.modules.classpredicates.isCallout(class) then |
@@ -33,146 +31,6 @@ function _callout_main() |
33 | 31 | end |
34 | 32 | end |
35 | 33 |
|
36 | | - -- an HTML callout div |
37 | | - local function calloutDiv(node) |
38 | | - node = _quarto.modules.callouts.decorate_callout_title_with_crossref(node) |
39 | | - |
40 | | - -- the first heading is the title |
41 | | - local div = pandoc.Div({}) |
42 | | - local c = quarto.utils.as_blocks(node.content) |
43 | | - if pandoc.utils.type(c) == "Blocks" then |
44 | | - div.content:extend(c) |
45 | | - else |
46 | | - div.content:insert(c) |
47 | | - end |
48 | | - local title = quarto.utils.as_inlines(node.title) |
49 | | - local callout_type = node.type |
50 | | - local calloutAppearance = node.appearance |
51 | | - local icon = node.icon |
52 | | - local collapse = node.collapse |
53 | | - local found = false |
54 | | - |
55 | | - _quarto.ast.walk(title, { |
56 | | - RawInline = function(_) |
57 | | - found = true |
58 | | - end, |
59 | | - RawBlock = function(_) |
60 | | - found = true |
61 | | - end |
62 | | - }) |
63 | | - |
64 | | - if calloutAppearance == _quarto.modules.constants.kCalloutAppearanceDefault and pandoc.utils.stringify(title) == "" and not found then |
65 | | - title = quarto.utils.as_inlines(pandoc.Plain(_quarto.modules.callouts.displayName(node.type))) |
66 | | - end |
67 | | - |
68 | | - -- Make an outer card div and transfer classes and id |
69 | | - local calloutDiv = pandoc.Div({}) |
70 | | - calloutDiv.attr = node.attr:clone() |
71 | | - |
72 | | - local identifier = node.attr.identifier |
73 | | - if identifier ~= "" then |
74 | | - node.attr.identifier = "" |
75 | | - calloutDiv.attr.identifier = identifier |
76 | | - -- inject an anchor so callouts can be linked to |
77 | | - -- local attr = pandoc.Attr(identifier, {}, {}) |
78 | | - -- local anchor = pandoc.Link({}, "", "", attr) |
79 | | - -- title:insert(1, anchor) |
80 | | - end |
81 | | - |
82 | | - div.attr.classes = pandoc.List() |
83 | | - div.attr.classes:insert("callout-body-container") |
84 | | - |
85 | | - -- add card attribute |
86 | | - calloutDiv.attr.classes:insert("callout") |
87 | | - calloutDiv.attr.classes:insert("callout-style-" .. calloutAppearance) |
88 | | - if node.type ~= nil then |
89 | | - calloutDiv.attr.classes:insert("callout-" .. node.type) |
90 | | - end |
91 | | - |
92 | | - -- the image placeholder |
93 | | - local noicon = "" |
94 | | - |
95 | | - -- Check to see whether this is a recognized type |
96 | | - if icon == false or not _quarto.modules.callouts.isBuiltInType(callout_type) or type == nil then |
97 | | - noicon = " no-icon" |
98 | | - calloutDiv.attr.classes:insert("no-icon") |
99 | | - end |
100 | | - local imgPlaceholder = pandoc.Plain({pandoc.RawInline("html", "<i class='callout-icon" .. noicon .. "'></i>")}); |
101 | | - local imgDiv = pandoc.Div({imgPlaceholder}, pandoc.Attr("", {"callout-icon-container"})); |
102 | | - |
103 | | - -- show a titled callout |
104 | | - if title ~= nil and (pandoc.utils.type(title) == "string" or next(title) ~= nil) then |
105 | | - |
106 | | - -- mark the callout as being titleed |
107 | | - calloutDiv.attr.classes:insert("callout-titled") |
108 | | - |
109 | | - -- create a unique id for the callout |
110 | | - local calloutid = "callout-" .. calloutidx |
111 | | - calloutidx = calloutidx + 1 |
112 | | - |
113 | | - -- create the header to contain the title |
114 | | - -- title should expand to fill its space |
115 | | - local titleDiv = pandoc.Div(pandoc.Plain(title), pandoc.Attr("", {"callout-title-container", "flex-fill"})) |
116 | | - local headerDiv = pandoc.Div({imgDiv, titleDiv}, pandoc.Attr("", {"callout-header", "d-flex", "align-content-center"})) |
117 | | - local bodyDiv = div |
118 | | - bodyDiv.attr.classes:insert("callout-body") |
119 | | - |
120 | | - if collapse ~= nil then |
121 | | - |
122 | | - -- collapse default value |
123 | | - local expandedAttrVal = "true" |
124 | | - if collapse == "true" or collapse == true then |
125 | | - expandedAttrVal = "false" |
126 | | - end |
127 | | - |
128 | | - -- create the collapse button |
129 | | - local btnClasses = "callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end" |
130 | | - local btnIcon = "<i class='callout-toggle'></i>" |
131 | | - local toggleButton = pandoc.RawInline("html", "<div class='" .. btnClasses .. "'>" .. btnIcon .. "</div>") |
132 | | - headerDiv.content:insert(pandoc.Plain(toggleButton)); |
133 | | - |
134 | | - -- configure the header div for collapse |
135 | | - local bsTargetClz = calloutid .. "-contents" |
136 | | - headerDiv.attr.attributes["bs-toggle"] = "collapse" |
137 | | - headerDiv.attr.attributes["bs-target"] = "." .. bsTargetClz |
138 | | - headerDiv.attr.attributes["aria-controls"] = calloutid |
139 | | - headerDiv.attr.attributes["aria-expanded"] = expandedAttrVal |
140 | | - headerDiv.attr.attributes["aria-label"] = 'Toggle callout' |
141 | | - |
142 | | - -- configure the body div for collapse |
143 | | - local collapseDiv = pandoc.Div({}) |
144 | | - collapseDiv.attr.identifier = calloutid |
145 | | - collapseDiv.attr.classes:insert(bsTargetClz) |
146 | | - collapseDiv.attr.classes:insert("callout-collapse") |
147 | | - collapseDiv.attr.classes:insert("collapse") |
148 | | - if expandedAttrVal == "true" then |
149 | | - collapseDiv.attr.classes:insert("show") |
150 | | - end |
151 | | - |
152 | | - -- add the current body to the collapse div and use the collapse div instead |
153 | | - collapseDiv.content:insert(bodyDiv) |
154 | | - bodyDiv = collapseDiv |
155 | | - end |
156 | | - |
157 | | - -- add the header and body to the div |
158 | | - calloutDiv.content:insert(headerDiv) |
159 | | - calloutDiv.content:insert(bodyDiv) |
160 | | - else |
161 | | - -- show an untitleed callout |
162 | | - |
163 | | - -- create a card body |
164 | | - local containerDiv = pandoc.Div({imgDiv, div}, pandoc.Attr("", {"callout-body"})) |
165 | | - containerDiv.attr.classes:insert("d-flex") |
166 | | - |
167 | | - -- add the container to the callout card |
168 | | - calloutDiv.content:insert(containerDiv) |
169 | | - end |
170 | | - |
171 | | - return calloutDiv |
172 | | - end |
173 | | - |
174 | | - |
175 | | - |
176 | 34 | _quarto.ast.add_handler({ |
177 | 35 | -- use either string or array of strings |
178 | 36 | class_name = { "callout", "callout-note", "callout-warning", "callout-important", "callout-caution", "callout-tip" }, |
@@ -276,7 +134,7 @@ function _callout_main() |
276 | 134 |
|
277 | 135 | _quarto.ast.add_renderer("Callout", function(_) |
278 | 136 | return _quarto.format.isHtmlOutput() and hasBootstrap() |
279 | | - end, calloutDiv) |
| 137 | + end, _quarto.modules.callouts.render_to_bootstrap_div) |
280 | 138 |
|
281 | 139 | _quarto.ast.add_renderer("Callout", function(_) |
282 | 140 | return _quarto.format.isEpubOutput() or _quarto.format.isRevealJsOutput() |
|
0 commit comments