@@ -10,7 +10,8 @@ var regex = {
1010 '#' : line ( '#' , 1 ) ,
1111 '##' : line ( '#' , 2 ) ,
1212 '###' : line ( '#' , 3 ) ,
13- '+' : line ( '\\+' , 1 )
13+ '+' : line ( '\\+' , 1 ) ,
14+ '@' : line ( '@' , 1 )
1415} ;
1516
1617function parseWithCode ( code : string , content : string ) {
@@ -24,58 +25,134 @@ function parseWithCode(code: string, content: string) {
2425 }
2526}
2627
27- function build ( ) {
28+ function build ( filePath ) {
2829 var result = {
2930 project : { } ,
3031 chapters : [ ]
31- } ;
32-
33- var lines = fs . readFileSync ( filePath , 'utf8' ) ;
34- var arrayOfLines = lines . split ( '\n' ) ;
35- var chapterCount = 0 ;
36- var pageCount = 0 ;
32+ } ,
33+ index = {
34+ chapter : - 1 ,
35+ page : - 1
36+ } ;
37+ var input = fs . readFileSync ( filePath , 'utf8' ) ;
38+ var lines = input . split ( '\n' ) ;
39+
40+ // project
41+ // matches(#) = capture
42+ // exit on matches(##)
43+ // #
44+ // - ##
45+ return project ( result , lines , index ) ;
46+ }
3747
38- for ( var i = 0 ; i < arrayOfLines . length ; i ++ ) {
39- var projectTitleMatch = parseWithCode ( '#' , arrayOfLines [ i ] )
48+ // project -> chapters
49+ function project ( result , lines : string [ ] , index ) {
50+ var matchedAt = null ;
51+ for ( var i = 0 ; i < lines . length ; i ++ ) {
52+ var projectTitleMatch = parseWithCode ( '#' , lines [ i ] ) ;
53+ var chapterStart = parseWithCode ( '##' , lines [ i ] ) ;
4054 if ( projectTitleMatch ) {
4155 // project.title
56+ matchedAt = i ;
4257 result . project . title = projectTitleMatch ;
43- result . project . description = '' ;
44- for ( var j = i + 1 ; j < arrayOfLines . length ; j ++ ) {
45- var chapterTitleMatch = parseWithCode ( '##' , arrayOfLines [ j ] ) ;
46- if ( ! chapterTitleMatch ) {
47- // project.description
48- result . project . description . concat ( arrayOfLines [ j ] ) ;
49- } else {
50- result . chapters . push ( {
51- title : chapterTitleMatch
52- } ) ;
53-
54- }
55- }
58+ } else if ( chapterStart ) {
59+ result . project . description = lines . slice ( matchedAt + 1 , i ) . toString ( ) ;
60+ chapter ( result , lines . slice ( i ) , index ) ;
5661 }
5762 }
58-
59-
60- // = parseWithCode('#', content);
61- // result.project.description = '';
62-
6363 return result ;
6464}
6565
66- console . log ( build ( ) ) ;
67-
68- // #
69-
70-
66+ // chapters -> pages
67+ // continue from matches(##)
68+ // matches(##) = capture
69+ // matches(###) = page
70+ // exit on end
7171// ##
72+ // - ##
73+ // - ###
74+ function chapter ( result , lines : string [ ] , index ) {
75+ var matchedAt = null ;
76+ for ( var i = 0 ; i < lines . length ; i ++ ) {
77+ var chapterTitleMatch = parseWithCode ( '##' , lines [ i ] ) ;
78+ var pageStart = parseWithCode ( '###' , lines [ i ] ) ;
79+ if ( chapterTitleMatch && ! matchedAt ) {
80+ // chapter title
81+ matchedAt = i ;
82+ index . chapter += 1 ;
83+ result . chapters . push ( {
84+ title : chapterTitleMatch ,
85+ description : '' ,
86+ pages : [ ]
87+ } ) ;
88+ } else if ( pageStart ) {
89+ result . chapters [ index . chapter ] . description = lines . slice ( matchedAt + 1 , i ) . toString ( ) ;
90+ return page ( result , lines . slice ( i ) , index ) ;
91+ } else if ( chapterTitleMatch ) {
92+ result . chapters [ index . chapter ] . description = lines . slice ( matchedAt + 1 , i ) . toString ( ) ;
93+ return chapter ( result , lines . slice ( i ) , index ) ;
94+ }
95+ }
96+ return result ;
97+ }
7298
99+ // pages -> explanation, next page, tasks
100+ // continue from matches(###)
101+ // matches(###) = capture
102+ // matches(+) = task
103+ // exit on end
73104// ###
105+ // - ###
106+ // - ##
107+ // - +
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 ] ) ;
114+ if ( pageTitleMatch && ! matchedAt ) {
115+ // chapter title
116+ matchedAt = i ;
117+ index . page += 1 ;
118+ result . chapters [ index . chapter ] . pages . push ( {
119+ title : pageTitleMatch ,
120+ description : '' ,
121+ explanation : ''
122+ } ) ;
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 ( ) ;
129+ } else {
130+ result . chapters [ index . chapter ] . pages [ index . page ] . description = lines . slice ( matchedAt + 1 , i ) . toString ( ) ;
131+ }
132+ if ( nextChapterStart ) {
133+ return chapter ( result , lines . slice ( i ) , index ) ;
134+ } else {
135+ return page ( result , lines . slice ( i ) , index ) ;
136+ }
137+ }
138+ }
139+ console . log ( '*** Pages ***' ) ;
140+ console . log ( result . chapters [ 0 ] . pages ) ;
141+ console . log ( '** Result ***' )
142+ return result ;
143+ }
74144
145+ // task
146+ // continue from matches(+)
147+ // matches(@) = capture action
148+ // exit on matches (##)
149+ // exit on end
75150// +
151+ // - @test
152+ // - @action
153+ // - @hint
154+ function task ( result , lines , index ) {
155+ return result ;
156+ }
76157
77- // @test
78-
79- // @action
80-
81- // @hint
158+ console . log ( build ( filePath ) ) ;
0 commit comments