Skip to content

Commit da95ea7

Browse files
authored
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
1 parent 93d42d9 commit da95ea7

5 files changed

+91
-29
lines changed

_extensions/webr/_extension.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: webr
22
title: Embedded webr code cells
33
author: James Joseph Balamuta
4-
version: 0.4.2-dev.7
4+
version: 0.4.2-dev.8
55
quarto-required: ">=1.4.554"
66
contributes:
77
filters:

_extensions/webr/webr.lua

+15-25
Original file line numberDiff line numberDiff line change
@@ -581,32 +581,22 @@ local function qwebrJSCellInsertionCode(counter)
581581
end
582582

583583
--- Remove lines with only whitespace until the first non-whitespace character is detected.
584-
---@param codeText table
584+
---@param codeLines table
585585
---@return table
586-
local function removeEmptyLinesUntilContent(codeText)
587-
-- Iterate through each line in the codeText table
588-
for _, value in ipairs(codeText) do
589-
-- Detect leading whitespace (newline, return character, or empty space)
590-
local detectedWhitespace = string.match(value, "^%s*$")
591-
592-
-- Check if the detectedWhitespace is either an empty string or nil
593-
-- This indicates whitespace was detected
594-
if isVariableEmpty(detectedWhitespace) then
595-
-- Delete empty space
596-
table.remove(codeText, 1)
597-
else
598-
-- Stop the loop as we've now have content
599-
break
600-
end
586+
local function removeEmptyLinesUntilContent(codeLines)
587+
588+
-- Remove empty lines at the beginning of the code block
589+
while codeLines[1] and string.match(codeLines[1], "^%s*$") do
590+
table.remove(codeLines, 1)
601591
end
602592

603593
-- Return the modified table
604-
return codeText
594+
return codeLines
605595
end
606596

607597
--- Extract Quarto code cell options from the block's text
608598
---@param block pandoc.CodeBlock
609-
---@return string
599+
---@return table
610600
---@return table
611601
local function extractCodeBlockOptions(block)
612602

@@ -637,11 +627,11 @@ local function extractCodeBlockOptions(block)
637627
-- Merge cell options with default options
638628
cellOptions = mergeCellOptions(cellOptions)
639629

640-
-- Set the codeblock text to exclude the special comments.
641-
cellCode = table.concat(newCodeLines, '\n')
630+
-- Remove empty lines at the beginning of the code block
631+
local restructuredCodeCell = removeEmptyLinesUntilContent(newCodeLines)
642632

643633
-- Return the code alongside options
644-
return cellCode, cellOptions
634+
return restructuredCodeCell, cellOptions
645635
end
646636

647637
--- Replace the code cell with a webR-powered cell
@@ -688,7 +678,7 @@ local function enableWebRCodeCell(el)
688678

689679
-- Local code cell storage
690680
local cellOptions = {}
691-
local cellCode = ''
681+
local cellCode = {}
692682

693683
-- Convert webr-specific option commands into attributes
694684
cellCode, cellOptions = extractCodeBlockOptions(el)
@@ -706,13 +696,13 @@ local function enableWebRCodeCell(el)
706696
end
707697
end
708698

709-
-- Remove space left between options and code contents
710-
cellCode = removeEmptyLinesUntilContent(cellCode)
699+
-- Set the codeblock text to exclude the special comments.
700+
local cellCodeMerged = table.concat(cellCode, '\n')
711701

712702
-- Create a new table for the CodeBlock
713703
local codeBlockData = {
714704
id = qwebrCounter,
715-
code = cellCode,
705+
code = cellCodeMerged,
716706
options = cellOptions
717707
}
718708

docs/qwebr-code-cell-demos.qmd

+7-3
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,19 @@ Lines of code can be highlighted using `editor-code-line-numbers` to draw attent
102102

103103
- `editor-code-line-numbers: 1-3` will highlight lines 1 to 3.
104104
- `editor-code-line-numbers: 1,3,6` will highlight lines 1, 3, and 6.
105-
- `editor-code-line-numbers: 1-3,6` will highlight lines 1 to 3 and 6.
105+
- `editor-code-line-numbers: 1,3-5,7` will highlight lines 1, 3 to 5, and 7.
106+
107+
We can see the `1,3-5,7` example in the following code cell:
106108

107109
::: {.panel-tabset}
108110
## `{quarto-webr}` Output
109111

110112
```{webr-r}
111113
#| read-only: true
112-
#| editor-code-line-numbers: 1-3,6
114+
#| editor-code-line-numbers: 1,3-5,7
113115
114116
# This is a comment
117+
115118
1 + 1
116119
2 + 2
117120
3 + 3
@@ -123,9 +126,10 @@ Lines of code can be highlighted using `editor-code-line-numbers` to draw attent
123126

124127
```{{webr-r}}
125128
#| read-only: true
126-
#| editor-code-line-numbers: 1-3,6
129+
#| editor-code-line-numbers: 1,3-5,7
127130
128131
# This is a comment
132+
129133
1 + 1
130134
2 + 2
131135
3 + 3

docs/qwebr-release-notes.qmd

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Features listed under the `-dev` version have not yet been solidified and may ch
5757

5858
## Bug fixes
5959

60+
- Newline characters that separate options from code lines are now removed. ([#217](https://github.com/coatless/quarto-webr/pulls/217))
6061
- 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))
6162

6263
## Documentation
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
title: "Test: Space Removal after Options"
3+
format: html
4+
engine: knitr
5+
filters:
6+
- webr
7+
---
8+
9+
Check that the editor contents avoids retaining spaces after the options.
10+
11+
12+
## Option with a Single Space
13+
14+
```{webr-r}
15+
#| autorun: true
16+
17+
print("test")
18+
1 + 1
19+
```
20+
21+
## Multiple Options with a Single Space
22+
23+
```{webr-r}
24+
#| read-only: true
25+
#| editor-code-line-numbers: 1,3-5,7
26+
27+
# This is a comment
28+
29+
1 + 1
30+
2 + 2
31+
3 + 3
32+
33+
# This is another comment
34+
```
35+
36+
## Multiple Spaces
37+
38+
```{webr-r}
39+
#| autorun: true
40+
41+
42+
43+
44+
print("test")
45+
46+
1 + 1
47+
48+
```
49+
50+
51+
## No Space
52+
53+
```{webr-r}
54+
#| autorun: true
55+
print("test")
56+
57+
1 + 1
58+
```
59+
60+
61+
## No Options
62+
63+
```{webr-r}
64+
fit <- lm(mpg ~ vs, data = mtcars)
65+
66+
summary(fit)
67+
```

0 commit comments

Comments
 (0)