From 876097c30e774b3549858ca6d06947ef5eac2f22 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Thu, 16 May 2024 15:58:49 -0700 Subject: [PATCH] lua - more local variable cleanup from non-modules --- src/resources/filters/customnodes/callout.lua | 29 +- src/resources/filters/main.lua | 2 + src/resources/filters/modules/import_all.lua | 22 ++ src/resources/filters/quarto-post/docx.lua | 353 +++++++++--------- src/resources/filters/quarto-post/jats.lua | 23 +- src/resources/filters/quarto-post/latex.lua | 19 +- 6 files changed, 228 insertions(+), 220 deletions(-) create mode 100644 src/resources/filters/modules/import_all.lua diff --git a/src/resources/filters/customnodes/callout.lua b/src/resources/filters/customnodes/callout.lua index cc8e9e211b0..d5548ae779d 100644 --- a/src/resources/filters/customnodes/callout.lua +++ b/src/resources/filters/customnodes/callout.lua @@ -1,17 +1,12 @@ -- callout.lua -- Copyright (C) 2021-2022 Posit Software, PBC -local constants = require("modules/constants") -local class_predicates = require("modules/classpredicates") - -local function _main() - local callouts = require("modules/callouts") - +function _callout_main() local calloutidx = 1 local function calloutType(div) for _, class in ipairs(div.attr.classes) do - if class_predicates.isCallout(class) then + if _quarto.modules.classpredicates.isCallout(class) then local type = class:match("^callout%-(.*)") if type == nil then type = "none" @@ -40,7 +35,7 @@ local function _main() -- an HTML callout div local function calloutDiv(node) - node = callouts.decorate_callout_title_with_crossref(node) + node = _quarto.modules.callouts.decorate_callout_title_with_crossref(node) -- the first heading is the title local div = pandoc.Div({}) @@ -66,8 +61,8 @@ local function _main() end }) - if calloutAppearance == constants.kCalloutAppearanceDefault and pandoc.utils.stringify(title) == "" and not found then - title = quarto.utils.as_inlines(pandoc.Plain(callouts.displayName(node.type))) + if calloutAppearance == _quarto.modules.constants.kCalloutAppearanceDefault and pandoc.utils.stringify(title) == "" and not found then + title = quarto.utils.as_inlines(pandoc.Plain(_quarto.modules.callouts.displayName(node.type))) end -- Make an outer card div and transfer classes and id @@ -98,7 +93,7 @@ local function _main() local noicon = "" -- Check to see whether this is a recognized type - if icon == false or not callouts.isBuiltInType(callout_type) or type == nil then + if icon == false or not _quarto.modules.callouts.isBuiltInType(callout_type) or type == nil then noicon = " no-icon" calloutDiv.attr.classes:insert("no-icon") end @@ -272,7 +267,7 @@ local function _main() _quarto.ast.add_renderer("Callout", function(_) return true end, function(node) - node = callouts.decorate_callout_title_with_crossref(node) + node = _quarto.modules.callouts.decorate_callout_title_with_crossref(node) local contents = resolveCalloutContents(node, true) local callout = pandoc.BlockQuote(contents) local result = pandoc.Div(callout, pandoc.Attr(node.attr.identifier or "")) @@ -291,8 +286,8 @@ local function _main() local calloutAppearance = node.appearance local hasIcon = node.icon - if calloutAppearance == constants.kCalloutAppearanceDefault and pandoc.utils.stringify(title) == "" then - title = callouts.displayName(type) + if calloutAppearance == _quarto.constants.kCalloutAppearanceDefault and pandoc.utils.stringify(title) == "" then + title = _quarto.modules.callouts.displayName(type) end -- the body of the callout @@ -378,7 +373,7 @@ local function _main() end, function(callout) ensure_typst_font_awesome() - local attrs = callouts.callout_attrs[callout.type] + local attrs = _quarto.modules.callouts.callout_attrs[callout.type] local background_color, icon_color, icon if attrs == nil then background_color = "white" @@ -392,7 +387,7 @@ local function _main() local title = callout.title if title == nil then - title = pandoc.Plain(callouts.displayName(callout.type)) + title = pandoc.Plain(_quarto.modules.callouts.displayName(callout.type)) end local typst_callout = _quarto.format.typst.function_call("callout", { @@ -425,7 +420,7 @@ local function _main() return calloutDocx(callout) end) end -_main() +_callout_main() function docx_callout_and_table_fixup() if not _quarto.format.isDocxOutput() then diff --git a/src/resources/filters/main.lua b/src/resources/filters/main.lua index 6b57fda6af8..848a2b0d3ea 100644 --- a/src/resources/filters/main.lua +++ b/src/resources/filters/main.lua @@ -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") diff --git a/src/resources/filters/modules/import_all.lua b/src/resources/filters/modules/import_all.lua new file mode 100644 index 00000000000..13f1360e124 --- /dev/null +++ b/src/resources/filters/modules/import_all.lua @@ -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") +} \ No newline at end of file diff --git a/src/resources/filters/quarto-post/docx.lua b/src/resources/filters/quarto-post/docx.lua index 7267a093389..79fb092af4a 100644 --- a/src/resources/filters/quarto-post/docx.lua +++ b/src/resources/filters/quarto-post/docx.lua @@ -3,201 +3,196 @@ -- -- renders AST nodes to docx -local constants = require("modules/constants") -local class_predicates = require("modules/classpredicates") -local callouts = require("modules/callouts") -local openxml = require("modules/openxml") - -local function calloutDocxDefault(node, type, hasIcon) - local title = quarto.utils.as_inlines(node.title) - local color = callouts.htmlColorForType(type) - local backgroundColor = callouts.htmlBackgroundColorForType(type) - - local tablePrefix = [[ - - - - - - - - - - - - - - - - - - - - - - +function calloutDocx(node) + local function calloutDocxDefault(node, type, hasIcon) + local title = quarto.utils.as_inlines(node.title) + local color = _quarto.modules.callouts.htmlColorForType(type) + local backgroundColor = _quarto.modules.callouts.htmlBackgroundColorForType(type) + + local tablePrefix = [[ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ]] + local calloutContents = pandoc.List({ + pandoc.RawBlock("openxml", tablePrefix:gsub('$background', backgroundColor):gsub('$color', color)), + }) + + -- Create a title if there isn't already one + if pandoc.utils.stringify(title) == "" then + title = quarto.utils.as_inlines(pandoc.Plain(_quarto.modules.callouts.displayName(node.type))) + end + + -- add the image to the title, if needed + local calloutImage = _quarto.modules.callouts.docxCalloutImage(type); + if hasIcon and calloutImage ~= nil then + -- Create a paragraph with the icon, spaces, and text + local image_title = pandoc.List({ + pandoc.RawInline("openxml", '\n\n\n'), + calloutImage, + pandoc.Space(), + pandoc.Space()}) + tappend(image_title, title) + calloutContents:insert(pandoc.Para(image_title)) + else + local titleRaw = _quarto.modules.openxml.openXmlPara(pandoc.Para(title), 'w:before="16" w:after="16"') + calloutContents:insert(titleRaw) + end + + + -- end the title row and start the body row + local tableMiddle = [[ + + + + + + + - - - + + - ]] - local calloutContents = pandoc.List({ - pandoc.RawBlock("openxml", tablePrefix:gsub('$background', backgroundColor):gsub('$color', color)), - }) - - -- Create a title if there isn't already one - if pandoc.utils.stringify(title) == "" then - title = quarto.utils.as_inlines(pandoc.Plain(callouts.displayName(node.type))) - end - - -- add the image to the title, if needed - local calloutImage = callouts.docxCalloutImage(type); - if hasIcon and calloutImage ~= nil then - -- Create a paragraph with the icon, spaces, and text - local image_title = pandoc.List({ - pandoc.RawInline("openxml", '\n\n\n'), - calloutImage, - pandoc.Space(), - pandoc.Space()}) - tappend(image_title, title) - calloutContents:insert(pandoc.Para(image_title)) - else - local titleRaw = openxml.openXmlPara(pandoc.Para(title), 'w:before="16" w:after="16"') - calloutContents:insert(titleRaw) - end - - -- end the title row and start the body row - local tableMiddle = [[ + ]] + calloutContents:insert(pandoc.Div(pandoc.RawBlock("openxml", tableMiddle))) + + -- the main contents of the callout + local contents = quarto.utils.as_blocks(node.content) + + -- ensure there are no nested callouts + if contents:find_if(function(el) + return is_regular_node(el, "Div") and el.attr.classes:find_if(_quarto.modules.classpredicates.isDocxCallout) ~= nil + end) ~= nil then + fail("Found a nested callout in the document. Please fix this issue and try again.") + end + + -- remove padding from existing content and add it + _quarto.modules.openxml.removeParagraphPadding(contents) + tappend(calloutContents, contents) + + -- close the table + local suffix = pandoc.List({pandoc.RawBlock("openxml", [[ - - - - - - - - - - - - - - ]] - calloutContents:insert(pandoc.Div(pandoc.RawBlock("openxml", tableMiddle))) - - -- the main contents of the callout - local contents = quarto.utils.as_blocks(node.content) - - -- ensure there are no nested callouts - if contents:find_if(function(el) - return is_regular_node(el, "Div") and el.attr.classes:find_if(class_predicates.isDocxCallout) ~= nil - end) ~= nil then - fail("Found a nested callout in the document. Please fix this issue and try again.") - end + + + ]])}) + tappend(calloutContents, suffix) - -- remove padding from existing content and add it - openxml.removeParagraphPadding(contents) - tappend(calloutContents, contents) - - -- close the table - local suffix = pandoc.List({pandoc.RawBlock("openxml", [[ - - - - ]])}) - tappend(calloutContents, suffix) - - -- return the callout - local callout = pandoc.Div(calloutContents, pandoc.Attr("", {"docx-callout"})) - return callout -end - - -local function calloutDocxSimple(node, type, hasIcon) - local color = callouts.htmlColorForType(type) - local title = quarto.utils.as_inlines(node.title) - - local tablePrefix = [[ - - - - - - - - - - - - - - - - - - - ]] - - local prefix = pandoc.List({ - pandoc.RawBlock("openxml", tablePrefix:gsub('$color', color)), - }) - - local calloutImage = callouts.docxCalloutImage(type) - if hasIcon and calloutImage ~= nil then - local imagePara = pandoc.Para({ - pandoc.RawInline("openxml", '\n\n\n'), calloutImage}) - prefix:insert(pandoc.RawBlock("openxml", '')) - prefix:insert(imagePara) - prefix:insert(pandoc.RawBlock("openxml", "\n")) - else - prefix:insert(pandoc.RawBlock("openxml", '')) - end - - local suffix = pandoc.List({pandoc.RawBlock("openxml", [[ - - - - ]])}) - - local calloutContents = pandoc.List({}) - tappend(calloutContents, prefix) - - -- deal with the title, if present - if title ~= nil then - local titlePara = pandoc.Para(pandoc.Strong(title)) - calloutContents:insert(openxml.openXmlPara(titlePara, 'w:before="16" w:after="64"')) + -- return the callout + local callout = pandoc.Div(calloutContents, pandoc.Attr("", {"docx-callout"})) + return callout end - -- convert to open xml paragraph - local contents = pandoc.List({}) -- use as pandoc.List() for find_if - contents:extend(quarto.utils.as_blocks(node.content)) - openxml.removeParagraphPadding(contents) - -- ensure there are no nested callouts - if contents:find_if(function(el) - return is_regular_node(el, "Div") and el.attr.classes:find_if(class_predicates.isDocxCallout) ~= nil - end) ~= nil then - fail("Found a nested callout in the document. Please fix this issue and try again.") + local function calloutDocxSimple(node, type, hasIcon) + local color = _quarto.modules.callouts.htmlColorForType(type) + local title = quarto.utils.as_inlines(node.title) + + local tablePrefix = [[ + + + + + + + + + + + + + + + + + + + ]] + + local prefix = pandoc.List({ + pandoc.RawBlock("openxml", tablePrefix:gsub('$color', color)), + }) + + local calloutImage = _quarto.modules.callouts.docxCalloutImage(type) + if hasIcon and calloutImage ~= nil then + local imagePara = pandoc.Para({ + pandoc.RawInline("openxml", '\n\n\n'), calloutImage}) + prefix:insert(pandoc.RawBlock("openxml", '')) + prefix:insert(imagePara) + prefix:insert(pandoc.RawBlock("openxml", "\n")) + else + prefix:insert(pandoc.RawBlock("openxml", '')) + end + + local suffix = pandoc.List({pandoc.RawBlock("openxml", [[ + + + + ]])}) + + local calloutContents = pandoc.List({}) + tappend(calloutContents, prefix) + + -- deal with the title, if present + if title ~= nil then + local titlePara = pandoc.Para(pandoc.Strong(title)) + calloutContents:insert(_quarto.modules.openxml.openXmlPara(titlePara, 'w:before="16" w:after="64"')) + end + + -- convert to open xml paragraph + local contents = pandoc.List({}) -- use as pandoc.List() for find_if + contents:extend(quarto.utils.as_blocks(node.content)) + _quarto.modules.openxml.removeParagraphPadding(contents) + + -- ensure there are no nested callouts + if contents:find_if(function(el) + return is_regular_node(el, "Div") and el.attr.classes:find_if(_quarto.modules.classpredicates.isDocxCallout) ~= nil + end) ~= nil then + fail("Found a nested callout in the document. Please fix this issue and try again.") + end + + tappend(calloutContents, contents) + tappend(calloutContents, suffix) + + local callout = pandoc.Div(calloutContents, pandoc.Attr("", {"docx-callout"})) + return callout end - - tappend(calloutContents, contents) - tappend(calloutContents, suffix) - - local callout = pandoc.Div(calloutContents, pandoc.Attr("", {"docx-callout"})) - return callout -end - -function calloutDocx(node) + node = decorate_callout_title_with_crossref(node) local type = node.type local appearance = node.appearance local hasIcon = node.icon - if appearance == constants.kCalloutAppearanceDefault then + if appearance == _quarto.modules.constants.kCalloutAppearanceDefault then return calloutDocxDefault(node, type, hasIcon) else return calloutDocxSimple(node, type, hasIcon) diff --git a/src/resources/filters/quarto-post/jats.lua b/src/resources/filters/quarto-post/jats.lua index 7291f5ea80d..e9729fd3280 100644 --- a/src/resources/filters/quarto-post/jats.lua +++ b/src/resources/filters/quarto-post/jats.lua @@ -3,9 +3,6 @@ local normalizeAuthors = require 'modules/authors' local normalizeLicense = require 'modules/license' -local constants = require("modules/constants") -local class_predicates = require("modules/classpredicates") - local function isCell(el) return el.classes:includes("cell") end @@ -31,15 +28,15 @@ local function jatsMeta(meta) local hasLicense = meta[normalizeLicense.constants.license] ~= nil local hasPermissions = hasCopyright or hasLicense - if meta[constants.kQuartoInternal] == nil then - meta[constants.kQuartoInternal] = {} + if meta[_quarto.modules.constants.kQuartoInternal] == nil then + meta[_quarto.modules.constants.kQuartoInternal] = {} end - meta[constants.kQuartoInternal][constants.kHasAuthorNotes] = hasNotes; - meta[constants.kQuartoInternal][constants.kHasPermissions] = hasPermissions; + meta[_quarto.modules.constants.kQuartoInternal][_quarto.modules.constants.kHasAuthorNotes] = hasNotes; + meta[_quarto.modules.constants.kQuartoInternal][_quarto.modules.constants.kHasPermissions] = hasPermissions; -- normalize keywords into tags if they're present and tags aren't - if meta[constants.kTags] == nil and meta[constants.kKeywords] ~= nil and meta[constants.kKeywords].t == "Table" then - meta[constants.kKeywords] = meta[constants.kTags] + if meta[_quarto.modules.constants.kTags] == nil and meta[_quarto.modules.constants.kKeywords] ~= nil and meta[_quarto.modules.constants.kKeywords].t == "Table" then + meta[_quarto.modules.constants.kKeywords] = meta[_quarto.modules.constants.kTags] end return meta @@ -162,7 +159,7 @@ function jatsSubarticle() -- this is a notebook cell, handle it if isCell(div) then - if class_predicates.isCodeCell(div) then + if _quarto.modules.class_predicates.isCodeCell(div) then -- if this is an executable notebook cell, walk the contents and add identifiers -- to the outputs @@ -195,20 +192,20 @@ function jatsSubarticle() if (isCodeCellOutput(childEl)) then childEl.identifier = parentId .. '-output-' .. count count = count + 1 - return renderCellOutput(childEl, constants.kNoteBookOutput) + return renderCellOutput(childEl, _quarto.modules.constants.kNoteBookOutput) end end }) -- render the cell - return renderCell(div, constants.kNoteBookCode) + return renderCell(div, _quarto.modules.constants.kNoteBookCode) else if #div.content == 0 then -- eat empty markdown cells return {} else -- the is a valid markdown cell, let it through - return renderCell(div, constants.kNoteBookContent) + return renderCell(div, _quarto.modules.constants.kNoteBookContent) end end elseif isCodeCellOutput(div) then diff --git a/src/resources/filters/quarto-post/latex.lua b/src/resources/filters/quarto-post/latex.lua index 13fe9509573..a01d89f0c2f 100644 --- a/src/resources/filters/quarto-post/latex.lua +++ b/src/resources/filters/quarto-post/latex.lua @@ -3,9 +3,6 @@ -- -- renders AST nodes to LaTeX -local constants = require("modules/constants") -local callouts = require("modules/callouts") - local callout_counters = {} local function ensure_callout_counter(ref) @@ -33,10 +30,10 @@ function latexCalloutBoxDefault(title, callout_type, icon, callout) local borderWidth = '.15mm' local borderRadius = '.35mm' local leftPad = '2mm' - local color = callouts.latexColorForType(callout_type) - local frameColor = callouts.latexFrameColorForType(callout_type) + local color = _quarto.modules.callouts.latexColorForType(callout_type) + local frameColor = _quarto.modules.callouts.latexFrameColorForType(callout_type) - local iconForType = callouts.iconForType(callout_type) + local iconForType = _quarto.modules.callouts.iconForType(callout_type) local calloutContents = pandoc.List({}); @@ -102,8 +99,8 @@ function latexCalloutBoxSimple(title, type, icon, callout) local borderWidth = '.15mm' local borderRadius = '.35mm' local leftPad = '2mm' - local color = callouts.latexColorForType(type) - local colorFrame = callouts.latexFrameColorForType(type) + local color = _quarto.modules.callouts.latexColorForType(type) + local colorFrame = _quarto.modules.callouts.latexFrameColorForType(type) if title == nil then title = "" @@ -146,7 +143,7 @@ function latexCalloutBoxSimple(title, type, icon, callout) local endInlines = { pandoc.RawInline('latex', '\n\\end{tcolorbox}') } -- generate the icon and use a minipage to position it - local iconForCat = callouts.iconForType(type) + local iconForCat = _quarto.modules.callouts.iconForType(type) if icon ~= false and iconForCat ~= nil then local iconName = '\\' .. iconForCat local iconColSize = '5.5mm' @@ -361,9 +358,9 @@ function render_latex() -- generate the callout box local callout - if calloutAppearance == constants.kCalloutAppearanceDefault then + if calloutAppearance == _quarto.modules.constants.kCalloutAppearanceDefault then if title == nil then - title = callouts.displayName(type) + title = _quarto.modules.callouts.displayName(type) else title = pandoc.write(pandoc.Pandoc(title), 'latex') end