Class: PutText::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/puttext/extractor.rb

Defined Under Namespace

Classes: NoSuchFileError, UnsupportedFileError

Constant Summary collapse

EXTENSIONS =
{
  '.rb'   => 'Ruby',
  '.slim' => 'Slim'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.file_supported?(path) ⇒ Boolean

Check if a file is supported by the parser, based on its extension.

Returns:

  • (Boolean)

    whether the file is supported.



30
31
32
33
34
35
36
37
38
# File 'lib/puttext/extractor.rb', line 30

def self.file_supported?(path)
  EXTENSIONS.each do |ext, parser_name|
    next unless path.end_with?(ext)
    next unless parser_class_by_name(parser_name)
    return true
  end

  false
end

.parser_class_by_name(name) ⇒ Class

Return the class of a parser by its name

Parameters:

  • name (String)

    the name of the parser.

Returns:

  • (Class)

    the classof the parser



24
25
26
# File 'lib/puttext/extractor.rb', line 24

def self.parser_class_by_name(name)
  PutText::Parser.const_get(name)
end

Instance Method Details

#extract(path) ⇒ POFile

Extract strings from files in the given path.

Parameters:

  • path (String)

    the path of a directory or file to extract strings from.

Returns:

  • (POFile)

    a POFile object, representing the strings extracted from the files or file in the specified path.



45
46
47
48
49
50
# File 'lib/puttext/extractor.rb', line 45

def extract(path)
  files           = files_in_path(path)
  supported_files = filter_files(files)

  parse_files(supported_files)
end

#extract_from_file(path) ⇒ Array<POEntry>

Parse gettext strings from a file in the path.

Parameters:

  • path (String)

    the path of the file to parse.

Returns:

  • (Array<POEntry>)

    an array of POEntry objects extracted from the given file.



56
57
58
# File 'lib/puttext/extractor.rb', line 56

def extract_from_file(path)
  parser_by_path(path).strings_from_file(path)
end