@@ -14,6 +14,10 @@ var regex = {
1414} ;
1515
1616
17+ function isEmpty ( line : string ) : boolean {
18+ return ! line . length || ! ! line . match ( / ^ \s + ?[ \n \r ] / ) ;
19+ }
20+
1721function parseWithCode ( code : string , content : string ) {
1822 if ( ! content ) {
1923 return false ;
@@ -25,7 +29,7 @@ function parseWithCode(code: string, content: string) {
2529 }
2630}
2731
28- function build ( filePath : string ) : Result {
32+ function build ( filePath : string ) {
2933 var result = {
3034 project : { } ,
3135 chapters : [ ]
@@ -54,7 +58,7 @@ function project(result: Result, lines: string[], index: Index) {
5458 if ( projectTitleMatch ) {
5559 // project.title
5660 matchedAt = i ;
57- result . project . title = projectTitleMatch ;
61+ result . project . title = projectTitleMatch . trim ( ) ;
5862 } else if ( chapterStart ) {
5963 result . project . description = lines . slice ( matchedAt + 1 , i ) . toString ( ) ;
6064 return chapter ( result , lines . slice ( i ) , index ) ;
@@ -81,10 +85,10 @@ function chapter(result: Result, lines: string[], index: Index): Result {
8185 // chapter title
8286 if ( chapterTitleMatch && ! matchedAt ) {
8387 matchedAt = i ;
84- index . page = 0 ;
88+ index . page = - 1 ;
8589 index . chapter += 1 ;
8690 result . chapters . push ( {
87- title : chapterTitleMatch ,
91+ title : chapterTitleMatch . trim ( ) ,
8892 description : '' ,
8993 pages : [ ]
9094 } ) ;
@@ -111,57 +115,45 @@ function chapter(result: Result, lines: string[], index: Index): Result {
111115// - ##
112116// - +
113117function page ( result : Result , lines : string [ ] , index : Index ) {
114- let matchedAt = null ;
118+ let matchedAt : number = null ;
115119 let hasBreak : number = null ;
120+ index . page += 1 ;
121+ result . chapters [ index . chapter ] . pages . push ( {
122+ title : parseWithCode ( '###' , lines [ 0 ] ) . trim ( ) ,
123+ description : '' ,
124+ explanation : '' ,
125+ tasks : [ ]
126+ } ) ;
127+ for ( let i = 1 ; i < lines . length ; i ++ ) {
116128
117- for ( let i = 0 ; i < lines . length ; i ++ ) {
118129 // matches
119130 let pageTitleMatch = parseWithCode ( '###' , lines [ i ] ) ;
120- let nextChapterStart = parseWithCode ( '##' , lines [ i ] ) ;
121-
122- // page title
123- if ( pageTitleMatch && ! matchedAt ) {
124- matchedAt = i ;
125- result . chapters [ index . chapter ] . pages . push ( {
126- title : pageTitleMatch ,
127- description : '' ,
128- explanation : '' ,
129- tasks : [ ]
130- } ) ;
131- index . page += 1 ;
132-
133- // empty line
134- } else if ( ! hasBreak && ! lines [ i ] . match ( / \S / ) ) {
131+ let nextChapter = parseWithCode ( '##' , lines [ i ] ) ;
132+ let nextTask = parseWithCode ( '+' , lines [ i ] ) ;
133+ // 1. page title
134+ if ( ! hasBreak && isEmpty ( lines [ i ] ) ) {
135135 hasBreak = i ;
136+ // 3. exit on page title match again or next chapter
137+ } else if ( pageTitleMatch || nextChapter ) {
136138
137- // description / break / explanation
138- } else if ( pageTitleMatch || nextChapterStart ) {
139+ // add to result
139140 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 ( ) ;
141+ result . chapters [ index . chapter ] . pages [ index . page ] . description = lines . slice ( 1 , hasBreak ) . toString ( ) ;
142+ result . chapters [ index . chapter ] . pages [ index . page ] . explanation = lines . slice ( hasBreak + 1 , i ) . toString ( ) ;
147143 } else {
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 ( ) ;
144+ result . chapters [ index . chapter ] . pages [ index . page ] . description = lines . slice ( 1 , i ) . toString ( ) ;
152145 }
153-
154146 // next chapter
155- if ( nextChapterStart ) {
147+ if ( ! ! nextChapter ) {
156148 return chapter ( result , lines . slice ( i ) , index ) ;
157- } else {
149+ // next page
150+ } else if ( ! ! pageTitleMatch ) {
158151 return page ( result , lines . slice ( i ) , index ) ;
159152 }
153+ } else if ( ! ! nextTask ) {
154+ return task ( result , lines . slice ( i ) , index ) ;
160155 }
161156 }
162- console . log ( '*** Pages ***' ) ;
163- console . log ( result . chapters [ 0 ] . pages [ 0 ] ) ;
164- console . log ( '** Result ***' ) ;
165157 return result ;
166158}
167159
@@ -179,4 +171,5 @@ function task(result: Result, lines: string[], index: Index) {
179171 return result ;
180172}
181173
182- console . log ( build ( './src/README.md' ) ) ;
174+ console . log ( build ( './src/README.md' ) . chapters [ 0 ] . pages [ 1 ] ) ;
175+ // build('./src/README.md');
0 commit comments