lib/asciidoctor/opal_ext/file.rb in asciidoctor-1.5.1 vs lib/asciidoctor/opal_ext/file.rb in asciidoctor-1.5.2
- old
+ new
@@ -42,11 +42,11 @@
return block_given? ? self : [].to_enum
end
if block_given?
lines = File.read(@path)
- %x{
+ %x(
self.eof = false;
self.lineno = 0;
var chomped = #{lines.chomp},
trailing = lines.length != chomped.length,
splitted = chomped.split(separator);
@@ -59,11 +59,11 @@
else {
#{yield `splitted[i]`};
}
}
self.eof = true;
- }
+ )
self
else
read.each_line
end
end
@@ -94,18 +94,26 @@
def self.file?(path)
true
end
def self.read(path)
- %x{
- var data = ''
- if (typeof module !== 'undefined' && module.exports) {
- // Running under Node.js
- var fs = require("fs");
- data = fs.readFileSync(path, "utf8");
- } else {
- // Running under the browser
+ case JAVASCRIPT_PLATFORM
+ when 'node'
+ %x(return require('fs').readFileSync(path, 'utf8');)
+ when 'java-nashorn'
+ %x(
+ var Paths = Java.type('java.nio.file.Paths');
+ var Files = Java.type('java.nio.file.Files');
+ var lines = Files.readAllLines(Paths.get(path), Java.type('java.nio.charset.StandardCharsets').UTF_8);
+ var data = [];
+ lines.forEach(function(line) { data.push(line); });
+ return data.join("\n");
+ )
+ #when 'java-rhino'
+ when 'browser'
+ %x(
+ var data = '';
var status = -1;
try {
var xhr = new XMLHttpRequest();
xhr.open('GET', path, false);
xhr.addEventListener('load', function() {
@@ -123,10 +131,15 @@
}
// assume that no data in local file mode means it doesn't exist
if (status == 404 || (status == 0 && data == '')) {
throw #{IOError.new `'No such file or directory: ' + path`};
}
- }
- }
- `data`
+ return data;
+ )
+ # NOTE we're assuming standalone is SpiderMonkey
+ when 'standalone'
+ %x(return read(path);)
+ else
+ ''
+ end
end
end