Module: Spacifier

Defined in:
lib/spacifier.rb,
lib/spacifier/version.rb,
lib/spacifier/punctuation.rb

Defined Under Namespace

Modules: Jekyll

Constant Summary collapse

VERSION =
"1.1.2".freeze

Class Method Summary collapse

Class Method Details

.punct?(c) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/spacifier/punctuation.rb', line 5

def punct?(c)
  !(/\p{P}/.match(c) == nil)
end

.spacify(words) ⇒ Object



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
# File 'lib/spacifier.rb', line 9

def spacify(words)
  return '' unless words.is_a?(String)
  # strip words
  words.strip!
  # init vars
  new_words = ""
  last_word_type = nil # 0 for cn, 1 for en
  # iterate chars
  words.each_char do |c|
    if c == " "
      new_words << c
      last_word_type = nil
    elsif /\d/.match(c) != nil
      new_words << c
      last_word_type = nil
    elsif punct?(c)
      new_words << c
      last_word_type = nil
    elsif /\p{Han}/.match(c) == nil
      new_words << " " if last_word_type == 0
      new_words << c
      last_word_type = 1
    else
      new_words << " " if last_word_type == 1
      new_words << c
      last_word_type = 0
    end
  end

  new_words
end