File: converter.rb

package info (click to toggle)
ruby-stringex 2.8.5-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,232 kB
  • sloc: ruby: 3,745; makefile: 5
file content (129 lines) | stat: -rw-r--r-- 3,284 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# encoding: UTF-8

require 'stringex/localization/conversion_expressions'

module Stringex
  module Localization
    class Converter
      include ConversionExpressions

      attr_reader :ending_whitespace, :options, :starting_whitespace, :string

      def initialize(string, options = {})
        @string = string.dup
        @options = Stringex::Configuration::StringExtensions.default_settings.merge(options)
        string =~ /^(\s+)/
        @starting_whitespace = $1 unless $1 == ''
        string =~ /(\s+)$/
        @ending_whitespace = $1 unless $1 == ''
      end

      def cleanup_accented_html_entities!
        string.gsub! expressions.accented_html_entity, '\1'
      end

      def cleanup_characters!
        string.gsub! expressions.cleanup_characters, ' '
      end

      def cleanup_html_entities!
        string.gsub! expressions.cleanup_html_entities, ''
      end

      def cleanup_smart_punctuation!
        expressions.smart_punctuation.each do |expression, replacement|
          string.gsub! expression, replacement
        end
      end

      def normalize_currency!
        string.gsub!(/(\d+),(\d+)/, '\1\2')
      end

      def smart_strip!
        string.strip!
        @string = "#{starting_whitespace}#{string}#{ending_whitespace}"
      end

      def strip!
        string.strip!
      end

      def strip_html_tags!
        string.gsub! expressions.html_tag, ''
      end

      def translate!(*conversions)
        conversions.each do |conversion|
          send conversion
        end
      end

      protected

      def unreadable_control_characters
        string.gsub! expressions.unreadable_control_characters, ''
      end

      def abbreviations
        string.gsub! expressions.abbreviation do |x|
          x.gsub '.', ''
        end
      end

      def apostrophes
        string.gsub! expressions.apostrophe, '\1\2'
      end

      def characters
        expressions.characters.each do |key, expression|
          next if key == :slash && options[:allow_slash]
          replacement = translate(key)
          replacement = " #{replacement} " unless replacement == '' || key == :dot
          string.gsub! expression, replacement
        end
      end

      def currencies
        if has_currencies?
          [:currencies_complex, :currencies_simple].each do |type|
            expressions.send(type).each do |key, expression|
              string.gsub! expression, " #{translate(key, :currencies)} "
            end
          end
        end
      end

      def ellipses
        string.gsub! expressions.characters[:ellipsis], " #{translate(:ellipsis)} "
      end

      def html_entities
        expressions.html_entities.each do |key, expression|
          string.gsub! expression, translate(key, :html_entities)
        end
        string.squeeze! ' '
      end

      def vulgar_fractions
        expressions.vulgar_fractions.each do |key, expression|
          string.gsub! expression, translate(key, :vulgar_fractions)
        end
      end

      private

      def expressions
        ConversionExpressions
      end

      def has_currencies?
        string =~ CURRENCIES_SUPPORTED
      end

      def translate(key, scope = :characters)
        Localization.translate scope, key
      end
    end
  end
end