Skip to content

Commit

Permalink
Remove space after options (#217)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
coatless authored Jun 18, 2024
1 parent 93d42d9 commit da95ea7
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 29 deletions.
2 changes: 1 addition & 1 deletion _extensions/webr/_extension.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
40 changes: 15 additions & 25 deletions _extensions/webr/webr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
10 changes: 7 additions & 3 deletions docs/qwebr-code-cell-demos.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/qwebr-release-notes.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 67 additions & 0 deletions tests/qwebr-test-option-space-removal.qmd
Original file line number Diff line number Diff line change
@@ -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)
```

0 comments on commit da95ea7

Please sign in to comment.