Module: Aef::Linebreak

Included in:
String
Defined in:
lib/aef/linebreak/linebreak.rb

Overview

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

If you want to use the String extension methods, simply use the following command:

require 'aef/linebreak/string_extension'

Defined Under Namespace

Classes: EncodeCommand, EncodingsCommand, VersionCommand

Constant Summary collapse

VERSION =
'1.3.0'
BREAK_BY_SYSTEM =
{
  :unix => "\n",
  :windows => "\r\n",
  :mac => "\r"
}
SYSTEM_BY_BREAK =
BREAK_BY_SYSTEM.invert
BREAK_REGEXP =
/(\r\n|[\r\n])/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.encode(input, system_or_replacement = :unix) ⇒ Object

Create a copy of a string with all the string’s linebreaks replaced by linebreaks of a specific system or a given replacement.

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



85
86
87
88
89
90
91
# File 'lib/aef/linebreak/linebreak.rb', line 85

def self.encode(input, system_or_replacement = :unix)
  if input.respond_to?(:to_s) then input = input.to_s
  else raise ArgumentError, 'Input needs to be a string or must support to_s' end

  input.gsub(BREAK_REGEXP,
    BREAK_BY_SYSTEM[system_or_replacement] || system_or_replacement)
end

.encoding?(input, *encodings) ⇒ Boolean

Checks whether a string includes linebreaks of all the given encoding systems.

One or more systems can be given as an array or an argument list of symbols.

Returns true or false.

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/aef/linebreak/linebreak.rb', line 66

def self.encoding?(input, *encodings)
  systems = BREAK_BY_SYSTEM.keys
  
  encodings.flatten!
  encodings.each do |encoding|
    unless systems.include?(encoding)
      raise ArgumentError,
        %{Invalid encoding system. Available systems: #{systems.join(', ')}. Arguments are expected as symbols or an array of symbols.}
    end
  end

  Aef::Linebreak.encodings(input) == Set.new(encodings)
end

.encodings(input) ⇒ Object

Detects encoding systems of a string.

Returns a Set with symbols of all encoding systems existent in the string.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/aef/linebreak/linebreak.rb', line 47

def self.encodings(input)
  if input.respond_to?(:to_s) then input = input.to_s
  else raise ArgumentError, 'Input needs to be a string or must support to_s' end

  occurences = Set.new

  input.scan(BREAK_REGEXP).each do |linebreak|
    occurences << SYSTEM_BY_BREAK[linebreak.first]
  end

  occurences
end

Instance Method Details

#linebreak_encode(system_or_replacement = :unix) ⇒ Object

Create a copy of the string with all the string’s linebreaks replaced by linebreaks of a specific system or a given replacement.

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

This method is supposed to be used as a method of String.



119
120
121
# File 'lib/aef/linebreak/linebreak.rb', line 119

def linebreak_encode(system_or_replacement = :unix)
  Aef::Linebreak.encode(self, system_or_replacement)
end

#linebreak_encoding?(*encodings) ⇒ Boolean

Checks whether the string includes linebreaks of all the given encoding systems.

One or more systems can be given as an array or an argument list of symbols.

This method is supposed to be used as a method of String.

Returns:

  • (Boolean)


108
109
110
# File 'lib/aef/linebreak/linebreak.rb', line 108

def linebreak_encoding?(*encodings)
  Aef::Linebreak.encoding?(self, encodings)
end

#linebreak_encodingsObject

Detects encoding systems of the string.

Returns a Set with symbols of all encoding systems existent in the string.

This method is supposed to be used as a method of String.



98
99
100
# File 'lib/aef/linebreak/linebreak.rb', line 98

def linebreak_encodings
  Aef::Linebreak.encodings(self)
end