Skip to content

Commit 7d637e0

Browse files
authored
Merge pull request #189 from janpfeifer/fixPctPct
Fixed "%% <flags...>"
2 parents 21950d2 + f46cf3a commit 7d637e0

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

examples/tests/hello.ipynb

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,40 @@
7070
"name": "stdout",
7171
"output_type": "stream",
7272
"text": [
73-
"%exec worked for x=17\n"
73+
"Got x=17\n"
7474
]
7575
}
7676
],
7777
"source": [
78-
"// Test %exec and flags\n",
78+
"// Test flags\n",
7979
"var x = flag.Int(\"x\", 0, \"value of x to feed f()\")\n",
8080
"\n",
8181
"func f() {\n",
82-
" fmt.Printf(\"%%exec worked for x=%d\\n\", *x)\n",
82+
" fmt.Printf(\"Got x=%d\\n\", *x)\n",
8383
"}\n",
8484
"\n",
85-
"//gonb:%exec f -x=17"
85+
"%% -x=17\n",
86+
"f()"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 5,
92+
"id": "748bf745-247f-4c94-90d2-07dc68cd7dd5",
93+
"metadata": {},
94+
"outputs": [
95+
{
96+
"name": "stdout",
97+
"output_type": "stream",
98+
"text": [
99+
"Got x=13\n"
100+
]
101+
}
102+
],
103+
"source": [
104+
"// Test %exec\n",
105+
"\n",
106+
"//gonb:%exec f -x=13"
86107
]
87108
}
88109
],
@@ -99,7 +120,7 @@
99120
"name": "go",
100121
"nbconvert_exporter": "",
101122
"pygments_lexer": "",
102-
"version": "go1.25.3"
123+
"version": "go1.25.4"
103124
}
104125
},
105126
"nbformat": 4,

internal/goexec/composer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ func (s *State) createGoFileFromLines(filePath string, cellId int, lines []strin
380380
var needsClosingMain bool
381381
for ii, line := range lines {
382382
trimmedLine := TrimGonbCommentPrefix(line)
383-
if strings.HasPrefix(trimmedLine, "%main") || trimmedLine == "%%" {
383+
if strings.HasPrefix(trimmedLine, "%main") || (strings.HasPrefix(trimmedLine, "%%") && (len(trimmedLine) == 2 || trimmedLine[2] == ' ' || trimmedLine[2] == '\t')) {
384384
// Write preamble of func main() and associate to the "%%" line:
385385
fileToCellLines[w.Line] = ii
386386
fileToCellLines[w.Line+1] = ii

internal/nbtests/nbtests_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ package nbtests
1212
import (
1313
"flag"
1414
"fmt"
15+
"os"
16+
"os/exec"
17+
"path"
18+
"strings"
19+
"testing"
20+
1521
"github.com/janpfeifer/gonb/common"
1622
"github.com/janpfeifer/gonb/internal/goexec"
1723
"github.com/janpfeifer/gonb/internal/kernel"
1824
"github.com/janpfeifer/must"
1925
"github.com/stretchr/testify/require"
2026
"k8s.io/klog/v2"
21-
"os"
22-
"os/exec"
23-
"path"
24-
"strings"
25-
"testing"
2627
)
2728

2829
var panicf = common.Panicf
@@ -227,7 +228,11 @@ func TestHello(t *testing.T) {
227228
Separator),
228229
Match(OutputLine(4),
229230
Separator,
230-
"%exec worked for x=17",
231+
"Got x=17",
232+
Separator),
233+
Match(OutputLine(5),
234+
Separator,
235+
"Got x=13",
231236
Separator),
232237
),
233238
*flagPrintNotebook)

internal/specialcmd/cellmagic.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ func IsGoCell(firstLine string) bool {
3434
return !CellSpecialCommands.Has(parts[0])
3535
}
3636

37-
// ExecuteSpecialCell checks whether it is a special cell (see [CellSpecialCommands]), and if so it executes the special cell command.
37+
// ExecuteSpecialCell checks whether it is a special cell (see [CellSpecialCommands]), and if so it executes the
38+
// special cell command.
3839
//
3940
// It returns if this was a special cell (if true it executes it), and potentially an execution error, if one happened.
4041
func ExecuteSpecialCell(msg kernel.Message, goExec *goexec.State, lines []string) (isSpecialCell bool, err error) {

0 commit comments

Comments
 (0)