Class: LoaderRuby::Loaders::Text

Inherits:
Base
  • Object
show all
Includes:
EncodingDetector
Defined in:
lib/loader_ruby/loaders/text.rb

Constant Summary collapse

EXTENSIONS =
%w[.txt .md .markdown .text .log .rst].freeze

Constants included from EncodingDetector

EncodingDetector::BOM_MAP

Instance Method Summary collapse

Instance Method Details

#load(path, **opts) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/loader_ruby/loaders/text.rb', line 10

def load(path, **opts)
  check_file_exists!(path)
  check_file_size!(path)

  explicit_encoding = opts[:encoding]

  raw = File.binread(path)
  detected = explicit_encoding || detect_encoding_from_bom(raw) || LoaderRuby.configuration.default_encoding
  content = transcode_to_utf8(raw, detected)

  Document.new(
    content: content,
    metadata: (path, format: :text, encoding: detected)
  )
end