vsm :: FileIndex :: defaultinit
# A VSMIndex to index files
class FileIndex
super StringIndex
# Index a file from its `path`.
#
# Return the created document or null if `path` is not accepted by `accept_file`.
#
# See `index_document`.
fun index_file(path: String, auto_update: nullable Bool): nullable DOC do
if not accept_file(path) then return null
var vector = parse_file(path)
var doc = new Document(path, path, vector)
index_document(doc, auto_update)
return doc
end
# Index multiple files
#
# The recursive method `index_dir` will be called for each directory found
# in `paths`.
#
# See `index_file`
fun index_files(paths: Collection[String], auto_update: nullable Bool) do
for path in paths do
if path.to_path.is_dir then
index_dir(path, false)
else
index_file(path, false)
end
end
if auto_update != null and auto_update then update_index
end
# Index all files in `dir` recursively
#
# See `index_file`.
fun index_dir(dir: String, auto_update: nullable Bool) do
if not dir.to_path.is_dir then return
for file in dir.files do
var path = dir / file
if path.to_path.is_dir then
index_dir(path, false)
else
index_file(path, false)
end
end
if auto_update != null and auto_update then update_index
end
# Is `path` accepted depending on `whitelist_exts` and `blacklist_exts`?
fun accept_file(path: String): Bool do
var ext = path.file_extension
if ext != null then
ext = ext.to_lower
if blacklist_exts.has(ext) then return false
if whitelist_exts.not_empty and not whitelist_exts.has(ext) then return false
end
return whitelist_exts.is_empty
end
# Parse the `file` content as a Vector
#
# See `parse_string`.
fun parse_file(file: String): Vector do
return parse_string(file.to_path.read_all)
end
# File extensions white list
#
# If not empty, only files with these extensions will be indexed.
#
# If an extension is in both `whitelist_exts` and `blacklist_exts`, the
# blacklist will prevail and the file will be ignored.
var whitelist_exts = new Array[String] is writable
# File extensions black list
#
# Files with these extensions will not be indexed.
var blacklist_exts = new Array[String] is writable
end
lib/vsm/vsm.nit:255,1--335,3