@@ -25,7 +25,7 @@ function parseWithCode(code, content) {
2525 return false ;
2626 }
2727}
28- function build ( filePath ) {
28+ function build ( lines ) {
2929 var result = {
3030 project : {
3131 title : '' ,
@@ -37,13 +37,11 @@ function build(filePath) {
3737 page : - 1 ,
3838 task : - 1
3939 } ;
40- var input = fs . readFileSync ( filePath , 'utf8' ) ;
41- var lines = input . split ( '\n' ) ;
4240 return project ( result , lines , index ) ;
4341}
4442function project ( result , lines , index ) {
45- let matchedAt = null ;
46- for ( let i = 0 ; i < lines . length ; i ++ ) {
43+ var matchedAt = null ;
44+ for ( var i = 0 ; i < lines . length ; i ++ ) {
4745 var projectTitleMatch = parseWithCode ( '#' , lines [ i ] ) ;
4846 var chapterStart = parseWithCode ( '##' , lines [ i ] ) ;
4947 if ( projectTitleMatch ) {
@@ -61,9 +59,9 @@ function project(result, lines, index) {
6159}
6260function chapter ( result , lines , index ) {
6361 var matchedAt = null ;
64- for ( let i = 0 ; i < lines . length ; i ++ ) {
65- let chapterTitleMatch = parseWithCode ( '##' , lines [ i ] ) ;
66- let pageStart = parseWithCode ( '###' , lines [ i ] ) ;
62+ for ( var i = 0 ; i < lines . length ; i ++ ) {
63+ var chapterTitleMatch = parseWithCode ( '##' , lines [ i ] ) ;
64+ var pageStart = parseWithCode ( '###' , lines [ i ] ) ;
6765 if ( chapterTitleMatch && ! matchedAt ) {
6866 matchedAt = i ;
6967 index . page = - 1 ;
@@ -87,7 +85,7 @@ function chapter(result, lines, index) {
8785 return result ;
8886}
8987function page ( result , lines , index ) {
90- let hasBreak = null ;
88+ var hasBreak = null ;
9189 index . page += 1 ;
9290 index . task = - 1 ;
9391 result . chapters [ index . chapter ] . pages . push ( {
@@ -96,12 +94,12 @@ function page(result, lines, index) {
9694 explanation : '' ,
9795 tasks : [ ]
9896 } ) ;
99- let inCodeBlock = false ;
100- for ( let i = 1 ; i < lines . length ; i ++ ) {
101- let pageTitleMatch = parseWithCode ( '###' , lines [ i ] ) ;
102- let nextChapter = parseWithCode ( '##' , lines [ i ] ) ;
103- let nextTask = parseWithCode ( '+' , lines [ i ] ) ;
104- let codeBlock = parseWithCode ( '```' , lines [ i ] ) ;
97+ var inCodeBlock = false ;
98+ for ( var i = 1 ; i < lines . length ; i ++ ) {
99+ var pageTitleMatch = parseWithCode ( '###' , lines [ i ] ) ;
100+ var nextChapter = parseWithCode ( '##' , lines [ i ] ) ;
101+ var nextTask = parseWithCode ( '+' , lines [ i ] ) ;
102+ var codeBlock = parseWithCode ( '```' , lines [ i ] ) ;
105103 if ( ! ! codeBlock ) {
106104 inCodeBlock = ! inCodeBlock ;
107105 }
@@ -138,20 +136,20 @@ function task(result, lines, index) {
138136 actions : [ ]
139137 } ) ;
140138 index . task += 1 ;
141- let inCodeBlock = false ;
142- for ( let i = 1 ; i < lines . length ; i ++ ) {
143- let nextPage = parseWithCode ( '###' , lines [ i ] ) ;
144- let nextChapter = parseWithCode ( '##' , lines [ i ] ) ;
145- let nextTask = parseWithCode ( '+' , lines [ i ] ) ;
146- let isPossibleAction = lines [ i ] . match ( / ^ @ a c t i o n | t e s t | h i n t / ) ;
147- let codeBlock = parseWithCode ( '```' , lines [ i ] ) ;
139+ var inCodeBlock = false ;
140+ for ( var i = 1 ; i < lines . length ; i ++ ) {
141+ var nextPage = parseWithCode ( '###' , lines [ i ] ) ;
142+ var nextChapter = parseWithCode ( '##' , lines [ i ] ) ;
143+ var nextTask = parseWithCode ( '+' , lines [ i ] ) ;
144+ var isPossibleAction = lines [ i ] . match ( / ^ @ a c t i o n | t e s t | h i n t / ) ;
145+ var codeBlock = parseWithCode ( '```' , lines [ i ] ) ;
148146 if ( ! ! codeBlock ) {
149147 inCodeBlock = ! inCodeBlock ;
150148 }
151149 if ( ! inCodeBlock ) {
152150 if ( ! ! isPossibleAction ) {
153- let action = lines [ i ] . slice ( 1 ) . split ( '(' ) [ 0 ] ;
154- let target = / \( ( .* ?) \) $ / . exec ( lines [ i ] ) [ 1 ] ;
151+ var action = lines [ i ] . slice ( 1 ) . split ( '(' ) [ 0 ] ;
152+ var target = / \( ( .* ?) \) $ / . exec ( lines [ i ] ) [ 1 ] ;
155153 switch ( action ) {
156154 case 'test' :
157155 result . chapters [ index . chapter ] . pages [ index . page ] . tasks [ index . task ] . tests . push ( target ) ;
@@ -192,12 +190,12 @@ function removeLineBreaks(text) {
192190}
193191function cleanup ( result ) {
194192 result . project . description = removeLineBreaks ( result . project . description ) ;
195- result . chapters . map ( ( chapter ) => {
193+ result . chapters . map ( function ( chapter ) {
196194 chapter . description = removeLineBreaks ( chapter . description ) ;
197- chapter . pages . map ( ( page ) => {
195+ chapter . pages . map ( function ( page ) {
198196 page . description = removeLineBreaks ( page . description ) ;
199197 page . explanation = removeLineBreaks ( page . explanation ) ;
200- page . tasks . map ( ( task ) => {
198+ page . tasks . map ( function ( task ) {
201199 task . description = removeLineBreaks ( task . description ) ;
202200 } ) ;
203201 } ) ;
@@ -214,25 +212,21 @@ function isValidJSON(text) {
214212 return false ;
215213 }
216214}
217- var output = process . argv [ 2 ] ;
218- if ( ! output ) {
219- throw ( 'Pass in path to output cr.json file' ) ;
220- }
221- var input = process . argv [ 3 ] ;
222- if ( ! input ) {
223- input = './README.md' ;
224- console . log ( `
225- Pass in a path to your .md file, otherwise it defaults to README.md
226- For example: npm start ./src/source.md
227- ` ) ;
228- }
229- var result = cleanup ( build ( input ) ) ;
230- if ( ! isValidJSON ( result ) ) {
231- throw ( 'Invalid JSON output' ) ;
232- }
233- fs . writeFile ( output , result ) , 'utf8' , function ( err ) {
234- if ( err )
235- return console . log ( err ) ;
236- console . log ( input + ' > ' + output ) ;
215+ module . exports = function ( filePath , output ) {
216+ if ( output === void 0 ) { output = './coderoad.json' ; }
217+ var lines = fs . readFileSync ( filePath , 'utf8' ) . split ( '\n' ) ;
218+ var result = cleanup ( build ( lines ) ) ;
219+ if ( ! isValidJSON ( result ) ) {
220+ console . log ( 'Invalid JSON output' ) ;
221+ process . exit ( 0 ) ;
222+ }
223+ else {
224+ try {
225+ fs . writeFileSync ( output , result , 'utf8' ) ;
226+ }
227+ catch ( e ) {
228+ console . log ( e ) ;
229+ process . exit ( 0 ) ;
230+ }
231+ }
237232} ;
238- ;
0 commit comments