class Squib::Args::Typographer

Internal class for handling arguments

Public Class Methods

new(config = Squib::Conf::DEFAULTS) click to toggle source
# File lib/squib/args/typographer.rb, line 7
def initialize(config = Squib::Conf::DEFAULTS)
%w(lsquote ldquote rsquote rdquote smart_quotes
   em_dash en_dash ellipsis).each do |var|
    instance_variable_set("@#{var}", config[var])
  end
end

Public Instance Methods

apostraphize(str) click to toggle source

A quote between two letters is an apostraphe

# File lib/squib/args/typographer.rb, line 69
def apostraphize(str)
  str.gsub(/(\w)(\')(\w)/, '\1' + @rsquote + '\3')
end
each_non_tag(str) { |token| ... } click to toggle source

Iterate over each non-tag for processing Allows us to ignore anything inside < and >

# File lib/squib/args/typographer.rb, line 45
def each_non_tag(str)
  full_str = ''
  tag_delimit = /(<(?:(?!<).)*>)/ # use non-capturing group w/ negative lookahead
  str.split(tag_delimit).each do |token|
    if token.start_with? '<'
      full_str << token # don't process tags
    else
      full_str << yield(token)
    end
  end
  return full_str
end
ellipsificate(str) click to toggle source

Straightforward replace

# File lib/squib/args/typographer.rb, line 74
def ellipsificate(str)
  str.gsub('...', @ellipsis)
end
em_dash(str) click to toggle source

Straightforward replace

# File lib/squib/args/typographer.rb, line 84
def em_dash(str)
  str.gsub('---', @em_dash)
end
en_dash(str) click to toggle source

Straightforward replace

# File lib/squib/args/typographer.rb, line 79
def en_dash(str)
  str.gsub('--', @en_dash)
end
explicit_replacements(str) click to toggle source
# File lib/squib/args/typographer.rb, line 20
def explicit_replacements(str)
  [ :left_curly, :right_curly, :apostraphize,
    :ellipsificate, :em_dash, :en_dash ].each do |sym|
    str = each_non_tag(str) do |token|
      self.method(sym).call(token)
    end
  end
  str
end
left_curly(str) click to toggle source

Straightforward replace

# File lib/squib/args/typographer.rb, line 59
def left_curly(str)
  str.gsub('``', @ldquote)
end
left_double_quote(str) click to toggle source

Quote next to non-whitespace curls

# File lib/squib/args/typographer.rb, line 94
def left_double_quote(str)
  str.gsub(/(\")(\S)/, @ldquote + '\2')
end
left_single_quote(str) click to toggle source

Quote next to non-whitespace curls

# File lib/squib/args/typographer.rb, line 110
def left_single_quote(str)
  str.gsub(/(\')(\S)/, @lsquote + '\2')
end
process(str) click to toggle source
# File lib/squib/args/typographer.rb, line 14
def process(str)
  str = explicit_replacements(str.to_s)
  str = smart_quotes(str) if @smart_quotes
  str
end
right_curly(str) click to toggle source

Straightforward replace

# File lib/squib/args/typographer.rb, line 64
def right_curly(str)
  str.gsub(%{''}, @rdquote)
end
right_double_quote(str) click to toggle source

Quote next to non-whitespace curls

# File lib/squib/args/typographer.rb, line 89
def right_double_quote(str)
  str.gsub(/(\S)(\")/, '\1' + @rdquote)
end
right_single_quote(str) click to toggle source

Quote next to non-whitespace curls

# File lib/squib/args/typographer.rb, line 105
def right_single_quote(str)
  str.gsub(/(\S)(\')/, '\1' + @rsquote)
end
single_inside_double_quote(str) click to toggle source

Handle the cases where a double quote is next to a single quote

# File lib/squib/args/typographer.rb, line 99
def single_inside_double_quote(str)
  str.gsub(/(\")(\')(\S)/, @ldquote + @lsquote + '\3')
     .gsub(/(\")(\')(\S)/, '\1' + @rsquote + @rdquote)
end
smart_quotes(str) click to toggle source
# File lib/squib/args/typographer.rb, line 30
def smart_quotes(str)
  [ :single_inside_double_quote,
    :right_double_quote,
    :left_double_quote,
    :right_single_quote,
    :left_single_quote].each do |sym|
    str = each_non_tag(str) do |token|
      self.method(sym).call(token)
    end
  end
  str
end