-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnaquiz.lua
168 lines (140 loc) · 4.77 KB
/
naquiz.lua
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
-- environment.lua
local function button_clear(div, defaultTitle, defaultClass, borderColor)
local beginDiv = ""
local buttonClass = div.attr.attributes['button-class']
-- quarto.log.output("=== button-clear ===")
-- quarto.log.output(div)
beginDiv = beginDiv .. "<button x-on:click='clear()'"
if buttonClass then
beginDiv = beginDiv .. 'class=' .. "'" .. buttonClass .. "'" .. '>'
else
beginDiv = beginDiv .. 'class=' .. "'" .. defaultClass .. "'" .. '>'
end
local title = div.attr.attributes['title']
if title then
beginDiv = beginDiv .. title
else
beginDiv = beginDiv .. defaultTitle
end
beginDiv = beginDiv .. "</button>\n"
if borderColor == nil then
borderColor = "card card-body"
else
borderColor = "card card-body " .. borderColor
end
local endDiv = "\n"
table.insert(div.content, pandoc.RawBlock('html', beginDiv))
table.insert(div.content, pandoc.RawBlock('html', endDiv))
-- quarto.log.output("=== \n\n\n\n")
-- quarto.log.output(div)
return div
end
local function button(div, defaultTitle, defaultClass, borderColor)
local beginDiv = "<div x-data='{ open: false }'>\n"
local buttonClass = div.attr.attributes['button-class']
beginDiv = beginDiv .. "<button x-on:click='open = ! open'"
if buttonClass then
beginDiv = beginDiv .. 'class=' .. "'" .. buttonClass .. "'" .. '>'
else
beginDiv = beginDiv .. 'class=' .. "'" .. defaultClass .. "'" .. '>'
end
local title = div.attr.attributes['title']
if title then
beginDiv = beginDiv .. title
else
beginDiv = beginDiv .. defaultTitle
end
beginDiv = beginDiv .. "</button>\n"
if borderColor == nil then
borderColor = "card card-body"
else
borderColor = "card card-body " .. borderColor
end
beginDiv = beginDiv ..
"<div x-show='open' x-transition.duration.200ms x-on:click.outside='open = false' class='" ..
borderColor .. "'" .. ">"
local endDiv = "</div>\n</div>\n"
table.insert(div.content, 1, pandoc.RawBlock('html', beginDiv))
table.insert(div.content, pandoc.RawBlock('html', endDiv))
-- quarto.log.output(div)
return div
end
local function choice(blocks, correct, idx)
local htmlStart = ''
if correct == true then
htmlStart = htmlStart ..
'<p><input class="form-check-input" type="radio" x-model="answer" value="correct"> <span id="correct" x-show="answer == $el.id" class="badge">✓</span>'
else
htmlStart = htmlStart ..
'<p><input class="form-check-input" type="radio" x-model="answer" value="no' ..
idx .. '"> <span id="no' .. idx .. '" x-show="answer == $el.id" class="badge">✗</span>'
end
table.insert(blocks[1].content, 1, pandoc.RawInline('html', htmlStart))
table.insert(blocks[1].content, pandoc.RawInline('html', '</p>'))
return blocks
end
local function choices(div)
local correct = false
if div.tag == "Div" then
if div.attr.classes:includes("choices") then
for idx, subDiv in ipairs(div.content) do
if subDiv.attr.classes:includes("choice") then
if subDiv.attr.classes:includes("correct-choice") then
correct = true
else
correct = false
end
choice(subDiv.content, correct, idx)
end
if subDiv.attr.classes:includes("button-clear") then
-- quarto.log.output("=== button-clear ===")
-- quarto.log.output(subDiv)
button_clear(subDiv, "Clear answer", "btn btn-light")
end
end
table.insert(div.content, 1,
pandoc.RawBlock('html',
'<div x-data="{ answer: \'\' , clear() { this.answer = \'\' }}">\n'))
table.insert(div.content, pandoc.RawBlock('html', '</div>\n'))
end
end
return div
end
local function question(div)
for idx, blocks in ipairs(div) do
if blocks.tag == "Div" then
choices(blocks)
end
end
return div
end
local function writeEnvironments(div)
if quarto.doc.is_format("html") then
quarto.doc.add_html_dependency({
name = "alpine",
version = "3.12",
scripts = {
{ path = "js/alpine@3.12.min.js", afterBody = "true" } },
stylesheets = { "css/buttons.css" }
})
if div.attr.classes:includes("question") then
-- quarto.log.output("=== question ===")
-- quarto.log.output(div)
div.content = question(div.content)
end
if div.attr.classes:includes("button-hint") then
div = button(div, "Hint", "btn btn-warning", "border-warning")
-- quarto.log.output("=== button-hint ===")
end
if div.attr.classes:includes("button-answer") then
div = button(div, "Answer", "btn btn-info", "border-info")
-- quarto.log.output("=== button-answer ===")
end
return (div)
end
end
-- Run in two passes so we process metadata
-- and then process the divs
return {
{ Div = writeEnvironments }
}