Class: Squib::Args::Typographer Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Internal class for handling arguments

Instance Method Summary collapse

Constructor Details

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Typographer


10
11
12
13
14
15
# File 'lib/squib/args/typographer.rb', line 10

def initialize(config = 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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A quote between two letters is an apostraphe


72
73
74
# File 'lib/squib/args/typographer.rb', line 72

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

#each_non_tag(str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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


48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/squib/args/typographer.rb', line 48

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Straightforward replace


77
78
79
# File 'lib/squib/args/typographer.rb', line 77

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

#em_dash(str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Straightforward replace


87
88
89
# File 'lib/squib/args/typographer.rb', line 87

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

#en_dash(str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Straightforward replace


82
83
84
# File 'lib/squib/args/typographer.rb', line 82

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

#explicit_replacements(str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


23
24
25
26
27
28
29
30
31
# File 'lib/squib/args/typographer.rb', line 23

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Straightforward replace


62
63
64
# File 'lib/squib/args/typographer.rb', line 62

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

#left_double_quote(str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Quote next to non-whitespace curls


97
98
99
# File 'lib/squib/args/typographer.rb', line 97

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

#left_single_quote(str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Quote next to non-whitespace curls


113
114
115
# File 'lib/squib/args/typographer.rb', line 113

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

#process(str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


17
18
19
20
21
# File 'lib/squib/args/typographer.rb', line 17

def process(str)
  str = explicit_replacements(str.to_s)
  str = smart_quotes(str) if @smart_quotes
  str
end

#right_curly(str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Straightforward replace


67
68
69
# File 'lib/squib/args/typographer.rb', line 67

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

#right_double_quote(str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Quote next to non-whitespace curls


92
93
94
# File 'lib/squib/args/typographer.rb', line 92

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

#right_single_quote(str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Quote next to non-whitespace curls


108
109
110
# File 'lib/squib/args/typographer.rb', line 108

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

#single_inside_double_quote(str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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


102
103
104
105
# File 'lib/squib/args/typographer.rb', line 102

def single_inside_double_quote(str)
  str.gsub(/(\")(\')(\S)/, @ldquote + @lsquote + '\3')
     .gsub(/(\")(\')(\S)/, '\1' + @rsquote + @rdquote)
end

#smart_quotes(str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/squib/args/typographer.rb', line 33

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