# Path: oembed/constants.py
# OEMBED_BLOCK_ELEMENTS = [
#     'address', 'blockquote', 'center', 'dir', 'div', 'dl', 'fieldset', 'form', 
#     'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'isindex', 'menu', 'noframes', 
#     'noscript', 'ol', 'p', 'pre', 'table', 'ul', 'dd', 'dt', 'frameset', 'li', 
#     'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'button', 'del', 'iframe',
#     'ins', 'map', 'object', 'script', '[document]'
# ]
# 
# URL_RE = re.compile(URL_PATTERN)
# 
# STANDALONE_URL_RE = re.compile('^\s*' + URL_PATTERN + '\s*$')
# 
# Path: oembed/parsers/text.py
# class TextParser(TextBlockParser):
#     def parse_data(self, text, maxwidth, maxheight, template_dir, context, 
#                    urlize_all_links):
#         """
#         Parses a block of text rendering links that occur on their own line
#         normally but rendering inline links using a special template dir
#         """
#         block_parser = TextBlockParser()
#         
#         lines = text.splitlines()
#         parsed = []
#         
#         for line in lines:
#             if STANDALONE_URL_RE.match(line):
#                 user_url = line.strip()
#                 try:
#                     resource = oembed.site.embed(user_url, maxwidth=maxwidth, maxheight=maxheight)
#                     context['minwidth'] = min(maxwidth, resource.width)
#                     context['minheight'] = min(maxheight, resource.height)
#                 except OEmbedException:
#                     if urlize_all_links:
#                         line = '<a href="%(LINK)s">%(LINK)s</a>' % {'LINK': user_url}
#                 else:
#                     context['minwidth'] = min(maxwidth, resource.width)
#                     context['minheight'] = min(maxheight, resource.height)
#                     
#                     line = self.render_oembed(
#                         resource, 
#                         user_url, 
#                         template_dir=template_dir, 
#                         context=context)
#             else:
#                 line = block_parser.parse(line, maxwidth, maxheight, 'inline',
#                                           context, urlize_all_links)
#             
#             parsed.append(line)
#         
#         return mark_safe('\n'.join(parsed))
# 
# class TextBlockParser(BaseParser):
#     def parse_data(self, text, maxwidth, maxheight, template_dir, context,
#                    urlize_all_links):
#         """
#         Parses a block of text indiscriminately
#         """
#         # create a dictionary of user urls -> rendered responses
#         replacements = {}
#         user_urls = set(re.findall(URL_RE, text))
#         
#         for user_url in user_urls:
#             try:
#                 resource = oembed.site.embed(user_url, maxwidth=maxwidth, maxheight=maxheight)
#             except OEmbedException:
#                 if urlize_all_links:
#                     replacements[user_url] = '<a href="%(LINK)s">%(LINK)s</a>' % {'LINK': user_url}
#             else:
#                 context['minwidth'] = min(maxwidth, resource.width)
#                 context['minheight'] = min(maxheight, resource.height)
#                 
#                 replacement = self.render_oembed(
#                     resource, 
#                     user_url, 
#                     template_dir=template_dir, 
#                     context=context
#                 )
#                 replacements[user_url] = replacement.strip()
#         
#         # go through the text recording URLs that can be replaced
#         # taking note of their start & end indexes
#         user_urls = re.finditer(URL_RE, text)
#         matches = []
#         for match in user_urls:
#             if match.group() in replacements:
#                 matches.append([match.start(), match.end(), match.group()])
#         
#         # replace the URLs in order, offsetting the indices each go
#         for indx, (start, end, user_url) in enumerate(matches):
#             replacement = replacements[user_url]
#             difference = len(replacement) - len(user_url)
#             
#             # insert the replacement between two slices of text surrounding the
#             # original url
#             text = text[:start] + replacement + text[end:]
#             
#             # iterate through the rest of the matches offsetting their indices
#             # based on the difference between replacement/original
#             for j in xrange(indx + 1, len(matches)):
#                 matches[j][0] += difference
#                 matches[j][1] += difference
#         return mark_safe(text)
#     
#     def extract_urls(self, text):
#         urls = set()
#         url_list = []
#         for url in re.findall(URL_RE, text):
#             if url not in urls:
#                 urls.add(url)
#                 url_list.append(url)
#         
#         return url_list


import re
import oembed
from BeautifulSoup import BeautifulSoup # use BS to parse HTML (it's easy!)
from oembed.constants import OEMBED_BLOCK_ELEMENTS, URL_RE, STANDALONE_URL_RE
from oembed.exceptions import OEmbedException
from oembed.parsers.base import BaseParser
from oembed.parsers.text import TextParser, TextBlockParser
                    maxwidth,
                    maxheight,
                    template_dir,
                    context,
                    urlize_all_links
                )
                user_url.replaceWith(replacement)
        
        return unicode(soup)
    
    def is_standalone(self, soupie):
        if re.match(STANDALONE_URL_RE, soupie):
            if soupie.parent.name in OEMBED_BLOCK_ELEMENTS:
                return True
        return False
    
    def inside_a(self, soupie):
        parent = soupie.parent
        while parent is not None:
            if parent.name == 'a':
                return True
            parent = parent.parent
        return False
    
    def extract_urls(self, text):
        block_parser = TextBlockParser()
        soup = BeautifulSoup(text)
        urls = set()
        url_list = []


