From 6bad365e246b5dcf24642490515c22db61e9c13d Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Wed, 9 Apr 2025 08:19:22 -0400 Subject: [PATCH 1/4] kbd,adoc - emit error message instead of crashing when bad parameters are passed --- src/resources/extensions/quarto/kbd/kbd.lua | 28 ++++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/resources/extensions/quarto/kbd/kbd.lua b/src/resources/extensions/quarto/kbd/kbd.lua index 47fd4737d2c..f65e5a38b5b 100644 --- a/src/resources/extensions/quarto/kbd/kbd.lua +++ b/src/resources/extensions/quarto/kbd/kbd.lua @@ -32,10 +32,30 @@ return { end return pandoc.RawInline('html', '' .. default_arg_str .. '') - elseif quarto.doc.isFormat("asciidoc") and args and #args == 1 then - -- get the 'first' kbd shortcut as we can only produce on shortcut in asciidoc - local shortcutText = pandoc.utils.stringify(args[1]):gsub('-', '+') - return pandoc.RawInline("asciidoc", "kbd:[" .. shortcutText .. "]") + elseif quarto.doc.isFormat("asciidoc") then + if args and #args == 1 then + -- https://docs.asciidoctor.org/asciidoc/latest/macros/keyboard-macro/ + + -- get the 'first' kbd shortcut as we can only produce one shortcut in asciidoc + local shortcutText = pandoc.utils.stringify(args[1]):gsub('-', '+') + + -- from the docs: + -- If the last key is a backslash (\), it must be followed by a space. + -- Without this space, the processor will not recognize the macro. + -- If one of the keys is a closing square bracket (]), it must be preceded by a backslash. + -- Without the backslash escape, the macro will end prematurely. + + if shortcutText:sub(-1) == "\\" then + shortcutText = shortcutText .. " " + end + if shortcutText:find("]") then + shortcutText = shortcutText:gsub("]", "\\]") + end + + return pandoc.RawInline("asciidoc", "kbd:[" .. shortcutText .. "]") + else + return quarto.shortcode.error_output("kbd", "kbd only supports one positional argument", "inline") + end else -- example shortcodes -- {{< kbd Shift-Ctrl-P >}} From 92068760d9fcc5e3debd89c55c831f1606fed97b Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Wed, 9 Apr 2025 08:19:41 -0400 Subject: [PATCH 2/4] regression test --- tests/docs/smoke-all/2025/04/09/kbd-adoc-bad-input.qmd | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 tests/docs/smoke-all/2025/04/09/kbd-adoc-bad-input.qmd diff --git a/tests/docs/smoke-all/2025/04/09/kbd-adoc-bad-input.qmd b/tests/docs/smoke-all/2025/04/09/kbd-adoc-bad-input.qmd new file mode 100644 index 00000000000..60636a30ac4 --- /dev/null +++ b/tests/docs/smoke-all/2025/04/09/kbd-adoc-bad-input.qmd @@ -0,0 +1,5 @@ +--- +format: asciidoc +--- + +{{< kbd key="\\" >}} \ No newline at end of file From 2624a2f6b0b55b1a40342e7df6ac62b3977ace00 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Thu, 10 Apr 2025 13:48:22 -0400 Subject: [PATCH 3/4] kbd,asciidoc - explicitly escape inline --- src/resources/extensions/quarto/kbd/kbd.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/resources/extensions/quarto/kbd/kbd.lua b/src/resources/extensions/quarto/kbd/kbd.lua index f65e5a38b5b..d50c73aae50 100644 --- a/src/resources/extensions/quarto/kbd/kbd.lua +++ b/src/resources/extensions/quarto/kbd/kbd.lua @@ -52,7 +52,7 @@ return { shortcutText = shortcutText:gsub("]", "\\]") end - return pandoc.RawInline("asciidoc", "kbd:[" .. shortcutText .. "]") + return pandoc.RawInline("asciidoc", "kbd:[" .. stringEscape(shortcutText, "asciidoc") .. "]") else return quarto.shortcode.error_output("kbd", "kbd only supports one positional argument", "inline") end From fcfea337143ccc500380bf9babf3ad9e92bc50d0 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Thu, 10 Apr 2025 17:42:06 -0400 Subject: [PATCH 4/4] Revert "kbd,asciidoc - explicitly escape inline" This reverts commit 2624a2f6b0b55b1a40342e7df6ac62b3977ace00. --- src/resources/extensions/quarto/kbd/kbd.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/resources/extensions/quarto/kbd/kbd.lua b/src/resources/extensions/quarto/kbd/kbd.lua index d50c73aae50..f65e5a38b5b 100644 --- a/src/resources/extensions/quarto/kbd/kbd.lua +++ b/src/resources/extensions/quarto/kbd/kbd.lua @@ -52,7 +52,7 @@ return { shortcutText = shortcutText:gsub("]", "\\]") end - return pandoc.RawInline("asciidoc", "kbd:[" .. stringEscape(shortcutText, "asciidoc") .. "]") + return pandoc.RawInline("asciidoc", "kbd:[" .. shortcutText .. "]") else return quarto.shortcode.error_output("kbd", "kbd only supports one positional argument", "inline") end