Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔄 synced file(s) with rostools/r3-theme #249

Merged
merged 3 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ _freeze
/.quarto/
.Rbuildignore
slides/*.html
_ignore
6 changes: 5 additions & 1 deletion _extensions/rostools/r3-theme/_extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ version: 1.0.6
quarto-required: ">=1.5.0"
contributes:
format:
common:
shortcodes:
- debruine/glossary
- quarto-ext/fontawesome
html:
highlight-style: a11y
fig-dpi: 72
Expand Down Expand Up @@ -55,6 +59,6 @@ contributes:
freeze: auto

knitr:
opts_chunk:
opts_chunk:
R.options:
width: 72
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
title: Glossary
author: Lisa DeBruine
version: 1.0.0
quarto-required: ">=1.2.0"
contributes:
shortcodes:
- glossary.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
.glossary {
color: purple;
text-decoration: underline;
cursor: help;
position: relative;
border: none;
padding: 0;
}

/* only needed for popup = "click" */
/* popup-definition */
.glossary .def {
display: none;
position: absolute;
z-index: 1;
width: 200px;
bottom: 100%;
left: 50%;
margin-left: -100px;
background-color: #333;
color: white;
padding: 5px;
border-radius: 6px;
}
/* show on click */
.glossary:active .def {
display: inline-block;
}
/* triangle arrow */
.glossary:active .def::after {
content: ' ';
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #333 transparent transparent transparent;
}

/* glossary table styles */
.glossary_table td {
vertical-align: top;
}

.glossary_table td:first-child {
padding-right: 1em;
}

.glossary_table tr {
border-bottom: 1px solid #ddd;
}

.glossary_table tr:nth-child(even) {
background-color: #99999933;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
-- Glossary.lua
-- Author: Lisa DeBruine

-- Global glossary table
globalGlossaryTable = {}

-- Helper Functions

local function addHTMLDeps()
-- add the HTML requirements for the library used
quarto.doc.add_html_dependency({
name = 'glossary',
stylesheets = {'glossary.css'}
})
end

local function kwExists(kwargs, keyword)
for key, value in pairs(kwargs) do
if key == keyword then
return true
end
end
return false
end

-- Function to sort a Lua table by keys
function sortByKeys(tbl)
local sortedKeys = {}

-- Extract keys from the table and store them in the 'sortedKeys' array
for key, _ in pairs(tbl) do
table.insert(sortedKeys, key)
end

-- Sort the keys alphabetically
table.sort(sortedKeys)

-- Create a new table with the sorted keys
local sortedTable = {}
for _, key in pairs(sortedKeys) do
sortedTable[key] = tbl[key]
end

return sortedTable
end

local function read_metadata_file(fname)
local metafile = io.open(fname, 'r')
local content = metafile:read("*a")
metafile:close()
local metadata = pandoc.read(content, "markdown").meta
return metadata
end

local function readGlossary(path)
local f = io.open(path, "r")
if not f then
io.stderr:write("Cannot open file " .. path)
else
local lines = f:read("*all")
f:close()
return(lines)
end
end

---Merge user provided options with defaults
---@param userOptions table
local function mergeOptions(userOptions, meta)
local defaultOptions = {
path = "glossary.yml",
popup = "hover",
show = true,
add_to_table = true
}

-- override with meta values first
if meta.glossary ~= nil then
for k, v in pairs(meta.glossary) do
local value = pandoc.utils.stringify(v)
if value == 'true' then value = true end
if value == 'false' then value = false end
defaultOptions[k] = value
end
end

-- then override with function keyword values
if userOptions ~= nil then
for k, v in pairs(userOptions) do
local value = pandoc.utils.stringify(v)
if value == 'true' then value = true end
if value == 'false' then value = false end
defaultOptions[k] = value
end
end

return defaultOptions
end


-- Main Glossary Function Shortcode

return {

["glossary"] = function(args, kwargs, meta)

-- this will only run for HTML documents
if not quarto.doc.isFormat("html:js") then
return pandoc.Null()
end

addHTMLDeps()

-- create glossary table
if kwExists(kwargs, "table") then
local gt = "<table class='glossary_table'>\n"
gt = gt .. "<tr><th> Term </th><th> Definition </th></tr>\n"

local sortedTable = sortByKeys(globalGlossaryTable)

for key, value in pairs(sortedTable) do
gt = gt .. "<tr><td>" .. key
gt = gt .. "</td><td>" .. value .. "</td></tr>\n"
end
gt = gt .. "</table>"

return pandoc.RawBlock('html', gt)
end

-- or set up in-text term
local options = mergeOptions(kwargs, meta)

local display = pandoc.utils.stringify(args[1])
local term = string.lower(display)

if kwExists(kwargs, "display") then
display = pandoc.utils.stringify(kwargs.display)
end

-- get definition
local def = ""
if kwExists(kwargs, "def") then
def = pandoc.utils.stringify(kwargs.def)
else
local metafile = io.open(options.path, 'r')
local content = "---\n" .. metafile:read("*a") .. "\n---\n"
metafile:close()
local glossary = pandoc.read(content, "markdown").meta
for key, value in pairs(glossary) do
glossary[string.lower(key)] = value
end
-- quarto.log.output()
if kwExists(glossary, term) then
def = pandoc.utils.stringify(glossary[term])
end
end

-- add to global table
if options.add_to_table then
globalGlossaryTable[term] = def
end

if options.popup == "hover" then
glosstext = "<button class='glossary' title='" .. def .."'>" .. display .. "</button>"
elseif options.popup == "click" then
glosstext = "<button class='glossary'><span class='def'>" .. def .."</span>" .. display .. "</button>"
elseif options.popup == "none" then
glosstext = "<button class='glossary'>" .. display .. "</button>"
end

return pandoc.RawInline("html", glosstext)

end

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
title: Font Awesome support
author: Carlos Scheidegger
version: 1.2.0
quarto-required: ">=1.2.269"
contributes:
shortcodes:
- fontawesome.lua
Loading