Module: NotesStructuredTextStripBodies
- Defined in:
- lib/notes_structured_text_strip_bodies.rb
Class Attribute Summary collapse
-
.logger ⇒ Object
Returns the value of attribute logger.
Class Method Summary collapse
- .is_header_block?(block) ⇒ Boolean
- .log {|logger| ... } ⇒ Object
- .read_block(input) ⇒ Object
- .strip(output, input) ⇒ Object
- .strip_file(output_dir, input_file, options = {}) ⇒ Object
- .strip_files(output_dir, input_files, options = {}) ⇒ Object
Class Attribute Details
.logger ⇒ Object
Returns the value of attribute logger.
3 4 5 |
# File 'lib/notes_structured_text_strip_bodies.rb', line 3 def logger @logger end |
Class Method Details
.is_header_block?(block) ⇒ Boolean
50 51 52 |
# File 'lib/notes_structured_text_strip_bodies.rb', line 50 def is_header_block?(block) !!block.find{|l| l =~ /^\$MessageID: /i} end |
.log {|logger| ... } ⇒ Object
8 9 10 |
# File 'lib/notes_structured_text_strip_bodies.rb', line 8 def log yield logger if logger end |
.read_block(input) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/notes_structured_text_strip_bodies.rb', line 40 def read_block(input) return nil if input.eof? block = [] begin l = input.readline.chomp block << l if l.length>0 end while !input.eof? && l != "" block end |
.strip(output, input) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/notes_structured_text_strip_bodies.rb', line 54 def strip(output, input) while block=read_block(input) if is_header_block?(block) block.each{|l| output << l << "\n"} output << "\n" end end end |
.strip_file(output_dir, input_file, options = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/notes_structured_text_strip_bodies.rb', line 29 def strip_file(output_dir, input_file, ={}) output_file = File.join(output_dir, File.basename(input_file)) raise "<input_file>: #{input_file} does not exist or is not a regular file" if !File.file?(input_file) File.open(input_file, "r") do |input| File.open(output_file, "w") do |output| log{|logger| logger.info("stripping: '#{input_file}' => '#{output_file}'")} strip(output, input) end end end |
.strip_files(output_dir, input_files, options = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/notes_structured_text_strip_bodies.rb', line 12 def strip_files(output_dir, input_files, ={}) raise "<output_dir>: #{output_dir} must be a directory" if !File.directory?(output_dir) log{|logger| logger.info("stripping to output directory: '#{output_dir}'")} [*input_files].each do |input_file_glob| log{|logger| logger.info("processing glob: '#{input_file_glob}'")} glob_matches = Dir[input_file_glob] log{|logger| logger.warn("no files match glob: '#{input_file_glob}'")} if glob_matches.empty? glob_matches.each do |input_file| strip_file(output_dir, input_file) end end end |