-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lua,callout - fix title when non-empty but no strings. refactor into …
…modules to clean up globals
- Loading branch information
Showing
12 changed files
with
977 additions
and
873 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.