./lib/overload/string.rb in lux-fw-0.5.37 vs ./lib/overload/string.rb in lux-fw-0.6.2

- old
+ new

@@ -1,63 +1,109 @@ class String def constantize - Object.const_get('::'+self) + Object.const_get('::' + self) end - # simple markdown - def as_html - self.gsub($/, '<br />') + # 'User'.constantize? # User + # 'UserFoo'.constantize? # nil + def constantize? + Object.const_defined?('::' + self) ? constantize : nil end - # convert escaped strings, remove scritpts - def to_html opts={} - value = self.gsub(/&lt;/, '<').gsub(/&gt;/, '>').gsub(/&amp;/,'&') - value = value.gsub(/<script/,'&lt;script') unless opts[:script] - value = value.gsub(/<link/,'&lt;link') unless opts[:link] - value + # prepare data for storage write, to make it safe to dump on screen without unescape + def html_escape display = false + # .gsub('<', '&lt;').gsub('>', '&gt;') + # .gsub("'", '&#39').gsub('"', '&#34') + self + .gsub('<', display ? '&lt;' : '#LT;') + .gsub(/\A^\s+|\s+\z/,'') end - def trim(len) + # restore original before storage read + def html_unsafe full = false + if full + self + .gsub('#LT;', '<') + .gsub('$LT;', '<') + .gsub('&lt;', '<') + .gsub('&gt;', '>') + .gsub('&#39', "'") + .gsub('&#34', '"') + else + self.gsub('#LT;', '<') + end + end + + # export html without scripts and styles + def html_safe full = false + html_unsafe(full) + .gsub(/<(\/?script)/i,'&lt;\1') + .gsub(/<(\/?style)/i,'&lt;\1') + end + + # simple markdown + def as_html + self + .gsub($/, '<br />') + .gsub(/(https?:\/\/[^\s<]+)/) { %[<a href="#{$1}">#{$1.trim(40)}</a>] } + end + + def trim len return self if self.length<len data = self.dup[0,len]+'&hellip;' data end def first self[0,1] end + def last num = 1 + len = self.length + self[len-num, len] + end + + # https://github.com/rgrove/sanitize def sanitize Sanitize.clean(self, :elements=>%w[span ul ol li b bold i italic u underline hr br p], :attributes=>{'span'=>['style']} ) end + def quick_sanitize + out = self.gsub('<!--{tag}-->', '') + out = out.gsub(/\sstyle="([^"]+)"/) do + $1.start_with?('text-align:') ? $1 : '' + end + out + end + def wrap node_name, opts={} return self unless node_name opts.tag(node_name, self) end def fix_ut8 self.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '?') end - def parse_erb - self.gsub(/<%=([^%]+)%>/) { eval $1; } + def parse_erb scope = nil + ERB.new(self).result(scope || binding) end def parameterize - self.downcase.gsub(/[^\w]+/,'-') - end - - def to_url str_from = 'šđč枊ĐČĆŽäÄéeöÖüüÜß' str_to = 'sdcczSDCCZaAeeoOuuUs' - str = self.downcase.gsub(/\s+/,'-').tr(str_from, str_to) - # self.downcase.gsub(/\s+/,'-').tr(str_from, str_to).gsub(/[^\w\-]/,'') - str.sub(/\.$/, '').gsub('&',' and ').gsub('.',' dot ').parameterize.gsub('-dot-','.').downcase[0, 50].sub(/[\.\-]$/,'') + + self + .tr(str_from, str_to) + .sub(/^[^\w+]/, '') + .sub(/[^\w+]$/, '') + .downcase + .gsub(/[^\w+]+/,'-')[0, 50] end + alias :to_url :parameterize - def css_to_hash + def qs_to_hash self.split('&').inject({}) do |h,line| el = line.split('=', 2) h[el[0]] = el[1] h end @@ -65,21 +111,25 @@ def to_a self.split(/\s*,\s*/) end + def attribute_safe + self.gsub('"', '').gsub("'", '') + end + + def db_safe + self.gsub(/[^0-9a-zA-Z_]/, '') + end + def starts_with? prefix prefix.respond_to?(:to_str) && self[0, prefix.length] == prefix end def ends_with? suffix + return true if suffix == self suffix.is_a?(String) && self[-suffix.length, suffix.length] == suffix && self != suffix - end - - def last(num=1) - len = self.length - self[len-num, len] end def span_green %[<span style="color: #080;">#{self}</span>] end @@ -92,12 +142,35 @@ def decolorize self.gsub(/\[0;\d\d;\d\dm([^\[]*)\[0m/) { $1 } end def escape - CGI::escape self + CGI::escape(self).gsub('+', '%20') end def unescape CGI::unescape self end + + def sha1 + Digest::SHA1.hexdigest self + end + + def md5 + Digest::MD5.hexdigest self + end + + def extract_scripts! list: false + scripts = [] + self.gsub!(/<script\b[^>]*>(.*?)<\/script>/im) { scripts.push $1; '' } + list ? scripts : scripts.map{ "<script>#{_1}</script>"}.join($/) + end + + def to_slug len = 80 + self.downcase.gsub(/[^\w]+/, '_').gsub(/_+/, '-').sub(/\-$/, '')[0, len] + end + + def remove_tags + self.gsub(/<[^>]+>/, '') + end + end