Skip to content

Commit 5023f19

Browse files
committed
add @onComplete
1 parent da83847 commit 5023f19

File tree

11 files changed

+109
-73
lines changed

11 files changed

+109
-73
lines changed

lib/build/parser/actions.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
"use strict";
22
var cleanup_1 = require('./cleanup');
33
var Match = require('./match');
4-
function trimCommandValue(text) {
5-
var value = text.substring(text.indexOf('(') + 1).slice(0, -1);
6-
var command = {
7-
action: text.substring(0, text.indexOf('(')),
8-
value: cleanup_1.trimLeadingSpaces(cleanup_1.trimQuotes(value))
9-
};
10-
return command.action + '(\'' + command.value + '\')';
11-
}
12-
exports.trimCommandValue = trimCommandValue;
134
function doAction(type, isArray, actionValue, result, line, index) {
145
if (result.chapters[index.chapter].pages[index.page].tasks[index.task][type] === undefined) {
156
result.chapters[index.chapter].pages[index.page].tasks[index.task][type] = [];
@@ -48,12 +39,12 @@ function addToTasks(result, line, index) {
4839
if (!!isActionArray) {
4940
var arrayOfActions = JSON.parse(isActionArray);
5041
arrayOfActions.forEach(function (value) {
51-
value = trimCommandValue(cleanup_1.trimQuotes(value.trim()));
42+
value = cleanup_1.trimCommandValue(cleanup_1.trimQuotes(value.trim()));
5243
result.chapters[index.chapter].pages[index.page].tasks[index.task].actions.push(value);
5344
});
5445
}
5546
else {
56-
var value = trimCommandValue(actionValue);
47+
var value = cleanup_1.trimCommandValue(actionValue);
5748
result.chapters[index.chapter].pages[index.page].tasks[index.task].actions.push(value);
5849
}
5950
return result;

lib/build/parser/cleanup.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ function trimLeadingSpaces(text) {
5050
}
5151
}
5252
exports.trimLeadingSpaces = trimLeadingSpaces;
53+
function trimCommandValue(text) {
54+
var value = text.substring(text.indexOf('(') + 1).slice(0, -1);
55+
var command = {
56+
action: text.substring(0, text.indexOf('(')),
57+
value: trimLeadingSpaces(trimQuotes(value))
58+
};
59+
return command.action + '(\'' + command.value + '\')';
60+
}
61+
exports.trimCommandValue = trimCommandValue;
62+
function trimValue(text) {
63+
var value = text.substring(text.indexOf('(') + 1).slice(0, -1);
64+
return trimLeadingSpaces(trimQuotes(value, true));
65+
}
66+
exports.trimValue = trimValue;
5367
function cleanup(result) {
5468
result = JSON.parse(JSON.stringify(result));
5569
if (result.project.description) {

lib/build/parser/match.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var regex = {
1111
'```': match('`', 3),
1212
'action': /^@(action|test|hint|continue)/,
1313
'import': /^@import\((.+)\)$/,
14-
'onComplete': /^@onComplete\((.+)\)$/
14+
'onComplete': /^(@onComplete.+)/
1515
};
1616
function parseWithCode(code) {
1717
return function (line) {

lib/build/parser/page.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ function page(result, lines, index) {
2222
case !!Match.isImport(line):
2323
lines = import_1.loadImport(lines, Match.isImport(line));
2424
continue;
25+
case (!!Match.isComplete(line) || !!currentComplete):
26+
currentComplete = !!currentComplete ? currentComplete += '\n' + line : Match.isComplete(line);
27+
bracketCount = cleanup_1.bracketTracker(currentComplete);
28+
if (bracketCount === 0) {
29+
result.chapters[index.chapter].pages[index.page].onComplete = cleanup_1.trimValue(currentComplete);
30+
currentComplete = null;
31+
}
32+
continue;
2533
case !!Match.codeBlock(line):
2634
if (line.length > 3) {
2735
result.chapters[index.chapter].pages[index.page].description += '\n' + line;
@@ -32,14 +40,6 @@ function page(result, lines, index) {
3240
continue;
3341
case inCodeBlock:
3442
continue;
35-
case !!Match.isComplete(line) || !!currentComplete:
36-
currentComplete = currentComplete ? currentComplete += line : Match.isComplete(line);
37-
bracketCount = cleanup_1.bracketTracker(line);
38-
if (bracketCount === 0) {
39-
result.chapters[index.chapter].pages[index.page].onComplete = currentComplete;
40-
currentComplete = null;
41-
}
42-
continue;
4343
case !!Match.chapter(line):
4444
return chapter_1.chapter(result, lines.slice(i), index);
4545
case !!Match.page(line):

src/build/parser/actions.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
import {trimQuotes, trimLeadingSpaces} from './cleanup';
1+
import {trimQuotes, trimCommandValue} from './cleanup';
22
import * as Match from './match';
33

4-
export function trimCommandValue(text: string): string {
5-
let value = text.substring(text.indexOf('(') + 1).slice(0, -1);
6-
let command = {
7-
action: text.substring(0, text.indexOf('(')),
8-
value: trimLeadingSpaces(trimQuotes(value))
9-
};
10-
return command.action + '(\'' + command.value + '\')';
11-
}
12-
134
function doAction(type: CR.OutputAction, isArray, actionValue, result, line, index) {
145
// set to array
156
if (result.chapters[index.chapter].pages[index.page].tasks[index.task][type] === undefined) {

src/build/parser/cleanup.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ export function trimLeadingSpaces(text: string): string {
4747
}
4848
}
4949

50+
export function trimCommandValue(text: string): string {
51+
let value = text.substring(text.indexOf('(') + 1).slice(0, -1);
52+
let command = {
53+
action: text.substring(0, text.indexOf('(')),
54+
value: trimLeadingSpaces(trimQuotes(value))
55+
};
56+
return command.action + '(\'' + command.value + '\')';
57+
}
58+
59+
export function trimValue(text: string): string {
60+
let value = text.substring(text.indexOf('(') + 1).slice(0, -1);
61+
return trimLeadingSpaces(trimQuotes(value, true));
62+
}
63+
5064
export function cleanup(result: CR.Output): string {
5165
result = JSON.parse(JSON.stringify(result));
5266
if (result.project.description) {

src/build/parser/match.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var regex = {
1212
'```': match('`', 3),
1313
'action': /^@(action|test|hint|continue)/,
1414
'import': /^@import\((.+)\)$/,
15-
'onComplete': /^@onComplete\((.+)\)$/
15+
'onComplete': /^(@onComplete.+)/
1616
};
1717

1818
function parseWithCode(code: string) {

src/build/parser/page.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Match from './match';
22
import {chapter} from './chapter';
33
import {task} from './task';
44
import {loadImport} from './import';
5-
import {bracketTracker} from './cleanup';
5+
import {bracketTracker, trimValue} from './cleanup';
66

77
export function page(result: CR.Output, lines: string[], index: CR.Index): CR.Output {
88
index.page += 1;
@@ -27,6 +27,17 @@ export function page(result: CR.Output, lines: string[], index: CR.Index): CR.Ou
2727
lines = loadImport(lines, Match.isImport(line));
2828
continue;
2929

30+
// @onComplete
31+
case (!!Match.isComplete(line) || !!currentComplete):
32+
currentComplete = !!currentComplete ? currentComplete += '\n' + line : Match.isComplete(line);
33+
bracketCount = bracketTracker(currentComplete);
34+
// complete
35+
if (bracketCount === 0) {
36+
result.chapters[index.chapter].pages[index.page].onComplete = trimValue(currentComplete);
37+
currentComplete = null;
38+
}
39+
continue;
40+
3041
// ``` `
3142
case !!Match.codeBlock(line):
3243
if (line.length > 3) {
@@ -38,16 +49,6 @@ export function page(result: CR.Output, lines: string[], index: CR.Index): CR.Ou
3849
case inCodeBlock:
3950
continue;
4051

41-
// @onComplete
42-
case !!Match.isComplete(line) || !!currentComplete:
43-
currentComplete = currentComplete ? currentComplete += line : Match.isComplete(line);
44-
bracketCount = bracketTracker(line);
45-
// complete
46-
if (bracketCount === 0) {
47-
result.chapters[index.chapter].pages[index.page].onComplete = currentComplete;
48-
currentComplete = null;
49-
}
50-
continue;
5152

5253
// ##
5354
case !!Match.chapter(line):

src/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ if (!program.args.length &&
2929
if (program.build) {
3030
const tutorial = program.args[0] || 'tutorial/tutorial.md';
3131
const output = 'coderoad.json';
32-
console.log(chalk.grey(`building from ${tutorial}...`))
32+
console.log(chalk.grey(`building from ${tutorial}...`));
3333
build(tutorial, output);
34-
console.log(chalk.grey(`build complete: coderoad.json`))
34+
console.log(chalk.grey(`build complete: coderoad.json`));
3535
}
3636

3737
// create

test/build-onComplete.spec.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('@onComplete', function() {
2929
};
3030
};
3131

32-
it('should add a @onComplete string to the page', function() {
32+
it('should add @onComplete string to the page', function() {
3333
var lines = ['### Page One', 'page description', 'more page description', '@onComplete("next page")'];
3434
var next = page(result(), lines, index());
3535
var nextPage = next.chapters[0].pages[0];
@@ -38,12 +38,36 @@ describe('@onComplete', function() {
3838
description: 'page description\nmore page description',
3939
onComplete: 'next page'
4040
});
41+
});
42+
43+
it('should handle multi-line codeblocks', function() {
44+
var lines = ['### Page One', 'page description', 'more page description',
45+
'@onComplete("next page', '```', 'var a = 42;', '```', '")'];
46+
var next = page(result(), lines, index());
47+
var nextPage = next.chapters[0].pages[0];
48+
expect(nextPage).to.deep.equal({
49+
title: 'Page One',
50+
description: 'page description\nmore page description',
51+
onComplete: 'next page\n```\nvar a = 42;\n```'
52+
});
53+
});
4154

42-
it('shouldn\'t add a @onComplete string to a task', function() {
43-
var lines = ['+ Task One', 'with more on the next', 'line', '@onComplete("next page")'];
44-
var next = task(resultTask(), lines, index());
45-
var nextTask = next.chapters[0].pages[0].tasks[0];
46-
expect(nextTask.description).to.equal('Task One\nwith more on the next\nline');
55+
it('should handle string literals', function() {
56+
var lines = ['### Page One', 'page description', 'more page description',
57+
'@onComplete("next page', '`var a = 42;`', '")'];
58+
var next = page(result(), lines, index());
59+
var nextPage = next.chapters[0].pages[0];
60+
expect(nextPage).to.deep.equal({
61+
title: 'Page One',
62+
description: 'page description\nmore page description',
63+
onComplete: 'next page\n`var a = 42;`'
64+
});
4765
});
66+
67+
xit('shouldn\'t add @onComplete string to a task', function() {
68+
var lines = ['+ Task One', 'with more on the next', 'line', '@onComplete("next page")'];
69+
var next = task(resultTask(), lines, index());
70+
var nextTask = next.chapters[0].pages[0].tasks[0];
71+
expect(nextTask.description).to.equal('Task One\nwith more on the next\nline');
4872
});
4973
});

test/build-task.spec.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var expect = require('chai').expect;
22
var task = require('../lib/build/parser/task').task;
3-
var trim = require('../lib/build/parser/actions').trimCommandValue;
3+
var trimCommandValue = require('../lib/build/parser/cleanup').trimCommandValue;
44

55
var result = function() {
66
return {
@@ -19,14 +19,14 @@ var index = function() {
1919
};
2020
};
2121

22-
describe('trimCommandValue', function () {
22+
describe('trimCommandValue', function() {
2323

24-
it('should not effect a normal command', function () {
25-
expect(trim("do('something')")).to.equal("do('something')");
24+
it('should not effect a normal command', function() {
25+
expect(trimCommandValue("do('something')")).to.equal("do('something')");
2626
});
2727

28-
it('should trim leading spaces', function () {
29-
expect(trim("do( 'something')")).to.equal("do('something')");
28+
it('should trim leading spaces', function() {
29+
expect(trimCommandValue("do( 'something')")).to.equal("do('something')");
3030
});
3131

3232
});
@@ -77,31 +77,32 @@ describe('task', function() {
7777

7878
it('should accept multpile import lines in task', function() {
7979
var lines = ['+ Task description', '',
80-
"@import('./test/imports/chapter-sample')", "@import('./test/imports/chapter-sample')"]
80+
"@import('./test/imports/chapter-sample')", "@import('./test/imports/chapter-sample')"
81+
]
8182
var next = task(result(), lines, index());
8283
var nextChapter = next.chapters[2];
8384
expect(nextChapter.title).to.equal('Chapter Sample');
8485
});
8586

86-
it('should add codeblock languages', function() {
87-
var lines = ['+ Task One', '```js', 'var a = 42;', '```'];
88-
var next = task(result(), lines, index());
89-
var nextTask = next.chapters[0].pages[0].tasks[0];
90-
expect(nextTask.description).to.equal('Task One\n```js\nvar a = 42;\n```');
91-
});
87+
it('should add codeblock languages', function() {
88+
var lines = ['+ Task One', '```js', 'var a = 42;', '```'];
89+
var next = task(result(), lines, index());
90+
var nextTask = next.chapters[0].pages[0].tasks[0];
91+
expect(nextTask.description).to.equal('Task One\n```js\nvar a = 42;\n```');
92+
});
9293

93-
it('should add single line codeblocks', function () {
94-
var lines = ['+ Task One', '```var a = 42;```'];
94+
it('should add single line codeblocks', function() {
95+
var lines = ['+ Task One', '```var a = 42;```'];
9596
var next = task(result(), lines, index());
9697
var nextTask = next.chapters[0].pages[0].tasks[0];
9798
expect(nextTask.description).to.equal('Task One\n```var a = 42;```');
98-
});
99-
100-
it('should handle strangely formatted codeblocks', function () {
101-
var lines = ['+ Task One', '```var a = 42;', '```'];
102-
var next = task(result(), lines, index());
103-
var nextTask = next.chapters[0].pages[0].tasks[0];
104-
expect(nextTask.description).to.equal('Task One\n```var a = 42;\n```');
105-
});
99+
});
100+
101+
it('should handle strangely formatted codeblocks', function() {
102+
var lines = ['+ Task One', '```var a = 42;', '```'];
103+
var next = task(result(), lines, index());
104+
var nextTask = next.chapters[0].pages[0].tasks[0];
105+
expect(nextTask.description).to.equal('Task One\n```var a = 42;\n```');
106+
});
106107

107108
}); // task

0 commit comments

Comments
 (0)