Skip to content

Commit d4ac41e

Browse files
committed
feat: manage acronyms
1 parent 10a978d commit d4ac41e

166 files changed

Lines changed: 4525 additions & 19426 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
title: Glossary
2+
author: Lisa DeBruine
3+
version: 1.0.0
4+
quarto-required: ">=1.2.0"
5+
contributes:
6+
shortcodes:
7+
- glossary.lua
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
.glossary {
2+
color: purple;
3+
text-decoration: underline;
4+
cursor: help;
5+
position: relative;
6+
border: none;
7+
padding: 0;
8+
}
9+
10+
/* only needed for popup = "click" */
11+
/* popup-definition */
12+
.glossary .def {
13+
display: none;
14+
position: absolute;
15+
z-index: 1;
16+
width: 200px;
17+
bottom: 100%;
18+
left: 50%;
19+
margin-left: -100px;
20+
background-color: #333;
21+
color: white;
22+
padding: 5px;
23+
border-radius: 6px;
24+
}
25+
/* show on click */
26+
.glossary:active .def {
27+
display: inline-block;
28+
}
29+
/* triangle arrow */
30+
.glossary:active .def::after {
31+
content: ' ';
32+
position: absolute;
33+
top: 100%;
34+
left: 50%;
35+
margin-left: -5px;
36+
border-width: 5px;
37+
border-style: solid;
38+
border-color: #333 transparent transparent transparent;
39+
}
40+
41+
/* glossary table styles */
42+
.glossary_table td {
43+
vertical-align: top;
44+
}
45+
46+
.glossary_table td:first-child {
47+
padding-right: 1em;
48+
}
49+
50+
.glossary_table tr {
51+
border-bottom: 1px solid #ddd;
52+
}
53+
54+
.glossary_table tr:nth-child(even) {
55+
background-color: #99999933;
56+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
-- Glossary.lua
2+
-- Author: Lisa DeBruine
3+
4+
-- Global glossary table
5+
globalGlossaryTable = {}
6+
7+
-- Helper Functions
8+
9+
local function addHTMLDeps()
10+
-- add the HTML requirements for the library used
11+
quarto.doc.add_html_dependency({
12+
name = 'glossary',
13+
stylesheets = {'glossary.css'}
14+
})
15+
end
16+
17+
local function kwExists(kwargs, keyword)
18+
for key, value in pairs(kwargs) do
19+
if key == keyword then
20+
return true
21+
end
22+
end
23+
return false
24+
end
25+
26+
-- Function to sort a Lua table by keys
27+
function sortByKeys(tbl)
28+
local sortedKeys = {}
29+
30+
-- Extract keys from the table and store them in the 'sortedKeys' array
31+
for key, _ in pairs(tbl) do
32+
table.insert(sortedKeys, key)
33+
end
34+
35+
-- Sort the keys alphabetically
36+
table.sort(sortedKeys)
37+
38+
-- Create a new table with the sorted keys
39+
local sortedTable = {}
40+
for _, key in pairs(sortedKeys) do
41+
sortedTable[key] = tbl[key]
42+
end
43+
44+
return sortedTable
45+
end
46+
47+
local function read_metadata_file(fname)
48+
local metafile = io.open(fname, 'r')
49+
local content = metafile:read("*a")
50+
metafile:close()
51+
local metadata = pandoc.read(content, "markdown").meta
52+
return metadata
53+
end
54+
55+
local function readGlossary(path)
56+
local f = io.open(path, "r")
57+
if not f then
58+
io.stderr:write("Cannot open file " .. path)
59+
else
60+
local lines = f:read("*all")
61+
f:close()
62+
return(lines)
63+
end
64+
end
65+
66+
---Merge user provided options with defaults
67+
---@param userOptions table
68+
local function mergeOptions(userOptions, meta)
69+
local defaultOptions = {
70+
path = "glossary.yml",
71+
popup = "hover",
72+
show = true,
73+
add_to_table = true
74+
}
75+
76+
-- override with meta values first
77+
if meta.glossary ~= nil then
78+
for k, v in pairs(meta.glossary) do
79+
local value = pandoc.utils.stringify(v)
80+
if value == 'true' then value = true end
81+
if value == 'false' then value = false end
82+
defaultOptions[k] = value
83+
end
84+
end
85+
86+
-- then override with function keyword values
87+
if userOptions ~= nil then
88+
for k, v in pairs(userOptions) do
89+
local value = pandoc.utils.stringify(v)
90+
if value == 'true' then value = true end
91+
if value == 'false' then value = false end
92+
defaultOptions[k] = value
93+
end
94+
end
95+
96+
return defaultOptions
97+
end
98+
99+
100+
-- Main Glossary Function Shortcode
101+
102+
return {
103+
104+
["glossary"] = function(args, kwargs, meta)
105+
106+
-- this will only run for HTML documents
107+
if not quarto.doc.isFormat("html:js") then
108+
return pandoc.Null()
109+
end
110+
111+
addHTMLDeps()
112+
113+
-- create glossary table
114+
if kwExists(kwargs, "table") then
115+
local gt = "<table class='glossary_table'>\n"
116+
gt = gt .. "<tr><th> Term </th><th> Definition </th></tr>\n"
117+
118+
local sortedTable = sortByKeys(globalGlossaryTable)
119+
120+
for key, value in pairs(sortedTable) do
121+
gt = gt .. "<tr><td>" .. key
122+
gt = gt .. "</td><td>" .. value .. "</td></tr>\n"
123+
end
124+
gt = gt .. "</table>"
125+
126+
return pandoc.RawBlock('html', gt)
127+
end
128+
129+
-- or set up in-text term
130+
local options = mergeOptions(kwargs, meta)
131+
132+
local display = pandoc.utils.stringify(args[1])
133+
local term = string.lower(display)
134+
135+
if kwExists(kwargs, "display") then
136+
display = pandoc.utils.stringify(kwargs.display)
137+
end
138+
139+
-- get definition
140+
local def = ""
141+
if kwExists(kwargs, "def") then
142+
def = pandoc.utils.stringify(kwargs.def)
143+
else
144+
local metafile = io.open(options.path, 'r')
145+
local content = "---\n" .. metafile:read("*a") .. "\n---\n"
146+
metafile:close()
147+
local glossary = pandoc.read(content, "markdown").meta
148+
for key, value in pairs(glossary) do
149+
glossary[string.lower(key)] = value
150+
end
151+
-- quarto.log.output()
152+
if kwExists(glossary, term) then
153+
def = pandoc.utils.stringify(glossary[term])
154+
end
155+
end
156+
157+
-- add to global table
158+
if options.add_to_table then
159+
globalGlossaryTable[term] = def
160+
end
161+
162+
if options.popup == "hover" then
163+
glosstext = "<button class='glossary' title='" .. def .."'>" .. display .. "</button>"
164+
elseif options.popup == "click" then
165+
glosstext = "<button class='glossary'><span class='def'>" .. def .."</span>" .. display .. "</button>"
166+
elseif options.popup == "none" then
167+
glosstext = "<button class='glossary'>" .. display .. "</button>"
168+
end
169+
170+
return pandoc.RawInline("html", glosstext)
171+
172+
end
173+
174+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
title: acronyms
2+
author: rchaput
3+
version: 2.0.1
4+
quarto-required: ">=1.4.0"
5+
contributes:
6+
filters:
7+
- parse-acronyms.lua
8+
shortcodes:
9+
- shortcodes_acronyms.lua

0 commit comments

Comments
 (0)