gift-pegjs
v1.0.2
Published
Parser of GIFT question format based on PEG.js
Downloads
1,208
Readme
GIFT-grammar-PEG.js
Status
Development of PEG grammar to parse GIFT (quiz question) file format. The goal is to find an intuitive and fun way to create quiz questions.
Initial hacking done using peggyjs.org/online. The GIFT.pegjs file goes on the left and the test GIFT goes on the right. Note that nothing is saved in this environment (you must copy-paste back to your own files).
GIFT parser (npm package gift-pegjs)
The PEG.js grammar in this repo is used to generate a parser for GIFT. You can see how it works in this online editor.
Software using the parser
- GIFT Format Extension For Visual Studio Code by Ethan Ou.
- Validate GIFT questions with this online GIFT editor (generated on this repo via GitHub pages from the /docs directory).
- Create quizzes in Google Forms using the GIFT Quiz Editor (Google Forms add-on). It's possible to export questions from Moodle in GIFT format and re-use them in Google Forms, or you can create your own questions in GIFT to save clicking in the Google interface. Google Quizzes are not able to support all GIFT question types, however.
For developers
Install
For simple web applications, copy the pegjs-gift.js
file into your project.
For modern Javascript development, install the library using npm:
npm install gift-pegjs
Usage
Importing the NPM library in ES6+ Javascript (recommended):
import { parse } from "gift-pegjs";
const question = "What is the value of pi (to 3 decimal places)? {#3.141..3.142}."
const quiz = parse(question)
Importing the library for simple web applications:
import { parse } from "./pegjs-gift"; // Change path depending on where the file is copied.
const question = "What is the value of pi (to 3 decimal places)? {#3.141..3.142}."
const quiz = parse(question)
Importing the NPM library in CommonJS (for use in Node.js applications):
const parse = require("gift-pegjs");
const question = "What is the value of pi (to 3 decimal places)? {#3.141..3.142}."
const quiz = parse(question)
Using the optional Typescript types:
import { parse, GIFTQuestion } from "gift-pegjs";
const question: string = "What is the value of pi (to 3 decimal places)? {#3.141..3.142}."
const quiz: GIFTQuestion[] = parse(question)
Example
import { parse } from "gift-pegjs";
let sampleQuestion = `
Is this True? {T}
Mahatma Gandhi's birthday is an Indian holiday on {
~15th
~3rd
=2nd
} of October.
Since {
~495 AD
=1066 AD
~1215 AD
~ 43 AD
} the town of Hastings England has been "famous with visitors".
What is the value of pi (to 3 decimal places)? {#3.141..3.142}.
`;
const quizQuestions = parse(sampleQuestion);
console.log(quizQuestions);
Tests
There are easily extendible tests from sample GIFT/JSON files in ./test/questions/
.
To be able to run the tests, you must set up your environment:
Clone this repo.
Install the node.js dependencies for this project. Inside the root directory of this repo, type:
npm install .
Run the regression tests with the following command:
npm test
Output will look something like this:
> [email protected] test > jest --colors PASS tests/all-questions-harness.js GIFT question types √ parses GIFT (./tests/questions/): category1.gift (4 ms) √ parses GIFT (./tests/questions/): categorySpecialCharacter.gift (1 ms) √ parses GIFT (./tests/questions/): description1.gift (2 ms) √ parses GIFT (./tests/questions/): descriptionTitleEscapedColon.gift (1 ms) √ parses GIFT (./tests/questions/): descriptionWithID.gift (1 ms) ... √ parses GIFT (./tests/questions/): type_inferred_multiline_markdown.gift Test Suites: 1 passed, 1 total Tests: 51 passed, 51 total Snapshots: 0 total Time: 2.415 s, estimated 3 s Ran all test suites.
To create a new test GIFT file, just name it
foo.gift
in./test/questions/
. The corresponding JSON filefoo.json
should exist (expected output) and can be easily generated from the output at https://peggyjs.org/online (you have to paste theGIFT.pegjs
file into the grammar on the left first). Note: anyundefined
values from that output must be declared asnull
in the JSON file.
GIFT format
General Import Format Technology (GIFT) is described at several places, but it's hard to know what the definitive source is.
This table is not sufficient to understand everything. Please see Moodle's documentation and this reference for many more details.
| Symbols | Use |
| ------- | ----- |
| //
text | Comment until end of line (optional) |
| ::
title::
| Question title (optional) |
| text | Question text (becomes title if no title specified)|
| [
format]
| The format of the following bit of text. Options are [html]
, [moodle]
, [plain]
and [markdown]
. The default is [moodle]
for the question text, other parts of the question default to the format used for the question text. |
| {
| Start answer(s) -- without any answers, text is a description of following questions |
| {T}
or {F}
| True or False answer; also {TRUE}
and {FALSE}
|
| {
... =
right ... }
| Correct answer for multiple choice, (multiple answer? -- see page comments) or fill-in-the-blank|
| {
... ~
wrong ... }
| Incorrect answer for multiple choice or multiple answer|
| {
... =
item ->
match ... }
| Answer for matching questions|
| #
feedback text | Answer feedback for preceding multiple, fill-in-the-blank, or numeric answers|
| ####
general feedback | General feedback|
| {#
| Start numeric answer(s)|
| answer:
tolerance | Numeric answer accepted within ± tolerance range|
| low..
high | Lower and upper range values of accepted numeric answer|
| =%
n%
answer:
tolerance | n percent credit for one of multiple numeric ranges within tolerance from answer|
| }
| End answer(s)|
| \
character | Backslash escapes the special meaning of ~
, =
, #
, {
, }
, and :
|
| \n
| Places a newline in question text -- blank lines delimit questions|
Parser in PHP: https://github.com/moodle/moodle/blob/master/question/format/gift/format.php
Background information
See these discussions in the Moodle forums:
See this test file from Moodle's code base.
Class model for Moodle questions (quiz) format
The following UML class diagram is an interpretation of the XML format for Moodle quiz import/export. Note that the XML format is naive, e.g., the QUESTION
tag is overloaded using TYPE=
and as such can't easily be schema-tized. The interpretation below aims to facilitate the understanding of the content and relations. It's a domain model reverse-engineered from a data model.
Railroad diagram of the grammar
Check out the railroad diagram for this project's PEG.
Testing the editor locally
- In a terminal of the cloned repo:
npm run build && node server.js
- Point a browser to http://localhost:8181/docs/editor/editor.html