Module: Truty::Conversion

Included in:
Truty
Defined in:
lib/truty/conversion.rb

Overview

Module with conversion engine for plain text. Converts to HTML.

Author:

  • Matěj Kašpar Jirásek

Instance Method Summary collapse

Instance Method Details

#convert(input, conversion = :html, lang = :general, convert = [:all]) ⇒ String

Fixes the typography and also converts the string.

Parameters:

  • input (String)

    Text input.

  • conversion (Symbol) (defaults to: :html)

    Conversion type (“html” or “none”)

  • convert (Array) (defaults to: [:all])

    Array of symbols with features that should be improved (possibilities: all, hyphens, quotes, ellipsis, dashes, abbreviations, prepositions, numbers, dates, characters, brackets, multiplication, units, widows)

Returns:

  • (String)

    Fixed and converted text.



16
17
18
19
20
21
# File 'lib/truty/conversion.rb', line 16

def convert(input, conversion = :html, lang = :general, convert = [:all])
  if !Truty.respond_to? conversion then
    conversion = :none
  end
  Truty.send(conversion, Truty.fix(input, lang, convert))
end

#czech_diacriticsHash

Returns hash with Czech HTML entities as keys and values as their respective human readable versions.

Returns:

  • (Hash)

    Hash with entities.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/truty/conversion.rb', line 35

def czech_diacritics
  {
    "á" => "á",
    "č" => "č",
    "ď" => "ď",
    "é" => "é",
    "ě" => "ě",
    "í" => "í",
    "ň" => "ň",
    "ó" => "ó",
    "ř" => "ř",
    "š" => "š",
    "ť" => "ť",
    "ú" => "ú",
    "ů" => "ů",
    "ý" => "ý",
    "ž" => "ž",
  }
end

#czech_html(input) ⇒ String

Escapes string to readable Czech HTML entities.

Parameters:

  • input (String)

    Text input.

Returns:

  • (String)

    Text with HTML entities.



59
60
61
62
63
64
# File 'lib/truty/conversion.rb', line 59

def czech_html(input)
  coder = HTMLEntities.new
  encoded = coder.encode(input, :named, :decimal)
  czech_diacritics.each { |k, v| encoded.gsub!(k, v) }
  encoded
end

#html(input) ⇒ String

Escapes string to HTML entities.

Parameters:

  • input (String)

    Text input.

Returns:

  • (String)

    Text with HTML entities.



27
28
29
30
# File 'lib/truty/conversion.rb', line 27

def html(input)
  coder = HTMLEntities.new
  coder.encode(input, :named, :decimal)
end

#none(input) ⇒ String

Returns the input as it is.

Parameters:

  • input (String)

    Input for conversion.

Returns:

  • (String)

    Not changed string from input.



70
71
72
# File 'lib/truty/conversion.rb', line 70

def none(input)
  input
end