Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/doing/helpers.rb
Instance Method Summary collapse
- #cap_first ⇒ Object
- #dedup_tags ⇒ Object
-
#dedup_tags! ⇒ Object
Deduplicated string.
- #link_urls(opt = {}) ⇒ Object
- #link_urls!(opt = {}) ⇒ Object
- #normalize_bool(default = :and) ⇒ Object
-
#normalize_bool! ⇒ Object
Symbol :and, :or, or :not.
Instance Method Details
#cap_first ⇒ Object
74 75 76 77 78 |
# File 'lib/doing/helpers.rb', line 74 def cap_first sub(/^\w/) do |m| m.upcase end end |
#dedup_tags ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/doing/helpers.rb', line 113 def item = self.dup = item.scan(/(?<=^| )(\@(\S+?)(\([^)]+\))?)(?=\b|$)/).uniq .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.
109 110 111 |
# File 'lib/doing/helpers.rb', line 109 def replace end |
#link_urls(opt = {}) ⇒ Object
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\-.,@?^=%&:/~+#]*[\w\-@^=%&/~+#])?}) 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 |
#link_urls!(opt = {}) ⇒ Object
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.
86 87 88 |
# File 'lib/doing/helpers.rb', line 86 def normalize_bool! replace normalize_bool end |