Class: AnyStyle::Feature::Category

Inherits:
AnyStyle::Feature show all
Defined in:
lib/anystyle/feature/category.rb

Instance Attribute Summary collapse

Attributes inherited from AnyStyle::Feature

#precision

Instance Method Summary collapse

Methods inherited from AnyStyle::Feature

#next, #prev, #ratio

Methods included from StringUtils

canonize, count, display_chars, display_width, indent, nnum, page_break?, scrub, strip_html, transliterate

Constructor Details

#initialize(index: [0, -1], strip: false) ⇒ Category

Returns a new instance of Category.


6
7
8
# File 'lib/anystyle/feature/category.rb', line 6

def initialize(index: [0, -1], strip: false)
  @index, @strip = index, !!strip
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index


4
5
6
# File 'lib/anystyle/feature/category.rb', line 4

def index
  @index
end

Instance Method Details

#categorize(char) ⇒ Object


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
# File 'lib/anystyle/feature/category.rb', line 22

def categorize(char)
  case char
  when /\p{Lu}/
    :Lu
  when /\p{Ll}/
    :Ll
  when /\p{Lm}/
    :Lm
  when /\p{L}/
    :L
  when /\p{M}/
    :M
  when /\p{N}/
    :N
  when /\p{Pc}/
    :Pc
  when /\p{Pd}/
    :Pd
  when /\p{Ps}/
    :Ps
  when /\p{Pe}/
    :Pe
  when /\p{Pi}/
    :Pi
  when /\p{Pf}/
    :Pf
  when /\p{P}/
    :P
  when /\p{S}/
    :S
  when /\p{Zl}/
    :Zl
  when /\p{Zp}/
    :Zp
  when /\p{Z}/
    :Z
  when /\p{C}/
    :C
  else
    :none
  end
end

#chars(token) ⇒ Object


14
15
16
17
18
19
20
# File 'lib/anystyle/feature/category.rb', line 14

def chars(token)
  if strip?
    token.strip.chars
  else
    token.chars
  end
end

#observe(token, **opts) ⇒ Object


10
11
12
# File 'lib/anystyle/feature/category.rb', line 10

def observe(token, **opts)
  chars(token).values_at(*index).map { |char| categorize char }
end

#strip?Boolean

Returns:

  • (Boolean)

65
66
67
# File 'lib/anystyle/feature/category.rb', line 65

def strip?
  @strip
end