lib/asciidoctor/opal_ext/file.rb in asciidoctor-1.5.0 vs lib/asciidoctor/opal_ext/file.rb in asciidoctor-1.5.1

- old
+ new

@@ -96,29 +96,36 @@ end def self.read(path) %x{ var data = '' - var status = -1; - try { - var xhr = new XMLHttpRequest(); - xhr.open('GET', path, false); - xhr.addEventListener('load', function() { - status = this.status; - // status is 0 for local file mode (i.e., file://) - if (status == 0 || status == 200) { - data = this.responseText; - } - }); - xhr.overrideMimeType('text/plain'); - xhr.send(); - } - catch (e) { - status = 0; - } - // 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`}; + if (typeof module !== 'undefined' && module.exports) { + // Running under Node.js + var fs = require("fs"); + data = fs.readFileSync(path, "utf8"); + } else { + // Running under the browser + var status = -1; + try { + var xhr = new XMLHttpRequest(); + xhr.open('GET', path, false); + xhr.addEventListener('load', function() { + status = this.status; + // status is 0 for local file mode (i.e., file://) + if (status == 0 || status == 200) { + data = this.responseText; + } + }); + xhr.overrideMimeType('text/plain'); + xhr.send(); + } + catch (e) { + status = 0; + } + // 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` end end