class File
Constants
- ALT_SEPARATOR
- SEPARATOR
Attributes
eof[R]
lineno[R]
path[R]
Public Class Methods
basename(path)
click to toggle source
# File lib/asciidoctor/opal_ext/file.rb, line 79 def self.basename(path) (offset = path.rindex SEPARATOR) ? path[(offset + 1)..-1] : path end
dirname(path)
click to toggle source
# File lib/asciidoctor/opal_ext/file.rb, line 83 def self.dirname(path) (offset = path.rindex SEPARATOR) ? path[0..(offset - 1)] : '.' end
expand_path(path)
click to toggle source
# File lib/asciidoctor/opal_ext/file.rb, line 71 def self.expand_path(path) path end
extname(path)
click to toggle source
# File lib/asciidoctor/opal_ext/file.rb, line 87 def self.extname(path) return '' if path.nil_or_empty? last_dot_idx = path[1..-1].rindex('.') last_dot_idx.nil? ? '' : path[(last_dot_idx + 1)..-1] end
file?(path)
click to toggle source
TODO use XMLHttpRequest HEAD request unless in local file mode
# File lib/asciidoctor/opal_ext/file.rb, line 94 def self.file?(path) true end
join(*paths)
click to toggle source
# File lib/asciidoctor/opal_ext/file.rb, line 75 def self.join(*paths) paths * SEPARATOR end
new(path, mode = 'r')
click to toggle source
# File lib/asciidoctor/opal_ext/file.rb, line 22 def initialize(path, mode = 'r') @path = path @contents = nil @eof = false @lineno = 0 end
read(path)
click to toggle source
# File lib/asciidoctor/opal_ext/file.rb, line 98 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 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`}; } } } %xdata` end
Public Instance Methods
each_line(separator = $/, &block)
click to toggle source
# File lib/asciidoctor/opal_ext/file.rb, line 40 def each_line(separator = $/, &block) if @eof return block_given? ? self : [].to_enum end if block_given? lines = File.read(@path) %x{ self.eof = false; self.lineno = 0; var chomped = #{lines.chomp}, trailing = lines.length != chomped.length, splitted = chomped.split(separator); for (var i = 0, length = splitted.length; i < length; i++) { self.lineno += 1; if (i < length - 1 || trailing) { #{yield `splitted[i] + separator`}; } else { #{yield `splitted[i]`}; } } self.eof = true; } self else read.each_line end end
read()
click to toggle source
# File lib/asciidoctor/opal_ext/file.rb, line 29 def read if @eof '' else res = File.read(@path) @eof = true @lineno = res.size res end end