Skip to content

Commit b0c7d86

Browse files
committed
save single onPageComplete
1 parent 0dba2fe commit b0c7d86

File tree

10 files changed

+110
-60
lines changed

10 files changed

+110
-60
lines changed

‎parser/index.pegjs

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
// final parsed output for coderoad.json file
2+
// Initial Data
3+
34
var output = {
45
info: {
56
title: 'Tutorial Title',
@@ -8,16 +9,24 @@
89
pages: []
910
};
1011

12+
// Types
13+
14+
const pageTypes = ['onPageComplete'];
15+
const taskTypes = ['tests', 'actions', 'hints']
16+
17+
// Helper Functions
18+
1119
function adjust(item) {
1220
return item[0].concat(item[1].join(''));
1321
}
22+
1423
function trim({desc, str, first, last}) {
1524
if ( str[0].match(first) && str[str.length - 1].match(last || first) ) {
1625
return str.slice(1, -1);
1726
}
18-
console.log('Error. Could not parse ' + desc + ' in ' + str);
19-
return str;
27+
throw `Error. Could not parse "${desc}" in "${str}".`;
2028
}
29+
2130
function trimBrackets(str) {
2231
return trim({
2332
desc: 'bracket',
@@ -26,13 +35,15 @@
2635
last: /\)/,
2736
});
2837
}
38+
2939
function trimQuotes(str) {
3040
return trim({
3141
desc: 'quote',
3242
str,
3343
first: /[\"\'\`]/
3444
});
3545
}
46+
3647
function trimBracketsAndQuotes(str) {
3748
return trimQuotes(trimBrackets(str));
3849
}
@@ -66,12 +77,22 @@ page
6677
= title: page_title
6778
description: description*
6879
tasks: page_task*
80+
actions: page_actions*
81+
6982
{
70-
output.pages.push({
71-
title,
83+
let page = {
84+
title,
7285
description: description.join('\n'),
73-
tasks,
86+
tasks
87+
}
88+
// map over any actions and add them
89+
actions.forEach(({type, value}) => {
90+
if (page.hasOwnProperty(type)) {
91+
throw `${type} already exists on page "${page.title}"`;
92+
}
93+
page[type] = value;
7494
});
95+
output.pages.push(page);
7596
}
7697

7798
page_title
@@ -89,14 +110,21 @@ page_task
89110
break?
90111

91112
{ let task = { description, tests: [], hints: [] };
92-
actions.forEach(({type, value}) => task[type].push(value));
113+
actions.forEach(({type, value}) => {
114+
if (taskTypes.includes(type)) {
115+
task[type].push(value);
116+
} else if (pageTypes.includes(type)) {
117+
output.pages[pages.length - 1][type] = value;
118+
}
119+
});
93120
return task;
94121
}
95122

96123
task_actions
97-
= test: task_test
98-
/ hint: task_hint
99-
/ action: task_action
124+
= task_test
125+
/ task_hint
126+
/ task_action
127+
/ page_actions
100128

101129
task_test
102130
= '@test'
@@ -123,20 +151,11 @@ on_page_complete
123151
= '@onPageComplete'
124152
'('
125153
quote
126-
content: .+
127-
quote
128-
')'
129-
{ return { type: 'onPageComplete', value: content.join('') }; }
130-
131-
page_import
132-
= '@import'
133-
'('
134-
quote
135-
filePath: file_path
154+
content: [a-zA-Z0-9 ]+
136155
quote
137156
')'
138157
break
139-
{ return filePath.join(''); }
158+
{ return { type: 'onPageComplete', value: content.join('') }; }
140159

141160
task_action
142161
= '@action'

‎parser/pegjs/data.js renamed to ‎parser/pegjs/_data.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// final parsed output for coderoad.json file
1+
// Initial Data
2+
23
var output = {
34
info: {
45
title: 'Tutorial Title',

‎parser/pegjs/functions.js renamed to ‎parser/pegjs/_functions.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Helper Functions
2+
13
function adjust(item) {
24
return item[0].concat(item[1].join(''));
35
}
@@ -6,8 +8,7 @@ function trim({desc, str, first, last}) {
68
if ( str[0].match(first) && str[str.length - 1].match(last || first) ) {
79
return str.slice(1, -1);
810
}
9-
console.log('Error. Could not parse ' + desc + ' in ' + str);
10-
return str;
11+
throw `Error. Could not parse "${desc}" in "${str}".`;
1112
}
1213

1314
function trimBrackets(str) {

‎parser/pegjs/_types.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Types
2+
3+
const pageTypes = ['onPageComplete'];
4+
const taskTypes = ['tests', 'actions', 'hints']

‎parser/pegjs/index.pegjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
2-
@import('./data.js')
3-
@import('./functions.js')
2+
@import('./_data.js')
3+
@import('./_types.js')
4+
@import('./_functions.js')
45
}
56

67
start

‎parser/pegjs/page-actions.pegjs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,8 @@ on_page_complete
55
= '@onPageComplete'
66
'('
77
quote
8-
content: .+
9-
quote
10-
')'
11-
{ return { type: 'onPageComplete', value: content.join('') }; }
12-
13-
page_import
14-
= '@import'
15-
'('
16-
quote
17-
filePath: file_path
8+
content: [a-zA-Z0-9 ]+
189
quote
1910
')'
2011
break
21-
{ return filePath.join(''); }
12+
{ return { type: 'onPageComplete', value: content.join('') }; }

‎parser/pegjs/page.pegjs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@ page
22
= title: page_title
33
description: description*
44
tasks: page_task*
5+
actions: page_actions*
6+
57
{
6-
output.pages.push({
7-
title,
8+
let page = {
9+
title,
810
description: description.join('\n'),
9-
tasks,
11+
tasks
12+
}
13+
// map over any actions and add them
14+
actions.forEach(({type, value}) => {
15+
if (page.hasOwnProperty(type)) {
16+
throw `${type} already exists on page "${page.title}"`;
17+
}
18+
page[type] = value;
1019
});
20+
output.pages.push(page);
1121
}
1222

1323
page_title
@@ -16,15 +26,3 @@ page_title
1626
title: content
1727
break
1828
{ return adjust(title); }
19-
20-
page_task
21-
= '+'
22-
space?
23-
description: description
24-
actions: task_actions*
25-
break?
26-
27-
{ let task = { description, tests: [], hints: [] };
28-
actions.forEach(({type, value}) => task[type].push(value));
29-
return task;
30-
}

‎parser/pegjs/task.pegjs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
1+
page_task
2+
= '+'
3+
space?
4+
description: description
5+
actions: task_actions*
6+
break?
7+
8+
{ let task = { description, tests: [], hints: [] };
9+
actions.forEach(({type, value}) => {
10+
if (taskTypes.includes(type)) {
11+
task[type].push(value);
12+
} else if (pageTypes.includes(type)) {
13+
output.pages[pages.length - 1][type] = value;
14+
}
15+
});
16+
return task;
17+
}
18+
119
task_actions
2-
= test: task_test
3-
/ hint: task_hint
4-
/ action: task_action
20+
= task_test
21+
/ task_hint
22+
/ task_action
23+
/ page_actions
524

625
task_test
726
= '@test'

‎test/import.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎test/page-complete.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,25 @@ description
1515
t.is(result.pages[0].onPageComplete, expected);
1616
});
1717

18+
test('parses a multiple onPageCompletes', t => {
19+
const expectedFirst = 'page one complete';
20+
const expectedSecond = 'page two complete'
21+
const data = `# Title
22+
description
23+
24+
## Page One
25+
description
26+
27+
@onPageComplete('${expectedFirst}')
28+
29+
## Page Two
30+
description
31+
32+
@onPageComplete('${expectedSecond}')
33+
`;
34+
const result = parse(data);
35+
t.is(result.pages[0].onPageComplete, expectedFirst);
36+
t.is(result.pages[1].onPageComplete, expectedSecond);
37+
});
38+
1839
test.todo('throws when multiple onPageCompletes');

0 commit comments

Comments
 (0)