code: def extract_with_context(lst, pred, before_context, after_context):
      
      rval = []
  
      start = 0
      length = 0
      while start < len(lst):
          usedfirst = False
          usedlast = False
          # Extend to the next match.
          while start + length < len(lst) and length < before_context + 1 \
                  and not pred(lst[start + length]):
              length += 1
  
          # Slide to the next match.
          while start + length < len(lst) and not pred(lst[start + length]):
              start += 1
  
          # If there was no next match, abort here (it's easier
          # to do this than to try to detect this case later).
          if start + length == len(lst):
              break
  
          # Now extend repeatedly until we can't find anything.
          while start + length < len(lst) and pred(lst[start + length]):
              extendlength = 1
              # Read in the 'after' context and see if it holds a URL.
              while extendlength < after_context + 1 and start + length + \
                      extendlength < len(lst) and \
                      not pred(lst[start + length + extendlength]):
                  extendlength += 1
              length += extendlength
              if start + length < len(lst) and not pred(lst[start + length]):
                  # Didn't find a matching line, so we either
                  # hit the end or extended to after_context + 1..
                  #
                  # Now read in possible 'before' context
                  # from the next URL; if we don't find one,
                  # we discard the readahead.
                  extendlength = 1
                  while extendlength < before_context and start + length + \
                          extendlength < len(lst) and \
                          not pred(lst[start + length + extendlength]):
                      extendlength += 1
                  if start + length + extendlength < len(lst) and \
                          pred(lst[start + length + extendlength]):
                      length += extendlength
  
          if length > 0 and start + length <= len(lst):
              if start == 0:
                  usedfirst = True
              if start + length == len(lst):
                  usedlast = True
              rval.append((lst[start:start + length], usedfirst, usedlast))
  
          start += length
          length = 0
      return rval
docs: Extract URL and context from a given chunk.