forked from popeindustries/inline-source
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinline-source.js
More file actions
73 lines (62 loc) · 1.68 KB
/
inline-source.js
File metadata and controls
73 lines (62 loc) · 1.68 KB
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
'use strict';
var context = require('./lib/context')
, fs = require('fs')
, path = require('path')
, parse = require('./lib/parse')
, run = require('./lib/run')
, utils = require('./lib/utils');
/**
* Inline sources found in 'htmlpath'
* @param {String} htmlpath
* @param {Object} options
* @param {Function} fn(err, html)
*/
module.exports = function inlineSource (htmlpath, options, fn) {
if ('function' == typeof options) {
fn = options;
options = {};
}
var ctx = context.create(options)
, next = function (html) {
ctx.html = html;
parse(ctx, function (err) {
if (err) return fn(err);
if (ctx.sources.length) {
run(ctx, ctx.sources, ctx.swallowErrors, fn);
} else {
return fn(null, ctx.html);
}
});
};
if (utils.isFilepath(htmlpath)) {
ctx.htmlpath = path.resolve(htmlpath);
fs.readFile(ctx.htmlpath, 'utf8', function (err, content) {
if (err) return fn(err);
next(content);
});
// Passed file content instead of path
} else {
next(htmlpath);
}
};
/**
* Synchronously inline sources found in 'htmlpath'
* @param {String} htmlpath
* @param {Object} options
* @returns {String}
*/
module.exports.sync = function inlineSourceSync (htmlpath, options) {
options = options || {};
var ctx = context.create(options);
if (utils.isFilepath(htmlpath)) {
ctx.htmlpath = path.resolve(htmlpath);
ctx.html = fs.readFileSync(ctx.htmlpath, 'utf8');
// Passed file content instead of path
} else {
ctx.html = htmlpath;
}
parse(ctx);
return (ctx.sources.length)
? run(ctx, ctx.sources, ctx.swallowErrors)
: ctx.html;
};