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

[WIP] Tests #14

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
10 changes: 5 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ jobs:
steps:
- checkout
- restore_cache:
key: dependency-cache-{{ checksum "node/package.json" }}
key: dependency-cache-{{ checksum "package.json" }}
- run:
name: install npm
command: cd node && npm install
command: npm install
- save_cache:
key: dependency-cache-{{ checksum "node/package.json" }}
key: dependency-cache-{{ checksum "package.json" }}
paths:
- node/node_modules
- node_modules
# run tests!
- run:
name: test npm
command: echo 'we need tests'
command: npm test
198 changes: 198 additions & 0 deletions lib/game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
const yaml = require('js-yaml');
const Content = require('./content');
const { removeEmptyObjectProperties, getMarkdownLink } = require('./helpers');
// for reading Games from a file, and also for writing to files.
// Not meant to be the main parser, just meant to provide an abstraction for dealing with lots of curriculum.

const extractSection = ({ rawText, sectionTitle }) => {
let startIndex = rawText.indexOf(sectionTitle) + sectionTitle.length + 1;

if (startIndex === sectionTitle.length) {
return null;
}
let endIndex = rawText.indexOf('---', startIndex) === -1 ? rawText.length : rawText.indexOf('---', startIndex);
let tempcontent = rawText.substring(startIndex, endIndex).trim();

return tempcontent;
};

/**
* Takes Games markdown and returns array of questions
* @param {string} rawText
* @returns {object}
*/
const parseGames = ({rawText, type}) => {
switch(type) {
case('fillTheGap'):
return (() => {
let startIndex = rawText.indexOf("Game Content") + "Game Content".length + 1;
let questionsArr = rawText.substring(startIndex).split('---').map(x => x.trim());
let output = [];
questionsArr.map(question => {
let data = {};
data.question = question.split('```')[1].trim();
let answerSubStr = question.split('```')[2].trim();
data.answers = answerSubStr
.substring(0,answerSubStr.indexOf("%exp"))
.trim()
.split(/\n*\* /).slice(1);

if (question.indexOf("%exp") != 1) data.explaination = question.substring(question.lastIndexOf("%exp")+4, question.lastIndexOf("%")).trim();

output.push(data);
})
return output;
})()
break;
case('tetris'):
return (() => {
let startIndex = rawText.indexOf("Game Content") + "Game Content".length + 1;
let gameString = rawText.substring(startIndex).trim();
let output = {answers: []};
[output.left, output.right ] = gameString.split("\n")[0].split(":").map(x => {return x.trim()});


let falseSet = gameString.substring(gameString.indexOf("```false\n")+8, gameString.indexOf("```\n")).trim().split("%\n");
for (let question of falseSet) {
let data = {};
data.value = false;
[data.text, data.explaination] = question.split("%exp").map(x => {return x.trim()});
output.answers.push(data);
}
var tempStr = gameString.substring(gameString.indexOf("```true\n")+7);
let trueSet = tempStr.substring(0, gameString.lastIndexOf("```")).trim().split("%\n");
for (let question of trueSet) {
let data = {};
data.value = true;
[data.text, data.explaination] = question.split("%exp").map(x => {return x.trim()});
output.answers.push(data);
}

return output;
})()
break;
default:
throw new Error(`Game type ${type} not found`);
break;
}
}

/**
* Given a game string and its path, creates Game object
* @param {string} content
* @param {string} filepath
* @class
* @extends Content
*/
module.exports = class Game extends Content {
constructor({ body, path }) {
// Should take an object
// Content reader and writer should combine into ReaderWriter and live in curriculum
// Each one of the contentType files (Course/Game ...) should take in the text and path
/**
new Content({body: text, path: path})
**/
super({ body, path });

this.parent = null;
this.links = null;
this.parse(this.rawText);
}

parse(text) {
this.title = text.substring(2, text.indexOf('\n'));
this.content = extractSection({
rawText: text,
sectionTitle: '## Content'
});
yaml.safeLoadAll(text.split('---')[0], doc => {
for (const prop in doc) {
this[prop] = doc[prop];
}
});
this.game = parseGames({
rawText: text,
type: this.type
})

if (this.links) {
this.links = this.links.map(getMarkdownLink);
}
}

/**
* Generates markdown based on current object properties
* @returns {string} Markdown
*/
render() {
let markdown = '';

// Title
markdown += `# ${this.title}\n`;
// Author
if (this.author != null) {
markdown += `author: ${this.author}\n\n`;
}
// Levels
if (this.levels != null) {
markdown += 'levels:\n\n';
for (let i in this.levels) {
markdown += ` - ${this.levels[i]}\n\n`;
}
}

// Type with fallback
markdown += `type: ${this.type || 'normal'}\n\n`;
// Category with fallback
markdown += `category: ${this.category || 'must-know'}\n\n`;
// Parent
if (this.parent != null) markdown += `parent: ${this.parent}\n\n`;
// Standards
if (this.standards != null) {
markdown += 'standards:\n\n';
for (let i in this.standards) {
markdown += ` - ${i}: ${this.standards[i]}\n\n`;
}
}
// Tags
if (this.tags != null) {
markdown += 'tags:\n\n';
for (let i in this.tags) {
markdown += ` - ${this.tags[i]}\n\n`;
}
}

// Links
if (this.links != null && this.links.length > 0) {
markdown += 'links:\n\n';
for (let i in this.links) {
markdown += ` - >-\n ${this.links[i]}\n\n`;
}
}

// Content
markdown += `---\n## Content\n\n${this.content}\n\n`;


// return new game file text
return markdown;
}

toJSON() {
return removeEmptyObjectProperties({
slug: this.slug,
headline: this.title,
author: this.author,
category: this.category,
type: this.type,
tags: this.tags,
inAlgoPool: this.inAlgoPool,
stub: this.stub,
levels: this.levels,
links: this.links,
content: this.content,
gameContent: this.gameContent,
footnotes: this.footnotes
});
}
};
6 changes: 4 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ const Course = require('./course');
const Standard = require('./standard');
const Workout = require('./workout');
const Insight = require('./insight');
const Game = require('./game.js');
const Networking = require('./networking/github');

