11import * as fs from 'fs' ;
2- const filePath = './src/README.md' ;
32// JSON.stringify
43
54function line ( char : string , times : number ) {
@@ -14,6 +13,7 @@ var regex = {
1413 '@' : line ( '@' , 1 )
1514} ;
1615
16+
1717function parseWithCode ( code : string , content : string ) {
1818 if ( ! content ) {
1919 return false ;
@@ -25,7 +25,7 @@ function parseWithCode(code: string, content: string) {
2525 }
2626}
2727
28- function build ( filePath ) {
28+ function build ( filePath : string ) : Result {
2929 var result = {
3030 project : { } ,
3131 chapters : [ ]
@@ -46,9 +46,9 @@ function build(filePath) {
4646}
4747
4848// project -> chapters
49- function project ( result , lines : string [ ] , index ) {
50- var matchedAt = null ;
51- for ( var i = 0 ; i < lines . length ; i ++ ) {
49+ function project ( result : Result , lines : string [ ] , index : Index ) {
50+ let matchedAt = null ;
51+ for ( let i = 0 ; i < lines . length ; i ++ ) {
5252 var projectTitleMatch = parseWithCode ( '#' , lines [ i ] ) ;
5353 var chapterStart = parseWithCode ( '##' , lines [ i ] ) ;
5454 if ( projectTitleMatch ) {
@@ -57,7 +57,7 @@ function project(result, lines: string[], index) {
5757 result . project . title = projectTitleMatch ;
5858 } else if ( chapterStart ) {
5959 result . project . description = lines . slice ( matchedAt + 1 , i ) . toString ( ) ;
60- chapter ( result , lines . slice ( i ) , index ) ;
60+ return chapter ( result , lines . slice ( i ) , index ) ;
6161 }
6262 }
6363 return result ;
@@ -71,23 +71,28 @@ function project(result, lines: string[], index) {
7171// ##
7272// - ##
7373// - ###
74- function chapter ( result , lines : string [ ] , index ) {
74+ function chapter ( result : Result , lines : string [ ] , index : Index ) : Result {
7575 var matchedAt = null ;
76- for ( var i = 0 ; i < lines . length ; i ++ ) {
77- var chapterTitleMatch = parseWithCode ( '##' , lines [ i ] ) ;
78- var pageStart = parseWithCode ( '###' , lines [ i ] ) ;
76+ for ( let i = 0 ; i < lines . length ; i ++ ) {
77+ // matches
78+ let chapterTitleMatch = parseWithCode ( '##' , lines [ i ] ) ;
79+ let pageStart = parseWithCode ( '###' , lines [ i ] ) ;
80+
81+ // chapter title
7982 if ( chapterTitleMatch && ! matchedAt ) {
80- // chapter title
8183 matchedAt = i ;
84+ index . page = 0 ;
8285 index . chapter += 1 ;
8386 result . chapters . push ( {
8487 title : chapterTitleMatch ,
8588 description : '' ,
8689 pages : [ ]
8790 } ) ;
91+ // next page
8892 } else if ( pageStart ) {
8993 result . chapters [ index . chapter ] . description = lines . slice ( matchedAt + 1 , i ) . toString ( ) ;
9094 return page ( result , lines . slice ( i ) , index ) ;
95+ // next chapter
9196 } else if ( chapterTitleMatch ) {
9297 result . chapters [ index . chapter ] . description = lines . slice ( matchedAt + 1 , i ) . toString ( ) ;
9398 return chapter ( result , lines . slice ( i ) , index ) ;
@@ -105,30 +110,48 @@ function chapter(result, lines: string[], index) {
105110// - ###
106111// - ##
107112// - +
108- function page ( result , lines : string [ ] , index ) {
109- var matchedAt = null ;
110- var firstBreak = null ;
111- for ( var i = 0 ; i < lines . length ; i ++ ) {
112- var pageTitleMatch = parseWithCode ( '###' , lines [ i ] ) ;
113- var nextChapterStart = parseWithCode ( '##' , lines [ i ] ) ;
113+ function page ( result : Result , lines : string [ ] , index : Index ) {
114+ let matchedAt = null ;
115+ let hasBreak : number = null ;
116+
117+ for ( let i = 0 ; i < lines . length ; i ++ ) {
118+ // matches
119+ let pageTitleMatch = parseWithCode ( '###' , lines [ i ] ) ;
120+ let nextChapterStart = parseWithCode ( '##' , lines [ i ] ) ;
121+
122+ // page title
114123 if ( pageTitleMatch && ! matchedAt ) {
115- // chapter title
116124 matchedAt = i ;
117- index . page += 1 ;
118125 result . chapters [ index . chapter ] . pages . push ( {
119126 title : pageTitleMatch ,
120127 description : '' ,
121- explanation : ''
128+ explanation : '' ,
129+ tasks : [ ]
122130 } ) ;
123- } else if ( ! firstBreak && lines [ i ] . match ( / \s * / ) ) {
124- firstBreak = i ;
125- } else if ( nextChapterStart || pageTitleMatch ) {
126- if ( firstBreak ) {
127- result . chapters [ index . chapter ] . pages [ index . page ] . description = lines . slice ( matchedAt + 1 , firstBreak ) . toString ( ) ;
128- result . chapters [ index . chapter ] . pages [ index . page ] . explanation = lines . slice ( firstBreak + 1 , i ) . toString ( ) ;
131+ index . page += 1 ;
132+
133+ // empty line
134+ } else if ( ! hasBreak && ! lines [ i ] . match ( / \S / ) ) {
135+ hasBreak = i ;
136+
137+ // description / break / explanation
138+ } else if ( pageTitleMatch || nextChapterStart ) {
139+ if ( hasBreak ) {
140+ console . log ( 'HERE!!!' , hasBreak ) ;
141+ console . log ( lines . slice ( matchedAt , hasBreak ) . toString ( ) ) ;
142+ console . log ( lines . slice ( hasBreak , i ) . toString ( ) ) ;
143+
144+
145+ result . chapters [ index . chapter ] . pages [ index . page - 1 ] . description = lines . slice ( matchedAt + 1 , hasBreak ) . toString ( ) ;
146+ result . chapters [ index . chapter ] . pages [ index . page - 1 ] . explanation = lines . slice ( hasBreak + 1 , i ) . toString ( ) ;
129147 } else {
130- result . chapters [ index . chapter ] . pages [ index . page ] . description = lines . slice ( matchedAt + 1 , i ) . toString ( ) ;
148+ console . log ( 'DOWN HERE' ) ;
149+ console . log ( lines . slice ( matchedAt + 1 , i ) . toString ( ) ) ;
150+
151+ result . chapters [ index . chapter ] . pages [ index . page - 1 ] . description = lines . slice ( matchedAt + 1 , i ) . toString ( ) ;
131152 }
153+
154+ // next chapter
132155 if ( nextChapterStart ) {
133156 return chapter ( result , lines . slice ( i ) , index ) ;
134157 } else {
@@ -137,8 +160,8 @@ function page(result, lines: string[], index) {
137160 }
138161 }
139162 console . log ( '*** Pages ***' ) ;
140- console . log ( result . chapters [ 0 ] . pages ) ;
141- console . log ( '** Result ***' )
163+ console . log ( result . chapters [ 0 ] . pages [ 0 ] ) ;
164+ console . log ( '** Result ***' ) ;
142165 return result ;
143166}
144167
@@ -151,8 +174,9 @@ function page(result, lines: string[], index) {
151174// - @test
152175// - @action
153176// - @hint
154- function task ( result , lines , index ) {
177+ function task ( result : Result , lines : string [ ] , index : Index ) {
178+ //
155179 return result ;
156180}
157181
158- console . log ( build ( filePath ) ) ;
182+ console . log ( build ( './src/README.md' ) ) ;
0 commit comments