Skip to content

Commit 71efc8a

Browse files
cscheidclaude
authored andcommitted
Add unit tests for percent format and fix regex bug
- Add comprehensive unit tests for percent format notebook parsing - Test detection and conversion for Python, Julia, R, and q languages - Fix isJupyterPercentScript regex: properly group alternation (markdown|raw) and use multiline mode so ^ matches start of any line - Add changelog entries for q/kdb+ support and regex fix Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent db82f6e commit 71efc8a

12 files changed

Lines changed: 275 additions & 2 deletions

File tree

‎news/changelog-1.9.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ All changes included in 1.9:
135135
### `jupyter`
136136

137137
- ([#13748](https://github.com/quarto-dev/quarto-cli/pull/13748)): Fix stdin encoding to UTF-8 on Windows to correctly handle JSON in documents containing non-ASCII characters.
138+
- ([#13936](https://github.com/quarto-dev/quarto-cli/pull/13936)): Add support for q/kdb+ programming language in percent format notebooks and code cell options. (author: @benlubas)
139+
- ([#13936](https://github.com/quarto-dev/quarto-cli/pull/13936)): Fix `isJupyterPercentScript` regex to correctly detect percent scripts with `[raw]` cells and cells not at the start of the file. The regex now properly groups the alternation `(markdown|raw)` and uses multiline mode.
138140

139141
### `knitr`
140142

‎src/core/jupyter/percent.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ export function isJupyterPercentScript(file: string, extensions?: string[]) {
3535
if (availableExtensions.includes(ext)) {
3636
const text = Deno.readTextFileSync(file);
3737
const cms = kLangCommentChars[kLanguageExtensions[ext]];
38-
const pat = new RegExp(`^\\s*${cms}\\s*%%+\\s+\\[markdown|raw\\]`);
38+
// Use multiline mode (m) so ^ matches start of any line, not just start of file.
39+
// Group the alternation properly: (markdown|raw) not markdown|raw
40+
const pat = new RegExp(`^\\s*${cms}\\s*%%+\\s+\\[(markdown|raw)\\]`, "m");
3941
return !!text.match(pat);
40-
4142
} else {
4243
return false;
4344
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This file contains the text "raw]" but is NOT a percent script
2+
# It should test that the regex doesn't match "raw]" anywhere in the file
3+
# For example: handle raw] data or [raw] text in comments
4+
5+
def process():
6+
return "raw]"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This is a regular Python file, not a percent script
2+
# It has no special cell markers
3+
4+
def hello():
5+
print("Hello")
6+
7+
hello()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/ This is a regular q file, not a percent script
2+
/ It has no special cell markers
3+
4+
hello: {[] "Hello"}
5+
hello[]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# %% [markdown]
2+
# This is a markdown cell
3+
4+
# %%
5+
println("Hello from Julia")
6+
7+
# %% [markdown]
8+
# Another markdown cell
9+
10+
# %%
11+
x = 1 + 1
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# ---
2+
# jupyter:
3+
# kernelspec:
4+
# display_name: Python 3
5+
# language: python
6+
# name: python3
7+
# ---
8+
9+
# %%
10+
print("Code first")
11+
12+
# %% [markdown]
13+
# This markdown cell is not at the start of the file
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# %% [markdown]
2+
# This is a markdown cell
3+
4+
# %%
5+
print("Hello from Python")
6+
7+
# %% [markdown]
8+
# Another markdown cell
9+
10+
# %%
11+
x = 1 + 1

‎tests/docs/percent-format/test-q.q‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/ %% [markdown]
2+
/ This is a markdown cell
3+
4+
/ %%
5+
1+1
6+
7+
/ %% [markdown]
8+
/ Another markdown cell
9+
10+
/ %%
11+
x: 42

‎tests/docs/percent-format/test-r.r‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# %% [markdown]
2+
# This is a markdown cell
3+
4+
# %%
5+
print("Hello from R")
6+
7+
# %% [markdown]
8+
# Another markdown cell
9+
10+
# %%
11+
x <- 1 + 1

0 commit comments

Comments
 (0)