module.exports = {
Networking,
ContentReaderWriter,
...helpers,
Curriculum,
Topic,
Course,
Standard,
Workout,
Insight,
...helpers
Game,
Networking
};

42 changes: 29 additions & 13 deletions lib/insight.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,38 @@ module.exports = class Insight extends Content {
new Content({body: text, path: path})
**/
super({ body, path });
// Obligatory Getting-rid-of-Windows-line-ending.
this.rawText = this.rawText
.replace(/\r\n/g, '\n')
.replace(/\n *\* */g, '\n* ');
// Sometimes, answer lists are malformed. Ensure that list is propely formatted.
// Sometimes, answer lists are malformed. Ensure that list is propely formatted.
.replace(/\n\s*\*\s*/g, '\n* ');

this.parent = null;
this.links = null;
this.practiceQuestion = null;
this.revisionQuestion = null;
this.quizQuestion = null;
this.footnotes = null;
this.parse(this.rawText);
}

parse(text) {
this.title = text.substring(2, text.indexOf('\n'));
this.content = extractSection({
rawText: text,
sectionTitle: '## Content'
});
this.content = extractSection({ rawText: text, sectionTitle: '## Content' });
this.practiceQuestion = parseQuestion(
extractSection({ rawText: text, sectionTitle: '## Practice' })
);
this.revisionQuestion = parseQuestion(
extractSection({ rawText: text, sectionTitle: '## Revision' })
);
this.quizQuestion = yaml.safeLoad(
let tempQuizData = yaml.safeLoad(
extractSection({ rawText: text, sectionTitle: '## Quiz' })
);
for (let key in tempQuizData) {
if (typeof tempQuizData[key] === "string") tempQuizData[key] = tempQuizData[key].trim();
}
this.quizQuestion = tempQuizData;

if (this.quizQuestion != null) this.quizQuestion.rawText = extractSection({ rawText: text, sectionTitle: '## Quiz' });

this.footnotes = extractSection({
rawText: text,
sectionTitle: '## Footnotes'
Expand All @@ -95,6 +104,7 @@ module.exports = class Insight extends Content {
if (this.links) {
this.links = this.links.map(getMarkdownLink);
}

}

/**
Expand Down Expand Up @@ -122,6 +132,8 @@ module.exports = class Insight extends Content {
markdown += `type: ${this.type || 'normal'}\n\n`;
// Category with fallback
markdown += `category: ${this.category || 'must-know'}\n\n`;
// Parent
if (this.parent != null) markdown += `parent: ${this.parent}\n\n`;
// Standards
if (this.standards != null) {
markdown += 'standards:\n\n';
Expand All @@ -141,7 +153,7 @@ module.exports = class Insight extends Content {
if (this.links != null && this.links.length > 0) {
markdown += 'links:\n\n';
for (let i in this.links) {
markdown += ` - >-\n ${this.links[i]}\n\n`;
markdown += ` - '[${this.links[i].name}](${this.links[i].url})'\n\n`;
}
}

Expand All @@ -168,7 +180,11 @@ module.exports = class Insight extends Content {

if (this.quizQuestion != null) {
markdown += `---\n## Quiz\n\nheadline: ${this.quizQuestion.headline}\n
question: ${this.quizQuestion.question}\n\nanswers:\n`;
question: |
${this.quizQuestion.question}

answers:
\n`;
for (let i in this.quizQuestion.answers) {
markdown += ` - ${this.quizQuestion.answers[i]}\n`;
}
Expand All @@ -193,8 +209,8 @@ question: ${this.quizQuestion.question}\n\nanswers:\n`;
links: this.links,
content: this.content,
gameContent: this.gameContent,
practiceQuestion: this.practiceQuestion.rawText,
reviseQuestion: this.revisionQuestion.rawText,
practiceQuestion: { text: this.practiceQuestion.text, answers: this.practiceQuestion.answers},
reviseQuestion: { text: this.revisionQuestion.text, answers: this.revisionQuestion.answers},
quiz: this.quizQuestion,
footnotes: this.footnotes
});
Expand Down
Loading