# Path: oembed/constants.py
# OEMBED_BLOCK_ELEMENTS = [
#     'address', 'blockquote', 'center', 'dir', 'div', 'dl', 'fieldset', 'form', 
#     'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'isindex', 'menu', 'noframes', 
#     'noscript', 'ol', 'p', 'pre', 'table', 'ul', 'dd', 'dt', 'frameset', 'li', 
#     'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'button', 'del', 'iframe',
#     'ins', 'map', 'object', 'script', '[document]'
# ]
# 
# URL_RE = re.compile(URL_PATTERN)
# 
# STANDALONE_URL_RE = re.compile('^\s*' + URL_PATTERN + '\s*$')
# 
# Path: oembed/parsers/text.py
# class TextParser(TextBlockParser):
#     def parse_data(self, text, maxwidth, maxheight, template_dir, context, 
#                    urlize_all_links):
#         """
#         Parses a block of text rendering links that occur on their own line
#         normally but rendering inline links using a special template dir
#         """
#         block_parser = TextBlockParser()
#         
#         lines = text.splitlines()
#         parsed = []
#         
#         for line in lines:
#             if STANDALONE_URL_RE.match(line):
#                 user_url = line.strip()
#                 try:
#                     resource = oembed.site.embed(user_url, maxwidth=maxwidth, maxheight=maxheight)
#                     context['minwidth'] = min(maxwidth, resource.width)
#                     context['minheight'] = min(maxheight, resource.height)
#                 except OEmbedException:
#                     if urlize_all_links:
#                         line = '<a href="%(LINK)s">%(LINK)s</a>' % {'LINK': user_url}
#                 else:
#                     context['minwidth'] = min(maxwidth, resource.width)
#                     context['minheight'] = min(maxheight, resource.height)
#                     
#                     line = self.render_oembed(
#                         resource, 
#                         user_url, 
#                         template_dir=template_dir, 
#                         context=context)
#             else:
#                 line = block_parser.parse(line, maxwidth, maxheight, 'inline',
#                                           context, urlize_all_links)
#             
#             parsed.append(line)
#         
#         return mark_safe('\n'.join(parsed))
# 
# class TextBlockParser(BaseParser):
#     def parse_data(self, text, maxwidth, maxheight, template_dir, context,
#                    urlize_all_links):
#         """
#         Parses a block of text indiscriminately
#         """
#         # create a dictionary of user urls -> rendered responses
#         replacements = {}
#         user_urls = set(re.findall(URL_RE, text))
#         
#         for user_url in user_urls:
#             try:
#                 resource = oembed.site.embed(user_url, maxwidth=maxwidth, maxheight=maxheight)
#             except OEmbedException:
#                 if urlize_all_links:
#                     replacements[user_url] = '<a href="%(LINK)s">%(LINK)s</a>' % {'LINK': user_url}
#             else:
#                 context['minwidth'] = min(maxwidth, resource.width)
#                 context['minheight'] = min(maxheight, resource.height)
#                 
#                 replacement = self.render_oembed(
#                     resource, 
#                     user_url, 
#                     template_dir=template_dir, 
#                     context=context
#                 )
#                 replacements[user_url] = replacement.strip()
#         
#         # go through the text recording URLs that can be replaced
#         # taking note of their start & end indexes
#         user_urls = re.finditer(URL_RE, text)
#         matches = []
#         for match in user_urls:
#             if match.group() in replacements:
#                 matches.append([match.start(), match.end(), match.group()])
#         
#         # replace the URLs in order, offsetting the indices each go
#         for indx, (start, end, user_url) in enumerate(matches):
#             replacement = replacements[user_url]
#             difference = len(replacement) - len(user_url)
#             
#             # insert the replacement between two slices of text surrounding the
#             # original url
#             text = text[:start] + replacement + text[end:]
#             
#             # iterate through the rest of the matches offsetting their indices
#             # based on the difference between replacement/original
#             for j in xrange(indx + 1, len(matches)):
#                 matches[j][0] += difference
#                 matches[j][1] += difference
#         return mark_safe(text)
#     
#     def extract_urls(self, text):
#         urls = set()
#         url_list = []
#         for url in re.findall(URL_RE, text):
#             if url not in urls:
#                 urls.add(url)
#                 url_list.append(url)
#         
#         return url_list


# Path: oembed/parsers/html.py
import re
import oembed
from BeautifulSoup import BeautifulSoup # use BS to parse HTML (it's easy!)
from oembed.constants import OEMBED_BLOCK_ELEMENTS, URL_RE, STANDALONE_URL_RE
from oembed.exceptions import OEmbedException
from oembed.parsers.base import BaseParser
from oembed.parsers.text import TextParser, TextBlockParser

                    maxwidth,
                    maxheight,
                    template_dir,
                    context,
                    urlize_all_links
                )
                user_url.replaceWith(replacement)
        
        return unicode(soup)
    
    def is_standalone(self, soupie):
        if re.match(STANDALONE_URL_RE, soupie):
            if soupie.parent.name in OEMBED_BLOCK_ELEMENTS:
                return True
        return False
    
    def inside_a(self, soupie):
        parent = soupie.parent
        while parent is not None:
            if parent.name == 'a':
                return True
            parent = parent.parent
        return False
    
    def extract_urls(self, text):
        block_parser = TextBlockParser()
        soup = BeautifulSoup(text)
        urls = set()
        url_list = []


        for user_url in soup.findAll(text=re.compile(URL_RE)):