From da95ea7a86b2682a84049f309312da3ca1c9106c Mon Sep 17 00:00:00 2001 From: James J Balamuta Date: Tue, 18 Jun 2024 00:39:17 -0700 Subject: [PATCH] Remove space after options (#217) * Fix newline removal after options by parsing a table instead of a string * Add test * Add release note and bump extension * Simplify logic. * Add more test cases... * Improve number example --- _extensions/webr/_extension.yml | 2 +- _extensions/webr/webr.lua | 40 +++++--------- docs/qwebr-code-cell-demos.qmd | 10 +++- docs/qwebr-release-notes.qmd | 1 + tests/qwebr-test-option-space-removal.qmd | 67 +++++++++++++++++++++++ 5 files changed, 91 insertions(+), 29 deletions(-) create mode 100644 tests/qwebr-test-option-space-removal.qmd diff --git a/_extensions/webr/_extension.yml b/_extensions/webr/_extension.yml index 587d4fc..2d324bd 100644 --- a/_extensions/webr/_extension.yml +++ b/_extensions/webr/_extension.yml @@ -1,7 +1,7 @@ name: webr title: Embedded webr code cells author: James Joseph Balamuta -version: 0.4.2-dev.7 +version: 0.4.2-dev.8 quarto-required: ">=1.4.554" contributes: filters: diff --git a/_extensions/webr/webr.lua b/_extensions/webr/webr.lua index e8d9dad..fd8af70 100644 --- a/_extensions/webr/webr.lua +++ b/_extensions/webr/webr.lua @@ -581,32 +581,22 @@ local function qwebrJSCellInsertionCode(counter) end --- Remove lines with only whitespace until the first non-whitespace character is detected. ----@param codeText table +---@param codeLines table ---@return table -local function removeEmptyLinesUntilContent(codeText) - -- Iterate through each line in the codeText table - for _, value in ipairs(codeText) do - -- Detect leading whitespace (newline, return character, or empty space) - local detectedWhitespace = string.match(value, "^%s*$") - - -- Check if the detectedWhitespace is either an empty string or nil - -- This indicates whitespace was detected - if isVariableEmpty(detectedWhitespace) then - -- Delete empty space - table.remove(codeText, 1) - else - -- Stop the loop as we've now have content - break - end +local function removeEmptyLinesUntilContent(codeLines) + + -- Remove empty lines at the beginning of the code block + while codeLines[1] and string.match(codeLines[1], "^%s*$") do + table.remove(codeLines, 1) end -- Return the modified table - return codeText + return codeLines end --- Extract Quarto code cell options from the block's text ---@param block pandoc.CodeBlock ----@return string +---@return table ---@return table local function extractCodeBlockOptions(block) @@ -637,11 +627,11 @@ local function extractCodeBlockOptions(block) -- Merge cell options with default options cellOptions = mergeCellOptions(cellOptions) - -- Set the codeblock text to exclude the special comments. - cellCode = table.concat(newCodeLines, '\n') + -- Remove empty lines at the beginning of the code block + local restructuredCodeCell = removeEmptyLinesUntilContent(newCodeLines) -- Return the code alongside options - return cellCode, cellOptions + return restructuredCodeCell, cellOptions end --- Replace the code cell with a webR-powered cell @@ -688,7 +678,7 @@ local function enableWebRCodeCell(el) -- Local code cell storage local cellOptions = {} - local cellCode = '' + local cellCode = {} -- Convert webr-specific option commands into attributes cellCode, cellOptions = extractCodeBlockOptions(el) @@ -706,13 +696,13 @@ local function enableWebRCodeCell(el) end end - -- Remove space left between options and code contents - cellCode = removeEmptyLinesUntilContent(cellCode) + -- Set the codeblock text to exclude the special comments. + local cellCodeMerged = table.concat(cellCode, '\n') -- Create a new table for the CodeBlock local codeBlockData = { id = qwebrCounter, - code = cellCode, + code = cellCodeMerged, options = cellOptions } diff --git a/docs/qwebr-code-cell-demos.qmd b/docs/qwebr-code-cell-demos.qmd index 3eff38b..0d70207 100644 --- a/docs/qwebr-code-cell-demos.qmd +++ b/docs/qwebr-code-cell-demos.qmd @@ -102,16 +102,19 @@ Lines of code can be highlighted using `editor-code-line-numbers` to draw attent - `editor-code-line-numbers: 1-3` will highlight lines 1 to 3. - `editor-code-line-numbers: 1,3,6` will highlight lines 1, 3, and 6. -- `editor-code-line-numbers: 1-3,6` will highlight lines 1 to 3 and 6. +- `editor-code-line-numbers: 1,3-5,7` will highlight lines 1, 3 to 5, and 7. + +We can see the `1,3-5,7` example in the following code cell: ::: {.panel-tabset} ## `{quarto-webr}` Output ```{webr-r} #| read-only: true -#| editor-code-line-numbers: 1-3,6 +#| editor-code-line-numbers: 1,3-5,7 # This is a comment + 1 + 1 2 + 2 3 + 3 @@ -123,9 +126,10 @@ Lines of code can be highlighted using `editor-code-line-numbers` to draw attent ```{{webr-r}} #| read-only: true -#| editor-code-line-numbers: 1-3,6 +#| editor-code-line-numbers: 1,3-5,7 # This is a comment + 1 + 1 2 + 2 3 + 3 diff --git a/docs/qwebr-release-notes.qmd b/docs/qwebr-release-notes.qmd index c1f960d..c3b0dae 100644 --- a/docs/qwebr-release-notes.qmd +++ b/docs/qwebr-release-notes.qmd @@ -57,6 +57,7 @@ Features listed under the `-dev` version have not yet been solidified and may ch ## Bug fixes +- Newline characters that separate options from code lines are now removed. ([#217](https://github.com/coatless/quarto-webr/pulls/217)) - Prevented vertical scroll bars from always being present by modifying the adaptive container of the editor to always be at least 2 pixels greater than the editor's content instead of being the exact amount. ([#164](https://github.com/coatless/quarto-webr/issues/164)) ## Documentation diff --git a/tests/qwebr-test-option-space-removal.qmd b/tests/qwebr-test-option-space-removal.qmd new file mode 100644 index 0000000..b356f89 --- /dev/null +++ b/tests/qwebr-test-option-space-removal.qmd @@ -0,0 +1,67 @@ +--- +title: "Test: Space Removal after Options" +format: html +engine: knitr +filters: + - webr +--- + +Check that the editor contents avoids retaining spaces after the options. + + +## Option with a Single Space + +```{webr-r} +#| autorun: true + +print("test") +1 + 1 +``` + +## Multiple Options with a Single Space + +```{webr-r} +#| read-only: true +#| editor-code-line-numbers: 1,3-5,7 + +# This is a comment + +1 + 1 +2 + 2 +3 + 3 + +# This is another comment +``` + +## Multiple Spaces + +```{webr-r} +#| autorun: true + + + + +print("test") + +1 + 1 + +``` + + +## No Space + +```{webr-r} +#| autorun: true +print("test") + +1 + 1 +``` + + +## No Options + +```{webr-r} +fit <- lm(mpg ~ vs, data = mtcars) + +summary(fit) +```