]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/core/utils/utils.rb
utils: don't hang when getting first par with nonexistant uri-fragment
[user/henk/code/ruby/rbot.git] / lib / rbot / core / utils / utils.rb
index 25e5159b928edb472dcedb1e2dbef7431bfa028d..ce5cdea4917cabafa55f88a5b8896accf1054898 100644 (file)
@@ -223,6 +223,22 @@ module ::Irc
       end
     end
 
+    # Turn a number of seconds into a hours:minutes:seconds e.g.
+    # 3:18:10 or 5'12" or 7s
+    #
+    def Utils.secs_to_short(seconds)
+      secs = seconds.to_i # make sure it's an integer
+      mins, secs = secs.divmod 60
+      hours, mins = mins.divmod 60
+      if hours > 0
+        return ("%s:%s:%s" % [hours, mins, secs])
+      elsif mins > 0
+        return ("%s'%s\"" % [mins, secs])
+      else
+        return ("%ss" % [secs])
+      end
+    end
+
 
     # Execute an external program, returning a String obtained by redirecting
     # the program's standards errors and output 
@@ -581,6 +597,18 @@ module ::Irc
       return ds.merge(cur) if cur
     end
 
+    # HTML info filters often need to check if the webpage location
+    # of a passed DataStream _ds_ matches a given Regexp.
+    def Utils.check_location(ds, rx)
+      debug ds[:headers]
+      if h = ds[:headers]
+        loc = [h['x-rbot-location'],h['location']].flatten.grep(rx)
+      end
+      loc ||= []
+      debug loc
+      return loc.empty? ? nil : loc
+    end
+
     # This method extracts title and content (first par)
     # from the given HTML or XML document _text_, using
     # standard methods (String#ircify_html_title,
@@ -590,11 +618,18 @@ module ::Irc
     # uri_fragment:: the URI fragment of the original request
     #
     def Utils.get_string_html_info(text, opts={})
+      debug "getting string html info"
       txt = text.dup
       title = txt.ircify_html_title
+      debug opts
       if frag = opts[:uri_fragment] and not frag.empty?
-        fragreg = /.*?<a\s+[^>]*name=["']?#{frag}["']?.*?>/im
-        txt.sub!(fragreg,'')
+        fragreg = /<a\s+[^>]*name=["']?#{frag}["']?[^>]*>/im
+        debug fragreg
+        debug txt
+        if txt.match(fragreg)
+          # grab the post-match
+          txt = $'
+        end
       end
       c_opts = opts.dup
       c_opts[:strip] ||= title