Module: Aef::BreakVerter

Defined in:
lib/breakverter/breakverter.rb

Overview

BreakVerter is Ruby library and commandline tool for conversion of text between linebreak encoding formats of unix, windows or mac.

Constant Summary collapse

VERSION =
'1.2.0'
BREAKS =
{
  :unix => "\n",
  :windows => "\r\n",
  :mac => "\r"
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert(input, output_encoding = :unix) ⇒ Object

Convert a file with any linebreak encoding to a specific linebreak encoding.

If given output_encoding is not a key of BREAKS, all linebreaks are replaced with output_encoding’s content itself.



33
34
35
36
37
38
# File 'lib/breakverter/breakverter.rb', line 33

def self.convert(input, output_encoding = :unix)
  if input.respond_to?(:to_s) then input = input.to_s
  else raise 'Input has to be a string or must support to_s' end

  input.gsub(/\r(?:\n)?|\n/, BREAKS[output_encoding] || output_encoding)
end

Instance Method Details

#linebreaks(output_encoding = :unix) ⇒ Object

Shortcut for direct receiver conversion, for example if BreakVerter is included into the string class



42
43
44
# File 'lib/breakverter/breakverter.rb', line 42

def linebreaks(output_encoding = :unix)
  Aef::BreakVerter.convert(self, output_encoding)
end