Skip to content

Commit b662952

Browse files
committed
typings, progress
1 parent 895eed2 commit b662952

File tree

6 files changed

+120
-60
lines changed

6 files changed

+120
-60
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
"test": "tests"
88
},
99
"scripts": {
10-
"test": "echo \"Error: no test specified\" && exit 1"
10+
"test": "echo \"Error: no test specified\" && exit 1",
11+
"start": "node src/build.js"
1112
},
1213
"author": "Shawn McKay <shawn.j.mckay@gmail.com>",
1314
"license": "ISC",
1415
"devDependencies": {
1516
"chai": "^3.4.1",
16-
"lodash": "^4.0.0",
1717
"mocha": "^2.3.4"
1818
}
1919
}

src/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ This is a test
55
Some chapter one content.
66

77
### Page one
8-
Some page content
8+
page one description
99

10-
### Page two
10+
page one explanation
1111

12-
Some content
12+
### Page two
13+
thing

src/build.js

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use strict";
22
var fs = require('fs');
3-
const filePath = './src/README.md';
43
function line(char, times) {
54
return new RegExp('^' + char + '{' + times + '}(?!#)(.*?)$', 'gm');
65
}
@@ -35,8 +34,8 @@ function build(filePath) {
3534
return project(result, lines, index);
3635
}
3736
function project(result, lines, index) {
38-
var matchedAt = null;
39-
for (var i = 0; i < lines.length; i++) {
37+
let matchedAt = null;
38+
for (let i = 0; i < lines.length; i++) {
4039
var projectTitleMatch = parseWithCode('#', lines[i]);
4140
var chapterStart = parseWithCode('##', lines[i]);
4241
if (projectTitleMatch) {
@@ -45,18 +44,19 @@ function project(result, lines, index) {
4544
}
4645
else if (chapterStart) {
4746
result.project.description = lines.slice(matchedAt + 1, i).toString();
48-
chapter(result, lines.slice(i), index);
47+
return chapter(result, lines.slice(i), index);
4948
}
5049
}
5150
return result;
5251
}
5352
function chapter(result, lines, index) {
5453
var matchedAt = null;
55-
for (var i = 0; i < lines.length; i++) {
56-
var chapterTitleMatch = parseWithCode('##', lines[i]);
57-
var pageStart = parseWithCode('###', lines[i]);
54+
for (let i = 0; i < lines.length; i++) {
55+
let chapterTitleMatch = parseWithCode('##', lines[i]);
56+
let pageStart = parseWithCode('###', lines[i]);
5857
if (chapterTitleMatch && !matchedAt) {
5958
matchedAt = i;
59+
index.page = 0;
6060
index.chapter += 1;
6161
result.chapters.push({
6262
title: chapterTitleMatch,
@@ -76,30 +76,36 @@ function chapter(result, lines, index) {
7676
return result;
7777
}
7878
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]);
79+
let matchedAt = null;
80+
let hasBreak = null;
81+
for (let i = 0; i < lines.length; i++) {
82+
let pageTitleMatch = parseWithCode('###', lines[i]);
83+
let nextChapterStart = parseWithCode('##', lines[i]);
8484
if (pageTitleMatch && !matchedAt) {
8585
matchedAt = i;
86-
index.page += 1;
8786
result.chapters[index.chapter].pages.push({
8887
title: pageTitleMatch,
8988
description: '',
90-
explanation: ''
89+
explanation: '',
90+
tasks: []
9191
});
92+
index.page += 1;
9293
}
93-
else if (!firstBreak && lines[i].match(/\s*/)) {
94-
firstBreak = i;
94+
else if (!hasBreak && !lines[i].match(/\S/)) {
95+
hasBreak = i;
9596
}
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();
97+
else if (pageTitleMatch || nextChapterStart) {
98+
if (hasBreak) {
99+
console.log('HERE!!!', hasBreak);
100+
console.log(lines.slice(matchedAt, hasBreak).toString());
101+
console.log(lines.slice(hasBreak, i).toString());
102+
result.chapters[index.chapter].pages[index.page - 1].description = lines.slice(matchedAt + 1, hasBreak).toString();
103+
result.chapters[index.chapter].pages[index.page - 1].explanation = lines.slice(hasBreak + 1, i).toString();
100104
}
101105
else {
102-
result.chapters[index.chapter].pages[index.page].description = lines.slice(matchedAt + 1, i).toString();
106+
console.log('DOWN HERE');
107+
console.log(lines.slice(matchedAt + 1, i).toString());
108+
result.chapters[index.chapter].pages[index.page - 1].description = lines.slice(matchedAt + 1, i).toString();
103109
}
104110
if (nextChapterStart) {
105111
return chapter(result, lines.slice(i), index);
@@ -110,11 +116,11 @@ function page(result, lines, index) {
110116
}
111117
}
112118
console.log('*** Pages ***');
113-
console.log(result.chapters[0].pages);
119+
console.log(result.chapters[0].pages[0]);
114120
console.log('** Result ***');
115121
return result;
116122
}
117123
function task(result, lines, index) {
118124
return result;
119125
}
120-
console.log(build(filePath));
126+
console.log(build('./src/README.md'));

src/build.ts

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as fs from 'fs';
2-
const filePath = './src/README.md';
32
// JSON.stringify
43

54
function line(char: string, times: number) {
@@ -14,6 +13,7 @@ var regex = {
1413
'@': line('@', 1)
1514
};
1615

16+
1717
function 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'));

src/typings.d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
interface Index {
3+
chapter: number;
4+
page: number;
5+
}
6+
7+
interface Result {
8+
project: Object;
9+
chapters: Chapter[];
10+
}
11+
12+
interface Chapter {
13+
title: string;
14+
description: string;
15+
pages: Page[];
16+
}
17+
18+
interface Page {
19+
title: string;
20+
description: string;
21+
explanation: string;
22+
tasks: Task[];
23+
}
24+
25+
interface Task {
26+
title: string;
27+
description: string;
28+
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"src/**/*.ts"
1010
],
1111
"files": [
12-
"src/build.ts"
12+
"src/build.ts",
13+
"src/typings.d.ts"
1314
],
1415
"exclude": [
1516
"node_modules"

0 commit comments

Comments
 (0)