-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (50 loc) · 1.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var DEBUG = true;
var js2coffee = require('js2coffee');
fs = require('fs')
fs.readFile('test.md', 'ascii', function (err,data) {
if (err) {
return console.log(err);
}
if (DEBUG == true) console.log("Length of document: " + data.length);
var startBlock = [];
var jsStart = [];
var endBlock = [];
var jsEnd = [];
// Calculate and store all indices of the js code blocks (beginings and endings)
for (var index = data.indexOf("~~~js");index >= 0; index = data.indexOf("~~~js", index + 1)) {
startBlock.push(index);
jsStart.push(index+6);
endBlock.push(data.indexOf("~~~\n", index+6)+4);
jsEnd.push(data.indexOf("~~~\n", index+6));
}
// Debug information
if (DEBUG == true) {
console.log("Start of Block: " + startBlock);
console.log("Start of actual Javascript: " + jsStart);
console.log("End of actual Javascript: " + jsEnd);
console.log("End of Block: " + endBlock);
}
// Create a resulatant Markdown string 'buffer'
var resultMarkdown = "";
if (startBlock.length == endBlock.length && jsStart.length == jsEnd.length) {
convertedCode = [] // An array that will hold all Coffee code.
// Convert all code blocks to Coffee blocks
for (i = 0; i<jsStart.length; i++) {
var js = data.substring(jsStart[i], jsEnd[i]);
var cs = js2coffee.build(js, {no_comments: false});
convertedCode.push("\n~~~coffee\n"+cs+"\n~~~\n");
if (i == 0) {
resultMarkdown += data.substring(0, endBlock[i]) + convertedCode[i];
}
if (i > 0) {
resultMarkdown += data.substring(endBlock[i-1], endBlock[i]) + convertedCode[i];
}
if (i == jsStart.length - 1) {
resultMarkdown += data.substring(endBlock[i], data.length);
}
}
console.log(resultMarkdown);
} else {
console.log("Invalid syntax, code blocks probably not closed");
}
});