Skip to content

Commit

Permalink
Support lists of exercises in setup: true blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
georgestagg committed Sep 13, 2024
1 parent 1008d89 commit 3e7d310
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 20 deletions.
41 changes: 21 additions & 20 deletions _extensions/live/live.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,23 @@ function assertBlockExercise(type, engine, block)
end
end

function ExerciseDataBlocks(btype, block)
local ex = block.attr.exercise
if (type(ex) ~= "table") then
ex = { ex }
end

local blocks = {}
for idx, ex_id in pairs(ex) do
blocks[idx] = pandoc.RawBlock(
"html",
"<script type=\"exercise-" .. btype .. "-" .. ex_id .. "-contents\">\n" ..
json_as_b64(block) .. "\n</script>"
)
end
return blocks
end

function PyodideCodeBlock(code)
block_id = block_id + 1

Expand Down Expand Up @@ -166,21 +183,13 @@ function PyodideCodeBlock(code)
-- Supplementary execise blocks: setup, check, hint, solution
if (block.attr.setup) then
assertBlockExercise("setup", "pyodide", block)
return pandoc.RawBlock(
"html",
"<script type=\"exercise-setup-" .. block.attr.exercise .. "-contents\">\n" ..
json_as_b64(block) .. "\n</script>"
)
return ExerciseDataBlocks("setup", block)
end

if (block.attr.check) then
assertBlockExercise("check", "pyodide", block)
if live_options["grading"] then
return pandoc.RawBlock(
"html",
"<script type=\"exercise-check-" .. block.attr.exercise .. "-contents\">\n" ..
json_as_b64(block) .. "\n</script>"
)
return ExerciseDataBlocks("check", block)
else
return {}
end
Expand Down Expand Up @@ -292,21 +301,13 @@ function WebRCodeBlock(code)
-- Supplementary execise blocks: setup, check, hint, solution
if (block.attr.setup) then
assertBlockExercise("setup", "webr", block)
return pandoc.RawBlock(
"html",
"<script type=\"exercise-setup-" .. block.attr.exercise .. "-contents\">\n" ..
json_as_b64(block) .. "\n</script>"
)
return ExerciseDataBlocks("setup", block)
end

if (block.attr.check) then
assertBlockExercise("check", "webr", block)
if live_options["grading"] then
return pandoc.RawBlock(
"html",
"<script type=\"exercise-check-" .. block.attr.exercise .. "-contents\">\n" ..
json_as_b64(block) .. "\n</script>"
)
return ExerciseDataBlocks("check", block)
else
return {}
end
Expand Down
46 changes: 46 additions & 0 deletions docs/exercises/exercises.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,52 @@ baz <- 3
foo + bar + baz + ______
```

### Shared setup

A setup block may be attached to multiple exercises by providing a list of exercises for the `exercise` cell option. The code in the setup cell will be executed before every evaluation of any of the listed exercises.

#### Source

````{.markdown filename="exercise.qmd"}
```{{webr}}
#| setup: true
#| exercise:
#| - ex_a1
#| - ex_a2
var_xyz <- c(1, 3, 7, 9, 13, 15)
```

```{{webr}}
#| exercise: ex_a1
var_xyz * 2
```

```{{webr}}
#| exercise: ex_a2
var_xyz ** 2
```
````

#### Output

```{webr}
#| setup: true
#| exercise:
#| - ex_a1
#| - ex_a2
var_xyz <- c(1, 3, 7, 9, 13, 15)
```

```{webr}
#| exercise: ex_a1
var_xyz * 2
```

```{webr}
#| exercise: ex_a2
var_xyz ** 2
```

## Adding hints and solutions

Add hints and solutions to your exercise in the form of fenced blocks. The blocks should set the `.hint` or `.solution` class. Then, link the blocks to your exercise by setting the `exercise` attribute.
Expand Down

0 comments on commit 3e7d310

Please sign in to comment.