Skip to content

Commit 44bba77

Browse files
committed
parser refactor
1 parent 1755172 commit 44bba77

File tree

10 files changed

+155
-133
lines changed

10 files changed

+155
-133
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# CodeRoad CLI
22

3-
Command line interface for CodeRoad. Coming soon.
3+
Command line interface for [CodeRoad](http://coderoad.github.io). Coming soon.
44

55
### Setup
66

cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var search_1 = require('./src/search/search');
99
var tutorials_1 = require('./src/tutorials/tutorials');
1010
var publish_1 = require('./src/publish/publish');
1111
program
12-
.version('0.0.1')
12+
.version('0.1.1')
1313
.usage('[options] <keywords>')
1414
.option('-b, --build [tutorial.md]', 'tutorial markdown file', /^.+\.md$/i)
1515
.option('-c, --create [name]', 'tutorial name')

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "coderoad-cli",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "Command line interface for CodeRoad. Build project files.",
55
"keywords": ["coderoad"],
66
"directories": {

src/build/build.js

Lines changed: 2 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
"use strict";
22
var fs = require('fs');
3-
var Match = require('./matchers');
43
var validate = require('./validators');
5-
var actions_1 = require('./actions');
64
var cleanup_1 = require('./cleanup');
5+
var project_1 = require('./parser/project');
76
function build(lines) {
87
var result = {
98
project: {},
@@ -14,130 +13,7 @@ function build(lines) {
1413
page: -1,
1514
task: -1
1615
};
17-
return project(result, lines, index);
18-
}
19-
function project(result, lines, index) {
20-
result.project = {
21-
title: '',
22-
description: ''
23-
};
24-
for (var i = 0; i < lines.length; i++) {
25-
var line = lines[i];
26-
var projectTitleMatch = Match.project(line);
27-
if (!!projectTitleMatch) {
28-
result.project.title = projectTitleMatch.trim();
29-
}
30-
else if (!!Match.chapter(line)) {
31-
return chapter(result, lines.slice(i), index);
32-
}
33-
else {
34-
result.project.description += line + '\n';
35-
}
36-
}
37-
return result;
38-
}
39-
function chapter(result, lines, index) {
40-
index.page = -1;
41-
index.chapter += 1;
42-
result.chapters.push({
43-
title: Match.chapter(lines[0]).trim(),
44-
pages: []
45-
});
46-
for (var i = 0; i < lines.length; i++) {
47-
var line = lines[i];
48-
if (Match.page(line)) {
49-
return page(result, lines.slice(i), index);
50-
}
51-
else if (Match.chapter(line) && i > 0) {
52-
return chapter(result, lines.slice(i), index);
53-
}
54-
else {
55-
if (result.chapters[index.chapter].description === undefined) {
56-
result.chapters[index.chapter].description = '';
57-
}
58-
result.chapters[index.chapter].description += line + '\n';
59-
}
60-
}
61-
return result;
62-
}
63-
function page(result, lines, index) {
64-
var hasBreak = null;
65-
index.page += 1;
66-
index.task = -1;
67-
result.chapters[index.chapter].pages.push({
68-
title: Match.page(lines[0]).trim()
69-
});
70-
var inCodeBlock = false;
71-
for (var i = 1; i < lines.length; i++) {
72-
var line = lines[i];
73-
if (!!Match.codeBlock(line)) {
74-
inCodeBlock = !inCodeBlock;
75-
}
76-
if (!inCodeBlock) {
77-
if (!hasBreak && Match.isEmpty(line)) {
78-
hasBreak = i;
79-
}
80-
else if (!!Match.chapter(line)) {
81-
return chapter(result, lines.slice(i), index);
82-
}
83-
else if (!!Match.page(line)) {
84-
return page(result, lines.slice(i), index);
85-
}
86-
else if (!!Match.task(line)) {
87-
if (result.chapters[index.chapter].pages[index.page].tasks === undefined) {
88-
result.chapters[index.chapter].pages[index.page].tasks = [];
89-
}
90-
return task(result, lines.slice(i), index);
91-
}
92-
else {
93-
if (!hasBreak) {
94-
if (result.chapters[index.chapter].pages[index.page].description === undefined) {
95-
result.chapters[index.chapter].pages[index.page].description = '';
96-
}
97-
result.chapters[index.chapter].pages[index.page].description += line + '\n';
98-
}
99-
else {
100-
if (result.chapters[index.chapter].pages[index.page].explanation === undefined) {
101-
result.chapters[index.chapter].pages[index.page].explanation = '';
102-
}
103-
result.chapters[index.chapter].pages[index.page].explanation += line + '\n';
104-
}
105-
}
106-
}
107-
}
108-
return result;
109-
}
110-
function task(result, lines, index) {
111-
result.chapters[index.chapter].pages[index.page].tasks.push({
112-
description: Match.task(lines[0])
113-
});
114-
index.task += 1;
115-
var inCodeBlock = false;
116-
for (var i = 1; i < lines.length; i++) {
117-
var line = lines[i];
118-
if (!!Match.codeBlock(line)) {
119-
inCodeBlock = !inCodeBlock;
120-
}
121-
if (!inCodeBlock) {
122-
var isAction = Match.isAction(line);
123-
if (!!isAction) {
124-
result = actions_1.default(result, line, index);
125-
}
126-
else if (!!Match.task(line)) {
127-
return task(result, lines.slice(i), index);
128-
}
129-
else if (!!Match.page(line)) {
130-
return page(result, lines.slice(i), index);
131-
}
132-
else if (!!Match.chapter(line)) {
133-
return chapter(result, lines.slice(i), index);
134-
}
135-
else {
136-
result.chapters[index.chapter].pages[index.page].tasks[index.task].description += line + '\n';
137-
}
138-
}
139-
}
140-
return result;
16+
return project_1.default(result, lines, index);
14117
}
14218
function default_1(filePath, output) {
14319
if (output === void 0) { output = './coderoad.json'; }

src/build/actions.js renamed to src/build/parser/actions.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use strict";
2-
var cleanup_1 = require('./cleanup');
3-
var Match = require('./matchers');
4-
function addToTasks(result, line, index) {
2+
var Match = require('./match');
3+
function actions(result, line, index) {
54
var action = line.slice(1).split('(')[0];
65
var value = cleanup_1.trimQuotes(/\((.*?)\)$/.exec(line)[1]);
76
var task = result.chapters[index.chapter].pages[index.page].tasks[index.task];
@@ -59,4 +58,4 @@ function addToTasks(result, line, index) {
5958
return result;
6059
}
6160
Object.defineProperty(exports, "__esModule", { value: true });
62-
exports.default = addToTasks;
61+
exports.default = actions;

src/build/parser/chapter.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use strict";
2+
var Match = require('./match');
3+
var chapter_1 = require('./chapter');
4+
var page_1 = require('./page');
5+
function chapter(result, lines, index) {
6+
index.page = -1;
7+
index.chapter += 1;
8+
result.chapters.push({
9+
title: Match.chapter(lines[0]).trim(),
10+
pages: []
11+
});
12+
for (var i = 0; i < lines.length; i++) {
13+
var line = lines[i];
14+
if (Match.page(line)) {
15+
return page_1.default(result, lines.slice(i), index);
16+
}
17+
else if (Match.chapter(line) && i > 0) {
18+
return chapter_1.default(result, lines.slice(i), index);
19+
}
20+
else {
21+
if (result.chapters[index.chapter].description === undefined) {
22+
result.chapters[index.chapter].description = '';
23+
}
24+
result.chapters[index.chapter].description += line + '\n';
25+
}
26+
}
27+
return result;
28+
}
29+
Object.defineProperty(exports, "__esModule", { value: true });
30+
exports.default = chapter;
File renamed without changes.

src/build/parser/page.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"use strict";
2+
var Match = require('./match');
3+
var chapter_1 = require('./chapter');
4+
var page_1 = require('./page');
5+
var task_1 = require('./task');
6+
function page(result, lines, index) {
7+
var hasBreak = null;
8+
index.page += 1;
9+
index.task = -1;
10+
result.chapters[index.chapter].pages.push({
11+
title: Match.page(lines[0]).trim()
12+
});
13+
var inCodeBlock = false;
14+
for (var i = 1; i < lines.length; i++) {
15+
var line = lines[i];
16+
if (!!Match.codeBlock(line)) {
17+
inCodeBlock = !inCodeBlock;
18+
}
19+
if (!inCodeBlock) {
20+
if (!hasBreak && Match.isEmpty(line)) {
21+
hasBreak = i;
22+
}
23+
else if (!!Match.chapter(line)) {
24+
return chapter_1.default(result, lines.slice(i), index);
25+
}
26+
else if (!!Match.page(line)) {
27+
return page_1.default(result, lines.slice(i), index);
28+
}
29+
else if (!!Match.task(line)) {
30+
if (result.chapters[index.chapter].pages[index.page].tasks === undefined) {
31+
result.chapters[index.chapter].pages[index.page].tasks = [];
32+
}
33+
return task_1.default(result, lines.slice(i), index);
34+
}
35+
else {
36+
if (!hasBreak) {
37+
if (result.chapters[index.chapter].pages[index.page].description === undefined) {
38+
result.chapters[index.chapter].pages[index.page].description = '';
39+
}
40+
result.chapters[index.chapter].pages[index.page].description += line + '\n';
41+
}
42+
else {
43+
if (result.chapters[index.chapter].pages[index.page].explanation === undefined) {
44+
result.chapters[index.chapter].pages[index.page].explanation = '';
45+
}
46+
result.chapters[index.chapter].pages[index.page].explanation += line + '\n';
47+
}
48+
}
49+
}
50+
}
51+
return result;
52+
}
53+
Object.defineProperty(exports, "__esModule", { value: true });
54+
exports.default = page;

src/build/parser/project.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
var Match = require('./match');
3+
var chapter_1 = require('./chapter');
4+
function project(result, lines, index) {
5+
result.project = {
6+
title: '',
7+
description: ''
8+
};
9+
for (var i = 0; i < lines.length; i++) {
10+
var line = lines[i];
11+
var projectTitleMatch = Match.project(line);
12+
if (!!projectTitleMatch) {
13+
result.project.title = projectTitleMatch.trim();
14+
}
15+
else if (!!Match.chapter(line)) {
16+
return chapter_1.default(result, lines.slice(i), index);
17+
}
18+
else {
19+
result.project.description += line + '\n';
20+
}
21+
}
22+
return result;
23+
}
24+
Object.defineProperty(exports, "__esModule", { value: true });
25+
exports.default = project;

src/build/parser/task.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"use strict";
2+
var Match = require('./match');
3+
var chapter_1 = require('./chapter');
4+
var page_1 = require('./page');
5+
function task(result, lines, index) {
6+
result.chapters[index.chapter].pages[index.page].tasks.push({
7+
description: Match.task(lines[0])
8+
});
9+
index.task += 1;
10+
var inCodeBlock = false;
11+
for (var i = 1; i < lines.length; i++) {
12+
var line = lines[i];
13+
if (!!Match.codeBlock(line)) {
14+
inCodeBlock = !inCodeBlock;
15+
}
16+
if (!inCodeBlock) {
17+
var isAction = Match.isAction(line);
18+
if (!!isAction) {
19+
result = addToTasks(result, line, index);
20+
}
21+
else if (!!Match.task(line)) {
22+
return task(result, lines.slice(i), index);
23+
}
24+
else if (!!Match.page(line)) {
25+
return page_1.default(result, lines.slice(i), index);
26+
}
27+
else if (!!Match.chapter(line)) {
28+
return chapter_1.default(result, lines.slice(i), index);
29+
}
30+
else {
31+
result.chapters[index.chapter].pages[index.page].tasks[index.task].description += line + '\n';
32+
}
33+
}
34+
}
35+
return result;
36+
}
37+
Object.defineProperty(exports, "__esModule", { value: true });
38+
exports.default = task;

0 commit comments

Comments
 (0)