11{
2- // final parsed output for coderoad.json file
2+ // Initial Data
3+
34 var output = {
45 info: {
56 title: ' Tutorial Title' ,
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' ,
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 }
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
7798page_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
96123task_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
101129task_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
141160task_action
142161 = '@action'
0 commit comments