8

Is there a way to use MarkDown's fixed-width code blocks in a quiz question?

I'm writing my quiz questions in my text editor in the "GIFT" format and then importing them to my quiz pool. The GIFT format is very quick and compact, and this method lets me develop questions without internet access.

However, I'm struggling with using the MarkDown text format in the quiz questions. The docs say it's supported.

The problem is that the GIFT format doesn't allow newlines in the middle of a question, but MarkDown relies on newlines to delineate code blocks. For example, I would like the following quiz question:

::Finding bugs:: [markdown] Consider the following code: x=5; if x=5 disp("It worked!") else print('Uh oh!') done On which lines do bugs occur? Answer in the form [1,2,3]. { =[2,3,5,6] } 

This doesn't import properly because the GIFT format prohibits newlines within a quiz question. But I can't remove the newlines because then MarkDown won't recognize the code block.

Thoughts? Thanks!

1
  • In GIFT, a blank line is a separator of questions, whereas in Markdown it has other meanings. So, you have to escape those blank lines... Commented Jun 13, 2020 at 16:20

2 Answers 2

4

(Answering my own question) This seems to work:

::Finding bugs:: [markdown] Consider the following code\:\n \n x\=5; \n if x\=5 \n disp("It worked!") \n else \n print('Uh oh!') \n done \n\n\n On which lines do bugs occur? Answer in the form [1,2,3]. { =[2,3,5,6] } 

Note the four spaces between each \n and the code. Ugh.

Sign up to request clarification or add additional context in comments.

Comments

1

My Moodle site (v.311) displays the answer from ConvexMartian with extra line breaks between each line:

enter image description here

To get rid of those spurious line breaks (Moodle now seems to interpret both the \n and the line breaks), you can put the code all on one line (which is pretty ugly!), e.g.:

::Finding bugs:: [markdown] Consider the following code\:\n\n x\=5;\n if x\=5\n disp("It worked!")\n else\n print('Uh oh!')\n done\n\n\n On which lines do bugs occur? Answer in the form [1,2,3]. { =[2,3,5,6] } 

The result:

enter image description here

Non-breaking space option with code block

This answer on a Moodle forum mentions you can use non-breaking spaces (instead of indenting) with code block (fence) in markdown. Your problem would look like:

::Finding bugs:: [markdown] Consider the following code: ``` x=5; if x=5 . disp("It worked!") else . print('Uh oh!') done ``` On which lines do bugs occur? Answer in the form [1,2,3]. { =[2,3,5,6] } 

However, the . displayed on lines above (those that are indented) must be a non-breaking space (or indentation will not be respected).

Putting code in answers

It's possible to put code blocks in answers, too (again, using the non-breaking space to preserve indentation).

In this example, GIFT control characters ({ } = : ~) appearing in the source code have to be escaped. Because of that, the source code looks pretty ugly, too (but it works):

::Code blocks in stem and answers :: [markdown] Complete the following TypeScript class so it has a method named `go` that accepts a string argument and always returns true. ``` export class A \{ . // complete \} ``` { = ``` go(a\: string) \{ . return true; \} ``` ~ ``` go(string a) \{ . return true; \} ``` } 

Result:

enter image description here

There are a few smart editors supporting GIFT that will help you escape the special characters in the code. I prefer this VSCode extension for GIFT. Disclaimer: it uses the GIFT grammar/parser I helped develop.

9 Comments

How did it solve the problem that inline parkdown (e.g. bla bla foo bla bla) is displayed with foo on a separate line instead of inline ?
@Antonello I'm not sure I understand your comment, but newlines are ok in GIFT, as long as there are not two in a row (blank line), which is a separator for questions.
if in a GIFT-formatted quiz you use the markdown formatting for a question (or an answer) and then you use the backticks to surround some text, you will see that Moodle will import it by giving the text you have surrounded on its own separate line, rather than keeping it in the same line as the rest of the text as all Markdown implementations do..
Three backticks are a code fence. It's different than inline code that doesn't get line breaks. Is that your question @antonello
@Antonello In the examples on that blog page, there is one case with the word go inside of single backticks and there is no line break. Maybe the problem is with your version of Moodle? It could even be in the style sheet.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.