Class: Tahweel::Writers::Json

Inherits:
Object
  • Object
show all
Defined in:
lib/tahweel/writers/json.rb

Overview

Writer class for outputting text to a .json file.

Instance Method Summary collapse

Instance Method Details

#extensionString

Returns the file extension for this writer.

Returns:

  • (String)

    The file extension.



12
# File 'lib/tahweel/writers/json.rb', line 12

def extension = "json"

#write(texts, destination, options = {}) ⇒ void

This method returns an undefined value.

Writes the extracted texts to a file.

Parameters:

  • texts (Array<String>)

    The extracted texts (one per page).

  • destination (String)

    The output file path.

  • options (Hash) (defaults to: {})

    Options for writing (unused for now).



20
21
22
23
24
25
26
27
28
29
# File 'lib/tahweel/writers/json.rb', line 20

def write(texts, destination, options = {}) # rubocop:disable Lint/UnusedMethodArgument
  structured_data = texts.map.with_index do |text, index|
    {
      page: index + 1,
      content: text.strip
    }
  end

  File.write(destination, JSON.pretty_generate(structured_data))
end