-
Notifications
You must be signed in to change notification settings - Fork 0
/
tome.js
258 lines (202 loc) · 6.15 KB
/
tome.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env node
var markdown = require( "markdown" ).markdown;
var fs = require('fs');
const path = require("path")
const configPath = "./tome.json";
var validFileExtensions = ["md", "txt"];
var htmlHead = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{title}</title>
<!--
<link rel="stylesheet" href="reset.css">
-->
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="main-content">
`;
var htmlTail = `
</div>
</body>
</html>
`;
const tableHead = `<table style="width:100%;" valign="top">`;
const tableTail = `</table>`;
const tdHead = `<td valign="top" style="width:{width_style}">`;
const tdTail = '</td>';
const trHead = '<tr>';
const trTail = '</tr>';
const argv = process.argv;
var jobs = null;
// Check to see if there are any commandline arguments, and attempt to
// interpret them as input and output filenames
if (argv.length >= 3 ) {
var commands = argv.slice(2);
var pathToSource = "" + commands[0];
var pathToOutputFile = null;
if (!fs.existsSync(pathToSource)) {
console.log("Path does not exist:", pathToSource);
process.exit();
return;
}
if (commands.length > 1) {
pathToOutputFile = commands[1];
} else {
pathToOutputFile = changeSuffix(pathToSource, "html");
}
jobs = [];
jobs.push({
source: pathToSource,
outputFile: pathToOutputFile
});
}
// if the jobs were not derived from the CLI arguments,
// we will attempt to load the config tile
if (jobs == null) {
// check if the config file exists
if (!fs.existsSync(configPath)) {
console.log("Error - ", configPath, " was not found.");
process.exit();
}
// Load the config
var config = fs.readFileSync(configPath);
jobs = JSON.parse(config);
}
/// Job Execution ///
jobs.forEach( (job) => {
console.log("job" , job)
var data = "";
var source = process.cwd() + "/" + job.source;
console.log("fill source path: ", source)
let sourceStats = fs.statSync(source);
var sourceIsSingleFile = sourceStats.isFile();
// create the list of files. If the source is a directory
// collect all the files from that directory
var files = null;
if (sourceIsSingleFile) {
files = [source];
} else {
files = getAllFiles(source);
}
var documentContents = [];
files.forEach((file) => {
if (!fs.existsSync(file)) {
// NOTE: this should be impossible, since we got the filenames
// by querying the file listing
console.log("File does not exist:", file);
return;
}
var fileContent = fs.readFileSync(file);
// NOTE / TODO: Should check to see if there is a newline
// and only add one if there isn't?
// (Or is it always necessary?)
documentContents.push(fileContent + '\n');
});
// Strip lines that are unwanted
var combinedTexts = documentContents.join('');
combinedTexts = stripHtmlComments(combinedTexts);
// split into individual lines so we can remove unwanted ones
var lines = combinedTexts.split("\n");
var outputLines = [];
lines.forEach(line => {
// skip "outline notes"
if (isOutlineNote(line)) {
return;
}
outputLines.push(line);
});
combinedTexts = outputLines.join('\n');
const compiled = parse(combinedTexts, job.title || "");
var outputFileFullPath = process.cwd() + "/" + job.outputFile;
// clean the path
outputFileFullPath = path.resolve(outputFileFullPath);
// Check if we are overwriting one of the source files!
// (This mode is dangerous, because the output file may be
// inside the scope of the source files)
// so we will check to make sure nobody accidentally overwrites one
// of the source files!
files.forEach(f => {
if (outputFileFullPath == path.resolve(f)) {
console.log("cannot overwrite one of the source files!");
process.exit();
}
});
ensureDirectoryExistence(outputFileFullPath);
fs.writeFileSync(outputFileFullPath, compiled);
console.log("Finished:", job.title || job.source);
});
function ensureDirectoryExistence(filePath) {
var dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}
function stripHtmlComments(content) {
return content.replace(/<!--(?!>)[\S\s]*?-->/g, '');
}
function isHiddenPath(path) {
return (/(^|\/)\.[^\/\.]/g).test(path);
}
function hasValidFileExtension(path) {
for (let i = 0; i < validFileExtensions.length; i++) {
const extension = validFileExtensions[i];
if (path.endsWith(extension)) {
return true;
}
}
return false;
}
function getAllFiles(dirPath, arrayOfFiles) {
files = fs.readdirSync(dirPath);
arrayOfFiles = arrayOfFiles || [];
files.forEach(function(file) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles)
} else {
var fullPath = path.join(dirPath, "/", file);
if (
!isHiddenPath(fullPath)
&& hasValidFileExtension(fullPath)
) {
arrayOfFiles.push(fullPath);
}
}
})
return arrayOfFiles;
}
function parse(data, title = "") {
// parse the markdown of the remaining text
var finalOutput = markdown.toHTML(data);
var customHtmlHead = htmlHead.replace("{title}", title);
// add html head and tail
finalOutput = [customHtmlHead, finalOutput, htmlTail].join("");
return finalOutput;
}
function removeAllWhitespace(s) {
return s.replace(/\s+/g, '');
}
function changeSuffix(filePath, newSuffix) {
var suffix = path.extname(filePath);
var newPath = filePath.substring(0, filePath.length - suffix.length)
+ "." + newSuffix;
return newPath;
}
function isOutlineNote(s) {
// if it's not a hash, then it's not an outline note
if (s[0] != '#') {
return false;
}
// find the first character that isn't a hash or a space
// and if it's an `@`, it's an Outline Note
for (var i = 1; i < s.length; i++) {
var c = s[i];
if (c != "#" && c != " ") {
return c == '@';
}
}
}