Class: StandaloneTypograf::Quotes::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/standalone_typograf/quotes.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, mode) ⇒ Processor

Returns a new instance of Processor.



36
37
38
39
40
41
42
43
# File 'lib/standalone_typograf/quotes.rb', line 36

def initialize(text, mode)
  @text = text
  @mode = mode
  # `tarr` means <b>text array</b>
  # this is because replaced item may contain more than 1 char (like `&laquo;`)
  # so replaced value will be placed into the one array cell.
  @tarr = text.split(//)
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



28
29
30
# File 'lib/standalone_typograf/quotes.rb', line 28

def mode
  @mode
end

#tarrObject (readonly)

Returns the value of attribute tarr.



28
29
30
# File 'lib/standalone_typograf/quotes.rb', line 28

def tarr
  @tarr
end

#textObject (readonly)

Returns the value of attribute text.



28
29
30
# File 'lib/standalone_typograf/quotes.rb', line 28

def text
  @text
end

Class Method Details

.compile(text, mode) ⇒ Object



30
31
32
33
34
# File 'lib/standalone_typograf/quotes.rb', line 30

def self.compile(text, mode)
  compiler = self.new(text, mode)
  compiler.compile
  text.replace compiler.tarr.join()
end

Instance Method Details

#compileObject



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
# File 'lib/standalone_typograf/quotes.rb', line 49

def compile
  tarr.each_with_index do |symbol, index|
    next unless symbol == Q

    # == Outer
    if outside? && open_quote?(index)
      tarr[index] = CHARS[:outer_left][mode]
      outer_open_lock!
      next
    end
    if inside? && !lock[:inner_open] && close_quote?(index)
      tarr[index] = CHARS[:outer_right][mode]
      outer_open_unlock!
      next
    end

    # == Inner
    if inside? && !lock[:inner_open] && open_quote?(index)
      tarr[index] = CHARS[:inner_left][mode]
      inner_open_lock!
      next
    end
    if inside? && lock[:inner_open] && close_quote?(index)
      tarr[index] = CHARS[:inner_right][mode]
      inner_open_unlock!
      next
    end
  end
end

#lockObject



45
46
47
# File 'lib/standalone_typograf/quotes.rb', line 45

def lock
  @lock ||= LOCK.deep_dup
end