Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi lang support #26

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Contributing to mdbook-quiz

Thanks for your interest in contributing to mdbook-quiz! To get started, you'll want to install [cargo-make](https://github.com/sagiegurari/cargo-make) and [pnpm](https://pnpm.io/installation). Then from the root of the repository, run:
Thanks for your interest in contributing to mdbook-quiz! To get started, you'll want to install, [graco](https://github.com/willcrichton/graco), [cargo-watch](https://github.com/watchexec/cargo-watch.git), [cargo-make](https://github.com/sagiegurari/cargo-make) and [pnpm](https://pnpm.io/installation). Then from the root of the repository, run:

```
cargo make watch
Expand Down
40 changes: 39 additions & 1 deletion example/src/quiz.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
[[questions]]
type = "MultipleChoice"
prompt.prompt = "What does it mean if a variable `x` is immutable?"
prompt.prompt = """
What does it mean if a variable `x` is immutable?

Rust:
```rust
let x = 0;
```

C++:
```cpp
const int x = 0;
```

Java:
```java
final int x = 0;
```
"""
answer.answer = "`x` cannot be changed after being assigned to a value."
prompt.distractors = [
"`x` is stored in the immutable region of memory.",
Expand All @@ -21,6 +38,7 @@ For example, you can make a mutable variable `x` by writing: `let mut x = 1`.

[[questions]]
type = "Tracing"
prompt.language="rust"
prompt.program = """
fn main() {
let x = 1;
Expand All @@ -33,4 +51,24 @@ answer.doesCompile = false
answer.lineNumber = 4
context = """
This is a compiler error because line 4 tries to mutate `x` when `x` is not marked as `mut`.
"""

[[questions]]
type = "Tracing"
prompt.language="cpp"
prompt.program = """

#include <iostream>

int main() {
const int x = 1;
std::cout << x << std::endl;
x += 1;
std::cout << x << std::endl;
}
"""
answer.doesCompile = false
answer.lineNumber = 6
context = """
This is a compiler error because line 6 tries to mutate `x` when `x` is marked as `const`.
"""
2 changes: 2 additions & 0 deletions js/packages/quiz-schema/src/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { QuestionFields } from "./common";
export interface TracingPrompt {
/** The contents of the program to trace */
program: string;

language?: string;
}

export interface TracingAnswer {
Expand Down
6 changes: 1 addition & 5 deletions js/packages/quiz/src/components/snippet.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import * as rustEditor from "@wcrichto/rust-editor";
import type { monaco } from "@wcrichto/rust-editor";
//@ts-ignore
import hljs from "highlight.js/lib/core";
//@ts-ignore
import rust from "highlight.js/lib/languages/rust";
import hljs from "highlight.js";
//@ts-ignore
import lean from "highlightjs-lean";
import React, { useContext, useEffect, useRef } from "react";
import ReactDOM from "react-dom/client";

// This reduces bundle size by not including a bunch of extra languages
hljs.registerLanguage("rust", rust);
hljs.registerLanguage("lean", lean);

export let DefaultLanguageContext = React.createContext<string | undefined>(
Expand Down
6 changes: 5 additions & 1 deletion js/packages/quiz/src/questions/tracing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ export let TracingMethods: QuestionMethods<TracingPrompt, TracingAnswer> = {
{/* If the program does not pass, indicate the last line number involved in the
compiler error. */}
</p>
<Snippet snippet={prompt.program} lineNumbers />
<Snippet
language={prompt.language}
snippet={prompt.program}
lineNumbers
/>
</>
),

Expand Down