Skip to content

Commit

Permalink
lua,callout - fix title when non-empty but no strings. refactor into …
Browse files Browse the repository at this point in the history
…modules to clean up globals
  • Loading branch information
cscheid committed May 17, 2024
1 parent fcfdc4c commit a26cc1c
Show file tree
Hide file tree
Showing 12 changed files with 977 additions and 873 deletions.
1,087 changes: 438 additions & 649 deletions src/resources/filters/customnodes/callout.lua

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/resources/filters/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ end

import("./mainstateinit.lua")

import("./modules/import_all.lua")

import("./ast/scopedwalk.lua")
import("./ast/customnodes.lua")
import("./ast/emulatedfilter.lua")
Expand Down
4 changes: 2 additions & 2 deletions src/resources/filters/modules/authors.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- meta.lua
-- Copyright (C) 2020-2022 Posit Software, PBC
-- authors.lua
-- Copyright (C) 2020-2024 Posit Software, PBC

-- read and replace the authors field
-- without reshaped data that has been
Expand Down
199 changes: 199 additions & 0 deletions src/resources/filters/modules/callouts.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
-- callouts.lua
-- Copyright (C) 2024 Posit Software, PBC

local constants = require("modules/constants")

local function callout_title_prefix(callout, withDelimiter)
local category = crossref.categories.by_ref_type[refType(callout.attr.identifier)]
if category == nil then
fail("unknown callout prefix '" .. refType(callout.attr.identifier) .. "'")
return
end

return titlePrefix(category.ref_type, category.name, callout.order, withDelimiter)
end

local function decorate_callout_title_with_crossref(callout)
callout = ensure_custom(callout)
if not param("enable-crossref", true) then
-- don't decorate captions with crossrefs information if crossrefs are disabled
return callout
end
-- nil should never happen here, but the Lua analyzer doesn't know it
if callout == nil then
-- luacov: disable
internal_error()
-- luacov: enable
return callout
end
if not is_valid_ref_type(refType(callout.attr.identifier)) then
return callout
end
if callout.title == nil then
callout.title = pandoc.Plain({})
end
local title = callout.title.content

