Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/doing/helpers.rb

Instance Method Summary collapse

Instance Method Details

#cap_firstObject



74
75
76
77
78
# File 'lib/doing/helpers.rb', line 74

def cap_first
  sub(/^\w/) do |m|
    m.upcase
  end
end

#dedup_tagsObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/doing/helpers.rb', line 113

def dedup_tags
  item = self.dup
  tags = item.scan(/(?<=^| )(\@(\S+?)(\([^)]+\))?)(?=\b|$)/).uniq
  tags.each do |tag|
    found = false
    item.gsub!(/( |^)#{tag[0]}\b/) do |m|
      if found
        ''
      else
        found = true
        m
      end
    end
  end
  item
end

#dedup_tags!Object

Returns Deduplicated string.

Returns:

  • Deduplicated string



109
110
111
# File 'lib/doing/helpers.rb', line 109

def dedup_tags!
  replace dedup_tags
end


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/doing/helpers.rb', line 139

def link_urls(opt = {})
  opt[:format] ||= :html
  str = self.dup

  if :format == :markdown
    # Remove <self-linked> formatting
    str.gsub!(/<(.*?)>/) do |match|
      m = Regexp.last_match
      if m[1] =~ /^https?:/
        m[1]
      else
        match
      end
    end
  end

  # Replace qualified urls
  str.gsub!(%r{(?mi)(?<!["'\[(\\])((http|https)://)([\w\-_]+(\.[\w\-_]+)+)([\w\-.,@?^=%&amp;:/~+#]*[\w\-@^=%&amp;/~+#])?}) do |_match|
    m = Regexp.last_match
    proto = m[1].nil? ? 'http://' : ''
    case opt[:format]
    when :html
      %(<a href="#{proto}#{m[0]}" title="Link to #{m[0]}">[#{m[3]}]</a>)
    when :markdown
      "[#{m[0]}](#{proto}#{m[0]})"
    else
      m[0]
    end
  end

  # Clean up unlinked <urls>
  str.gsub!(/<(\w+:.*?)>/) do |match|
    m = Regexp.last_match
    if m[1] =~ /<a href/
      match
    else
      %(<a href="#{m[1]}" title="Link to #{m[1]}">[link]</a>)
    end
  end

  str
end

Parameters:

  • opt (Hash) (defaults to: {})

    Additional Options



135
136
137
# File 'lib/doing/helpers.rb', line 135

def link_urls!(opt = {})
  replace link_urls(opt)
end

#normalize_bool(default = :and) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/doing/helpers.rb', line 90

def normalize_bool(default = :and)
  case self
  when /(and|all)/i
    :and
  when /(any|or)/i
    :or
  when /(not|none)/i
    :not
  else
    default.is_a?(Symbol) ? default : default.normalize_bool
  end
end

#normalize_bool!Object

Returns Symbol :and, :or, or :not.

Returns:

  • Symbol :and, :or, or :not



86
87
88
# File 'lib/doing/helpers.rb', line 86

def normalize_bool!
  replace normalize_bool
end