Class: Wp2txt::OutputWriter
- Inherits:
-
Object
- Object
- Wp2txt::OutputWriter
- Defined in:
- lib/wp2txt/output_writer.rb
Overview
OutputWriter handles output file management with rotation Supports both text and JSONL formats
Instance Attribute Summary collapse
-
#output_files ⇒ Object
readonly
Get list of output files created.
Instance Method Summary collapse
-
#close ⇒ Object
Close current file and finalize.
-
#file_count ⇒ Integer
Get count of output files created so far.
-
#initialize(output_dir:, base_name:, format: :text, file_size_mb: 10) ⇒ OutputWriter
constructor
A new instance of OutputWriter.
-
#write(content) ⇒ Object
Write formatted article to output Thread-safe for parallel processing.
-
#write_from_file(source_path) ⇒ Object
Stream content from a file, rotating only at article boundaries (blank lines) This ensures no article is split across output files.
-
#write_raw(content) ⇒ Object
Write raw content directly without formatting Used for merging pre-formatted temp files.
Constructor Details
#initialize(output_dir:, base_name:, format: :text, file_size_mb: 10) ⇒ OutputWriter
Returns a new instance of OutputWriter.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/wp2txt/output_writer.rb', line 14 def initialize(output_dir:, base_name:, format: :text, file_size_mb: 10) @output_dir = output_dir @base_name = base_name @format = format @file_size_mb = file_size_mb @file_size_bytes = file_size_mb * 1024 * 1024 @current_file = nil @current_size = 0 @file_index = 1 @mutex = Mutex.new @output_files = [] FileUtils.mkdir_p(@output_dir) unless File.directory?(@output_dir) end |
Instance Attribute Details
#output_files ⇒ Object (readonly)
Get list of output files created
112 113 114 |
# File 'lib/wp2txt/output_writer.rb', line 112 def output_files @output_files end |
Instance Method Details
#close ⇒ Object
Close current file and finalize
104 105 106 107 108 109 |
# File 'lib/wp2txt/output_writer.rb', line 104 def close @mutex.synchronize do close_current_file end @output_files end |
#file_count ⇒ Integer
Get count of output files created so far
116 117 118 |
# File 'lib/wp2txt/output_writer.rb', line 116 def file_count @output_files.size end |
#write(content) ⇒ Object
Write formatted article to output Thread-safe for parallel processing
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/wp2txt/output_writer.rb', line 34 def write(content) return if content.nil? || (content.is_a?(String) && content.strip.empty?) @mutex.synchronize do ensure_file_open output = format_output(content) @current_file.write(output) @current_size += output.bytesize rotate_file_if_needed end rescue Errno::ENOSPC close_on_error raise Wp2txt::FileIOError, "Disk full: cannot write to output directory '#{@output_dir}'" rescue IOError, SystemCallError => e close_on_error raise Wp2txt::FileIOError, "Write failed: #{e.}" end |
#write_from_file(source_path) ⇒ Object
Stream content from a file, rotating only at article boundaries (blank lines) This ensures no article is split across output files
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/wp2txt/output_writer.rb', line 81 def write_from_file(source_path) return unless File.exist?(source_path) @mutex.synchronize do File.open(source_path, "r:UTF-8") do |src| src.each_line do |line| ensure_file_open @current_file.write(line) @current_size += line.bytesize # Only rotate at blank lines (article boundaries) rotate_file_if_needed if line.strip.empty? end end end rescue Errno::ENOSPC close_on_error raise Wp2txt::FileIOError, "Disk full: cannot write to output directory '#{@output_dir}'" rescue IOError, SystemCallError => e close_on_error raise Wp2txt::FileIOError, "Write failed: #{e.}" end |
#write_raw(content) ⇒ Object
Write raw content directly without formatting Used for merging pre-formatted temp files
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/wp2txt/output_writer.rb', line 58 def write_raw(content) return if content.nil? || content.empty? @mutex.synchronize do ensure_file_open @current_file.write(content) @current_size += content.bytesize rotate_file_if_needed end rescue Errno::ENOSPC close_on_error raise Wp2txt::FileIOError, "Disk full: cannot write to output directory '#{@output_dir}'" rescue IOError, SystemCallError => e close_on_error raise Wp2txt::FileIOError, "Write failed: #{e.}" end |