Skip to content

Commit 0dba2fe

Browse files
committed
auto indent js files
1 parent 1c4fda5 commit 0dba2fe

File tree

12 files changed

+129
-88
lines changed

12 files changed

+129
-88
lines changed

‎parser/compile/indent.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
// https://github.com/sindresorhus/repeating/blob/master/index.js
4+
// https://github.com/sindresorhus/indent-string/blob/master/index.js
5+
6+
const repeating = (n, str) => {
7+
str = str === undefined ? ' ' : str;
8+
let ret = '';
9+
do {
10+
if (n & 1) {
11+
ret += str;
12+
}
13+
str += str;
14+
} while ((n >>= 1));
15+
return ret;
16+
}
17+
18+
module.exports = (str, count) => {
19+
let indent = ' ';
20+
count = count === undefined ? 1 : count;
21+
22+
if (typeof str !== 'string') {
23+
throw new TypeError(`Expected \`input\` to be a \`string\`, got \`${typeof str}\``);
24+
}
25+
26+
if (typeof count !== 'number') {
27+
throw new TypeError(`Expected \`count\` to be a \`number\`, got \`${typeof count}\``);
28+
}
29+
30+
if (count === 0) {
31+
return str;
32+
}
33+
34+
indent = count > 1 ? repeating(count, indent) : indent;
35+
36+
return str.replace(/^(?!\s*$)/mg, indent);
37+
};

‎parser/compile/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const pegjs = require('pegjs');
44
const loadImports = require('./loadImports');
55

66
let compiled = '';
7-
const start = readFileSync(join(__dirname, '../pegjs/start.pegjs'), 'utf8');
7+
const start = readFileSync(join(__dirname, '../pegjs/index.pegjs'), 'utf8');
88

99
compiled += loadImports(start);
1010

‎parser/compile/loadImports.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
const { readFileSync } = require('fs');
22
const { join } = require('path');
3+
const indent = require('./indent');
34

45
const importPath = /^@import\((.+)\)$/gm;
6+
const pegjs = /\.pegjs$/;
57

68
function loadImports(str) {
9+
// collect all imports
710
const imports = str.match(importPath);
11+
812
imports.forEach(i => {
9-
const fileName = i.slice(9, -2) + '.pegjs';
10-
const file = readFileSync(join(__dirname, '../pegjs/', fileName), 'utf8');
13+
14+
// capture file name
15+
const fileName = i.slice(9, -2);
16+
17+
// load import
18+
let file = readFileSync(join(__dirname, '../pegjs/', fileName), 'utf8');
19+
20+
21+
// indent js files
22+
if (!fileName.match(pegjs)) {
23+
file = indent(file, 2);
24+
}
25+
26+
// add imported file to text
1127
str = str.replace(i, file);
1228
});
1329
return str;

‎parser/index.pegjs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
return trimQuotes(trimBrackets(str));
3838
}
3939

40-
4140
}
4241

4342
start
@@ -63,7 +62,6 @@ info_title
6362
title: content
6463
{ return adjust(title); }
6564

66-
6765
page
6866
= title: page_title
6967
description: description*
@@ -83,7 +81,6 @@ page_title
8381
break
8482
{ return adjust(title); }
8583

86-
8784
page_task
8885
= '+'
8986
space?
@@ -119,11 +116,9 @@ task_hint
119116
return { type: 'hints', value: h };
120117
}
121118

122-
123119
page_actions
124120
= on_page_complete
125121

126-
127122
on_page_complete
128123
= '@onPageComplete'
129124
'('
@@ -143,7 +138,6 @@ page_import
143138
break
144139
{ return filePath.join(''); }
145140

146-
147141
task_action
148142
= '@action'
149143
'('
@@ -202,13 +196,11 @@ action_write_from_file
202196
quote
203197
')'
204198

205-
206199
description
207200
= description: content
208201
break
209202
{ return adjust(description); }
210203

211-
212204
content = [^#^@^+] [^\n^\r]+ [\n\r]
213205
space = [ \s]
214206
break = [\n\r]?

‎parser/pegjs/data.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// final parsed output for coderoad.json file
2+
var output = {
3+
info: {
4+
title: 'Tutorial Title',
5+
description: '',
6+
},
7+
pages: []
8+
};

‎parser/pegjs/functions.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function adjust(item) {
2+
return item[0].concat(item[1].join(''));
3+
}
4+
5+
function trim({desc, str, first, last}) {
6+
if ( str[0].match(first) && str[str.length - 1].match(last || first) ) {
7+
return str.slice(1, -1);
8+
}
9+
console.log('Error. Could not parse ' + desc + ' in ' + str);
10+
return str;
11+
}
12+
13+
function trimBrackets(str) {
14+
return trim({
15+
desc: 'bracket',
16+
str: str,
17+
first: /\(/,
18+
last: /\)/,
19+
});
20+
}
21+
22+
function trimQuotes(str) {
23+
return trim({
24+
desc: 'quote',
25+
str,
26+
first: /[\"\'\`]/
27+
});
28+
}
29+
30+
function trimBracketsAndQuotes(str) {
31+
return trimQuotes(trimBrackets(str));
32+
}

‎parser/pegjs/functions.pegjs

Lines changed: 0 additions & 28 deletions
This file was deleted.

‎parser/pegjs/index.pegjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
@import('./data.js')
3+
@import('./functions.js')
4+
}
5+
6+
start
7+
= doc
8+
{ return output; }
9+
10+
doc
11+
= info
12+
break?
13+
page*
14+
15+
@import('./info.pegjs')
16+
@import('./page.pegjs')
17+
@import('./task.pegjs')
18+
@import('./page-actions.pegjs')
19+
@import('./task-actions.pegjs')
20+
@import('./shared.pegjs')
21+
@import('./characters.pegjs')

‎parser/pegjs/page-actions.pegjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
page_actions
22
= on_page_complete
33

4-
54
on_page_complete
65
= '@onPageComplete'
76
'('

‎parser/pegjs/page.pegjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,15 @@ page_title
1616
title: content
1717
break
1818
{ return adjust(title); }
19+
20+
page_task
21+
= '+'
22+
space?
23+
description: description
24+
actions: task_actions*
25+
break?
26+
27+
{ let task = { description, tests: [], hints: [] };
28+
actions.forEach(({type, value}) => task[type].push(value));
29+
return task;
30+
}

‎parser/pegjs/start.pegjs

Lines changed: 0 additions & 36 deletions
This file was deleted.

‎parser/pegjs/page-task.pegjs renamed to ‎parser/pegjs/task.pegjs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
page_task
2-
= '+'
3-
space?
4-
description: description
5-
actions: task_actions*
6-
break?
7-
8-
{ let task = { description, tests: [], hints: [] };
9-
actions.forEach(({type, value}) => task[type].push(value));
10-
return task;
11-
}
12-
131
task_actions
142
= test: task_test
153
/ hint: task_hint

0 commit comments

Comments
 (0)