-- unlabeled callouts do not get a title prefix
local is_uncaptioned = not ((title ~= nil) and (#title > 0))
-- this is a hack but we need it to control styling downstream
callout.is_uncaptioned = is_uncaptioned
local title_prefix = callout_title_prefix(callout, not is_uncaptioned)
tprepend(title, title_prefix)

return callout
end

local function resolveCalloutContents(node, require_title)
local title = quarto.utils.as_inlines(node.title)
local type = node.type

local contents = pandoc.List({})

-- Add the titles and contents
-- class_name
if pandoc.utils.stringify(title) == "" and require_title then
---@diagnostic disable-next-line: need-check-nil
title = stringToInlines(type:sub(1,1):upper()..type:sub(2))
end

-- raw paragraph with styles (left border, colored)
if title ~= nil then
contents:insert(pandoc.Para(pandoc.Strong(title)))
end
tappend(contents, quarto.utils.as_blocks(node.content))

return contents
end

local kDefaultDpi = 96
local function docxCalloutImage(type)

-- If the DPI has been changed, we need to scale the callout icon
local dpi = pandoc.WriterOptions(PANDOC_WRITER_OPTIONS)['dpi']
local scaleFactor = 1
if dpi ~= nil then
scaleFactor = dpi / kDefaultDpi
end

-- try to form the svg name
local svg = nil
if type ~= nil then
svg = param("icon-" .. type, nil)
end

-- lookup the image
if svg ~= nil then
local img = pandoc.Image({}, svg, '', {[constants.kProjectResolverIgnore]="true"})
img.attr.attributes["width"] = tostring(16 * scaleFactor)
img.attr.attributes["height"] = tostring(16 * scaleFactor)
return img
else
return nil
end
end

local callout_attrs = {
note = {
color = constants.kColorNote,
background_color = constants.kBackgroundColorNote,
latex_color = "quarto-callout-note-color",
latex_frame_color = "quarto-callout-note-color-frame",
fa_icon = "faInfo",
fa_icon_typst = "fa-info"
},
warning = {
color = constants.kColorWarning,
background_color = constants.kBackgroundColorWarning,
latex_color = "quarto-callout-warning-color",
latex_frame_color = "quarto-callout-warning-color-frame",
fa_icon = "faExclamationTriangle",
fa_icon_typst = "fa-exclamation-triangle"
},
important = {
color = constants.kColorImportant,
background_color = constants.kBackgroundColorImportant,
latex_color = "quarto-callout-important-color",
latex_frame_color = "quarto-callout-important-color-frame",
fa_icon = "faExclamation",
fa_icon_typst = "fa-exclamation"
},
caution = {
color = constants.kColorCaution,
background_color = constants.kBackgroundColorCaution,
latex_color = "quarto-callout-caution-color",
latex_frame_color = "quarto-callout-caution-color-frame",
fa_icon = "faFire",
fa_icon_typst = "fa-fire"
},
tip = {
color = constants.kColorTip,
background_color = constants.kBackgroundColorTip,
latex_color = "quarto-callout-tip-color",
latex_frame_color = "quarto-callout-tip-color-frame",
fa_icon = "faLightbulb",
fa_icon_typst = "fa-lightbulb"
},

__other = {
color = constants.kColorUnknown,
background_color = constants.kColorUnknown,
latex_color = "quarto-callout-color",
latex_frame_color = "quarto-callout-color-frame",
fa_icon = nil,
fa_icon_typst = nil
}
}

setmetatable(callout_attrs, {
__index = function(tbl, key)
return tbl.__other
end
})

local function htmlColorForType(type)
return callout_attrs[type].color
end

local function htmlBackgroundColorForType(type)
return callout_attrs[type].background_color
end

local function latexColorForType(type)
return callout_attrs[type].latex_color
end

local function latexFrameColorForType(type)
return callout_attrs[type].latex_frame_color
end

local function iconForType(type)
return callout_attrs[type].fa_icon
end

local function isBuiltInType(type)
local icon = iconForType(type)
return icon ~= nil
end

local function displayName(type)
local defaultName = type:sub(1,1):upper()..type:sub(2)
return param("callout-" .. type .. "-title", defaultName)
end

return {
decorate_callout_title_with_crossref = decorate_callout_title_with_crossref,
callout_attrs = callout_attrs,

-- TODO capitalization
resolveCalloutContents = resolveCalloutContents,
docxCalloutImage = docxCalloutImage,

htmlColorForType = htmlColorForType,
htmlBackgroundColorForType = htmlBackgroundColorForType,
latexColorForType = latexColorForType,
latexFrameColorForType = latexFrameColorForType,
iconForType = iconForType,
isBuiltInType = isBuiltInType,
displayName = displayName,
}
35 changes: 35 additions & 0 deletions src/resources/filters/modules/classpredicates.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
-- classpredicates.lua
-- Copyright (C) 2024 Posit Software, PBC

local function isCell(el)
return el.classes:includes("cell")
end

local function isCodeCellOutput(el)
return el.classes:includes("cell-output")
end

local function isCallout(class)
return class == 'callout' or class:match("^callout%-")
end

local function isDocxCallout(class)
return class == "docx-callout"
end

local function isCodeCell(class)
return class == "cell"
end

local function isCodeCellDisplay(class)
return class == "cell-output-display"
end

return {
isCallout = isCallout,
isCell = isCell,
isCodeCell = isCodeCell,
isCodeCellDisplay = isCodeCellDisplay,
isCodeCellOutput = isCodeCellOutput,
isDocxCallout = isDocxCallout
}
45 changes: 44 additions & 1 deletion src/resources/filters/modules/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,30 @@ local kLangCommentChars = {
}
local kDefaultCodeAnnotationComment = {"#"}

-- These colors are used as background colors with an opacity of 0.75
local kColorUnknown = "909090"
local kColorNote = "0758E5"
local kColorImportant = "CC1914"
local kColorWarning = "EB9113"
local kColorTip = "00A047"
local kColorCaution = "FC5300"

-- these colors are used with no-opacity
local kColorUnknownFrame = "acacac"
local kColorNoteFrame = "4582ec"
local kColorImportantFrame = "d9534f"
local kColorWarningFrame = "f0ad4e"
local kColorTipFrame = "02b875"
local kColorCautionFrame = "fd7e14"

local kBackgroundColorUnknown = "e6e6e6"
local kBackgroundColorNote = "dae6fb"
local kBackgroundColorImportant = "f7dddc"
local kBackgroundColorWarning = "fcefdc"
local kBackgroundColorTip = "ccf1e3"
local kBackgroundColorCaution = "ffe5d0"


return {
kCitation = kCitation,
kContainerId = kContainerId,
Expand Down Expand Up @@ -199,5 +223,24 @@ return {
kLangCommentChars = kLangCommentChars,
kDefaultCodeAnnotationComment = kDefaultCodeAnnotationComment,
kHtmlTableProcessing = kHtmlTableProcessing,
kHtmlPreTagProcessing = kHtmlPreTagProcessing
kHtmlPreTagProcessing = kHtmlPreTagProcessing,

kColorUnknown = kColorUnknown,
kColorNote = kColorNote,
kColorImportant = kColorImportant,
kColorWarning = kColorWarning,
kColorTip = kColorTip,
kColorCaution = kColorCaution,
kColorUnknownFrame = kColorUnknownFrame,
kColorNoteFrame = kColorNoteFrame,
kColorImportantFrame = kColorImportantFrame,
kColorWarningFrame = kColorWarningFrame,
kColorTipFrame = kColorTipFrame,
kColorCautionFrame = kColorCautionFrame,
kBackgroundColorUnknown = kBackgroundColorUnknown,
kBackgroundColorNote = kBackgroundColorNote,
kBackgroundColorImportant = kBackgroundColorImportant,
kBackgroundColorWarning = kBackgroundColorWarning,
kBackgroundColorTip = kBackgroundColorTip,
kBackgroundColorCaution = kBackgroundColorCaution,
}
22 changes: 22 additions & 0 deletions src/resources/filters/modules/import_all.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- import_all.lua
-- imports all modules into _quarto.modules

_quarto.modules = {
astshortcode = require("modules/astshortcode"),
authors = require("modules/authors"),
callouts = require("modules/callouts"),
classpredicates = require("modules/classpredicates"),
constants = require("modules/constants"),
dashboard = require("modules/dashboard"),
filenames = require("modules/filenames"),
filters = require("modules/filters"),
license = require("modules/license"),
lightbox = require("modules/lightbox"),
mediabag = require("modules/mediabag"),
openxml = require("modules/openxml"),
patterns = require("modules/patterns"),
scope = require("modules/scope"),
string = require("modules/string"),
tablecolwidths = require("modules/tablecolwidths"),
typst = require("modules/typst")
}
35 changes: 35 additions & 0 deletions src/resources/filters/modules/openxml.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
-- openxml.lua
-- Copyright (C) 2024 Posit Software, PBC

local function openXmlPara(para, spacing)
local xmlPara = pandoc.Para({
pandoc.RawInline("openxml", "<w:pPr>\n<w:spacing " .. spacing .. "/>\n</w:pPr>")
})
tappend(xmlPara.content, para.content)
return xmlPara
end

local function removeParagraphPadding(contents)
if #contents > 0 then

if #contents == 1 then
if contents[1].t == "Para" then
contents[1] = openXmlPara(contents[1], 'w:before="16" w:after="16"')
end
else
if contents[1].t == "Para" then
contents[1] = openXmlPara(contents[1], 'w:before="16"')
end

if contents[#contents].t == "Para" then
contents[#contents] = openXmlPara(contents[#contents], 'w:after="16"')
end
end
end
end

return {
-- TODO capitalization
openXmlPara = openXmlPara,
removeParagraphPadding = removeParagraphPadding
}
Loading

0 comments on commit a26cc1c

Please sign in to comment.