npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gift-pegjs

v1.0.2

Published

Parser of GIFT question format based on PEG.js

Downloads

1,208

Readme

GIFT-grammar-PEG.js

Status

Node.js CI

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:

  • Install node.js

  • 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 file foo.json should exist (expected output) and can be easily generated from the output at https://peggyjs.org/online (you have to paste the GIFT.pegjs file into the grammar on the left first). Note: any undefined values from that output must be declared as null 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|

Source for above table

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.

UML class diagram (domain model) loosely based on XML structure

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