Skip to content

Commit 895eed2

Browse files
committed
progress towards builder
1 parent c6741b8 commit 895eed2

File tree

4 files changed

+210
-58
lines changed

4 files changed

+210
-58
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"license": "ISC",
1414
"devDependencies": {
1515
"chai": "^3.4.1",
16+
"lodash": "^4.0.0",
1617
"mocha": "^2.3.4"
1718
}
1819
}

src/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,10 @@ This is a test
33

44
## Chapter one
55
Some chapter one content.
6+
7+
### Page one
8+
Some page content
9+
10+
### Page two
11+
12+
Some content

src/build.js

Lines changed: 87 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ var regex = {
88
'#': line('#', 1),
99
'##': line('#', 2),
1010
'###': line('#', 3),
11-
'+': line('\\+', 1)
11+
'+': line('\\+', 1),
12+
'@': line('@', 1)
1213
};
1314
function parseWithCode(code, content) {
1415
if (!content) {
@@ -21,33 +22,99 @@ function parseWithCode(code, content) {
2122
return false;
2223
}
2324
}
24-
function build() {
25+
function build(filePath) {
2526
var result = {
2627
project: {},
2728
chapters: []
29+
}, index = {
30+
chapter: -1,
31+
page: -1
2832
};
29-
var lines = fs.readFileSync(filePath, 'utf8');
30-
var arrayOfLines = lines.split('\n');
31-
var chapterCount = 0;
32-
var pageCount = 0;
33-
for (var i = 0; i < arrayOfLines.length; i++) {
34-
var projectTitleMatch = parseWithCode('#', arrayOfLines[i]);
33+
var input = fs.readFileSync(filePath, 'utf8');
34+
var lines = input.split('\n');
35+
return project(result, lines, index);
36+
}
37+
function project(result, lines, index) {
38+
var matchedAt = null;
39+
for (var i = 0; i < lines.length; i++) {
40+
var projectTitleMatch = parseWithCode('#', lines[i]);
41+
var chapterStart = parseWithCode('##', lines[i]);
3542
if (projectTitleMatch) {
43+
matchedAt = i;
3644
result.project.title = projectTitleMatch;
37-
result.project.description = '';
38-
for (var j = i + 1; j < arrayOfLines.length; j++) {
39-
var chapterTitleMatch = parseWithCode('##', arrayOfLines[j]);
40-
if (!chapterTitleMatch) {
41-
result.project.description.concat(arrayOfLines[j]);
42-
}
43-
else {
44-
result.chapters.push({
45-
title: chapterTitleMatch
46-
});
47-
}
45+
}
46+
else if (chapterStart) {
47+
result.project.description = lines.slice(matchedAt + 1, i).toString();
48+
chapter(result, lines.slice(i), index);
49+
}
50+
}
51+
return result;
52+
}
53+
function chapter(result, lines, index) {
54+
var matchedAt = null;
55+
for (var i = 0; i < lines.length; i++) {
56+
var chapterTitleMatch = parseWithCode('##', lines[i]);
57+
var pageStart = parseWithCode('###', lines[i]);
58+
if (chapterTitleMatch && !matchedAt) {
59+
matchedAt = i;
60+
index.chapter += 1;
61+
result.chapters.push({
62+
title: chapterTitleMatch,
63+
description: '',
64+
pages: []
65+
});
66+
}
67+
else if (pageStart) {
68+
result.chapters[index.chapter].description = lines.slice(matchedAt + 1, i).toString();
69+
return page(result, lines.slice(i), index);
70+
}
71+
else if (chapterTitleMatch) {
72+
result.chapters[index.chapter].description = lines.slice(matchedAt + 1, i).toString();
73+
return chapter(result, lines.slice(i), index);
74+
}
75+
}
76+
return result;
77+
}
78+
function page(result, lines, index) {
79+
var matchedAt = null;
80+
var firstBreak = null;
81+
for (var i = 0; i < lines.length; i++) {
82+
var pageTitleMatch = parseWithCode('###', lines[i]);
83+
var nextChapterStart = parseWithCode('##', lines[i]);
84+
if (pageTitleMatch && !matchedAt) {
85+
matchedAt = i;
86+
index.page += 1;
87+
result.chapters[index.chapter].pages.push({
88+
title: pageTitleMatch,
89+
description: '',
90+
explanation: ''
91+
});
92+
}
93+
else if (!firstBreak && lines[i].match(/\s*/)) {
94+
firstBreak = i;
95+
}
96+
else if (nextChapterStart || pageTitleMatch) {
97+
if (firstBreak) {
98+
result.chapters[index.chapter].pages[index.page].description = lines.slice(matchedAt + 1, firstBreak).toString();
99+
result.chapters[index.chapter].pages[index.page].explanation = lines.slice(firstBreak + 1, i).toString();
100+
}
101+
else {
102+
result.chapters[index.chapter].pages[index.page].description = lines.slice(matchedAt + 1, i).toString();
103+
}
104+
if (nextChapterStart) {
105+
return chapter(result, lines.slice(i), index);
106+
}
107+
else {
108+
return page(result, lines.slice(i), index);
48109
}
49110
}
50111
}
112+
console.log('*** Pages ***');
113+
console.log(result.chapters[0].pages);
114+
console.log('** Result ***');
115+
return result;
116+
}
117+
function task(result, lines, index) {
51118
return result;
52119
}
53-
console.log(build());
120+
console.log(build(filePath));

src/build.ts

Lines changed: 115 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1617
function 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

Comments
 (0)