class StandaloneTypograf::Quotes::Processor

Attributes

mode[R]
tarr[R]
text[R]

Public Class Methods

compile(text, mode) click to toggle source
# 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
new(text, mode) click to toggle source
# 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

Public Instance Methods

compile() click to toggle source
# 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
lock() click to toggle source
# File lib/standalone_typograf/quotes.rb, line 45
def lock
  @lock ||= LOCK.deep_dup
end

Private Instance Methods

close_quote?(index) click to toggle source

@return [Boolean] If this quote is close

# File lib/standalone_typograf/quotes.rb, line 90
def close_quote?(index)
  return true if index == (@tarr.length-1)
  (text[index-1] =~ /[[:alpha:]]|[?!."]/) && (text[index+1] =~ /\s|[,.;!?#{Q}]/)
end
inner_open_lock!() click to toggle source
# File lib/standalone_typograf/quotes.rb, line 117
def inner_open_lock!
  lock[:inner_open] = true
end
inner_open_unlock!() click to toggle source
# File lib/standalone_typograf/quotes.rb, line 121
def inner_open_unlock!
  lock[:inner_open] = false
end
inside?() click to toggle source

@return [Boolean] If we inside of outer quotes

# File lib/standalone_typograf/quotes.rb, line 103
def inside?
  !outside?
end
open_quote?(index) click to toggle source

@return [Boolean] If this quote is open

# File lib/standalone_typograf/quotes.rb, line 83
def open_quote?(index)
  return true if index == 0
  (text[index-1] =~ /\s|^/) && (text[index+1] =~ /[[:alpha:]]/)
end
outer_open_lock!() click to toggle source
# File lib/standalone_typograf/quotes.rb, line 107
def outer_open_lock!
  lock[:inside]     = true
  lock[:outer_open] = true
end
outer_open_unlock!() click to toggle source
# File lib/standalone_typograf/quotes.rb, line 112
def outer_open_unlock!
  lock[:inside]     = false
  lock[:outer_open] = false
end
outside?() click to toggle source

@return [Boolean] If we outside of outer quotes

# File lib/standalone_typograf/quotes.rb, line 97
def outside?
  !lock[:outer_open] && !lock[:inside]
end