@@ -36,7 +36,8 @@ function build(filePath: string) {
3636 } ,
3737 index = {
3838 chapter : - 1 ,
39- page : - 1
39+ page : - 1 ,
40+ task : - 1
4041 } ;
4142 var input = fs . readFileSync ( filePath , 'utf8' ) ;
4243 var lines = input . split ( '\n' ) ;
@@ -115,17 +116,16 @@ function chapter(result: Result, lines: string[], index: Index): Result {
115116// - ##
116117// - +
117118function page ( result : Result , lines : string [ ] , index : Index ) {
118- let matchedAt : number = null ;
119119 let hasBreak : number = null ;
120120 index . page += 1 ;
121+ index . task = - 1 ;
121122 result . chapters [ index . chapter ] . pages . push ( {
122123 title : parseWithCode ( '###' , lines [ 0 ] ) . trim ( ) ,
123124 description : '' ,
124125 explanation : '' ,
125126 tasks : [ ]
126- } ) ;
127+ } ) ;
127128 for ( let i = 1 ; i < lines . length ; i ++ ) {
128-
129129 // matches
130130 let pageTitleMatch = parseWithCode ( '###' , lines [ i ] ) ;
131131 let nextChapter = parseWithCode ( '##' , lines [ i ] ) ;
@@ -157,6 +157,7 @@ function page(result: Result, lines: string[], index: Index) {
157157 return result ;
158158}
159159
160+
160161// task
161162// continue from matches(+)
162163// matches(@) = capture action
@@ -167,9 +168,46 @@ function page(result: Result, lines: string[], index: Index) {
167168// - @action
168169// - @hint
169170function task ( result : Result , lines : string [ ] , index : Index ) {
170- //
171+ result . chapters [ index . chapter ] . pages [ index . page ] . tasks . push ( {
172+ title : parseWithCode ( '+' , lines [ 0 ] ) ,
173+ description : '' ,
174+ tests : [ ] ,
175+ actions : [ ]
176+ } ) ;
177+ index . task += 1 ;
178+ for ( let i = 1 ; i < lines . length ; i ++ ) {
179+ // matches
180+ let nextPage = parseWithCode ( '###' , lines [ i ] ) ;
181+ let nextChapter = parseWithCode ( '##' , lines [ i ] ) ;
182+ let nextTask = parseWithCode ( '+' , lines [ i ] ) ;
183+ let isPossibleAction = lines [ i ] . match ( / ^ @ / ) ;
184+
185+ if ( ! ! nextPage || ! ! nextChapter || ! ! nextTask ) {
186+ result . chapters [ index . chapter ] . pages [ index . page ] . tasks [ index . task ] . description = lines . slice ( 1 , i ) . toString ( ) ;
187+ }
188+ if ( ! ! nextTask ) {
189+ return task ( result , lines . slice ( i ) , index ) ;
190+ } else if ( ! ! nextPage ) {
191+ return page ( result , lines . slice ( i ) , index ) ;
192+ } else if ( ! ! nextChapter ) {
193+ return chapter ( result , lines . slice ( i ) , index ) ;
194+ } else if ( ! ! isPossibleAction ) {
195+ let action = lines [ i ] . slice ( 1 ) . split ( '(' ) [ 0 ] ;
196+ let target = / \( ( .* ?) \) $ / . exec ( lines [ i ] ) [ 1 ] ;
197+ switch ( action ) {
198+ case 'test' :
199+ result . chapters [ index . chapter ] . pages [ index . page ] . tasks [ index . task ] . tests . push ( target ) ;
200+ break ;
201+ case 'action' :
202+ result . chapters [ index . chapter ] . pages [ index . page ] . tasks [ index . task ] . actions . push ( target ) ;
203+ break ;
204+ default :
205+ console . log ( 'Invalid task action' ) ;
206+ }
207+ }
208+ }
171209 return result ;
172210}
173211
174- console . log ( build ( './src/README.md' ) . chapters [ 0 ] . pages [ 1 ] ) ;
212+ console . log ( build ( './src/README.md' ) . chapters [ 0 ] . pages [ 1 ] . tasks [ 1 ] ) ;
175213// build('./src/README.md');
0 commit comments