Class: Squib::Args::Typographer

Inherits:
Object
  • Object
show all
Defined in:
lib/squib/args/typographer.rb

Overview

Internal class for handling arguments

Instance Method Summary collapse

Constructor Details

#initialize(config = Squib::Conf::DEFAULTS) ⇒ Typographer

Returns a new instance of Typographer.



7
8
9
10
11
12
# 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

Instance Method Details

#apostraphize(str) ⇒ Object

A quote between two letters is an apostraphe



69
70
71
# File 'lib/squib/args/typographer.rb', line 69

def apostraphize(str)
  str.gsub(/(\w)(\')(\w)/, '\1' + @rsquote + '\3')
end

#each_non_tag(str) ⇒ Object

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



45
46
47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object

Straightforward replace



74
75
76
# File 'lib/squib/args/typographer.rb', line 74

def ellipsificate(str)
  str.gsub('...', @ellipsis)
end

#em_dash(str) ⇒ Object

Straightforward replace



84
85
86
# File 'lib/squib/args/typographer.rb', line 84

def em_dash(str)
  str.gsub('---', @em_dash)
end

#en_dash(str) ⇒ Object

Straightforward replace



79
80
81
# File 'lib/squib/args/typographer.rb', line 79

def en_dash(str)
  str.gsub('--', @en_dash)
end

#explicit_replacements(str) ⇒ Object



20
21
22
23
24
25
26
27
28
# 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) ⇒ Object

Straightforward replace



59
60
61
# File 'lib/squib/args/typographer.rb', line 59

def left_curly(str)
  str.gsub('``', @ldquote)
end

#left_double_quote(str) ⇒ Object

Quote next to non-whitespace curls



94
95
96
# File 'lib/squib/args/typographer.rb', line 94

def left_double_quote(str)
  str.gsub(/(\")(\S)/, @ldquote + '\2')
end

#left_single_quote(str) ⇒ Object

Quote next to non-whitespace curls



110
111
112
# File 'lib/squib/args/typographer.rb', line 110

def left_single_quote(str)
  str.gsub(/(\')(\S)/, @lsquote + '\2')
end

#process(str) ⇒ Object



14
15
16
17
18
# 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) ⇒ Object

Straightforward replace



64
65
66
# File 'lib/squib/args/typographer.rb', line 64

def right_curly(str)
  str.gsub(%{''}, @rdquote)
end

#right_double_quote(str) ⇒ Object

Quote next to non-whitespace curls



89
90
91
# File 'lib/squib/args/typographer.rb', line 89

def right_double_quote(str)
  str.gsub(/(\S)(\")/, '\1' + @rdquote)
end

#right_single_quote(str) ⇒ Object

Quote next to non-whitespace curls



105
106
107
# File 'lib/squib/args/typographer.rb', line 105

def right_single_quote(str)
  str.gsub(/(\S)(\')/, '\1' + @rsquote)
end

#single_inside_double_quote(str) ⇒ Object

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



99
100
101
102
# 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) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# 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