# Path: breadability/_compat.py
# PY3 = version_info[0] == 3
# def unicode_compatible(cls):
# def to_string(object):
# def to_bytes(object):
# def to_unicode(object):
# def instance_to_bytes(instance):
# def instance_to_unicode(instance):
# 
# Path: breadability/document.py
# def convert_breaks_to_paragraphs(html):
#     """
#     Converts <hr> tag and multiple <br> tags into paragraph.
#     """
#     logger.debug("Converting multiple <br> & <hr> tags into <p>.")
# 
#     return BREAK_TAGS_PATTERN.sub(_replace_break_tags, html)
# 
# def decode_html(html):
#     """
#     Converts bytes stream containing an HTML page into Unicode.
#     Tries to guess character encoding from meta tag of by "chardet" library.
#     """
#     if isinstance(html, unicode):
#         return html
# 
#     match = CHARSET_META_TAG_PATTERN.search(html)
#     if match:
#         declared_encoding = match.group(1).decode("ASCII")
#         # proceed unknown encoding as if it wasn't found at all
#         with ignored(LookupError):
#             return html.decode(declared_encoding, "ignore")
# 
#     # try to enforce UTF-8 firstly
#     with ignored(UnicodeDecodeError):
#         return html.decode("utf8")
# 
#     text = TAG_MARK_PATTERN.sub(to_bytes(" "), html)
#     diff = text.decode("utf8", "ignore").encode("utf8")
#     sizes = len(diff), len(text)
# 
#     # 99% of text is UTF-8
#     if abs(len(text) - len(diff)) < max(sizes) * 0.01:
#         return html.decode("utf8", "ignore")
# 
#     # try detect encoding
#     encoding = "utf8"
#     encoding_detector = chardet.detect(text)
#     if encoding_detector["encoding"]:
#         encoding = encoding_detector["encoding"]
# 
#     return html.decode(encoding, "ignore")
# 
# class OriginalDocument(object):
#     """The original document to process."""
# 
#     def __init__(self, html, url=None):
#         self._html = html
#         self._url = url
# 
#     @property
#     def url(self):
#         """Source URL of HTML document."""
#         return self._url
# 
#     def __unicode__(self):
#         """Renders the document as a string."""
#         return tounicode(self.dom)
# 
#     @cached_property
#     def dom(self):
#         """Parsed HTML document from the input."""
#         html = self._html
#         if not isinstance(html, unicode):
#             html = decode_html(html)
# 
#         html = convert_breaks_to_paragraphs(html)
#         document = build_document(html, self._url)
# 
#         return document
# 
#     @cached_property
#     def links(self):
#         """Links within the document."""
#         return self.dom.findall(".//a")
# 
#     @cached_property
#     def title(self):
#         """Title attribute of the parsed document."""
#         title_element = self.dom.find(".//title")
#         if title_element is None or title_element.text is None:
#             return ""
#         else:
#             return title_element.text.strip()
# 
# Path: tests/utils.py
# def load_snippet(file_name):
#     """Helper to fetch in the content of a test snippet."""
#     file_path = join(TEST_DIR, "data/snippets", file_name)
#     with open(file_path, "rb") as file:
#         return file.read()


from collections import defaultdict
from breadability._compat import (
    to_unicode,
    to_bytes,
    unicode,
)
from breadability.document import (
    convert_breaks_to_paragraphs,
    decode_html,
    OriginalDocument,
)
from .utils import load_snippet




def test_convert_br_tags_to_paragraphs():
    returned = convert_breaks_to_paragraphs(
        ("<div>HI<br><br>How are you?<br><br> \t \n  <br>"
         "Fine\n I guess</div>"))

    assert returned == "<div>HI</p><p>How are you?</p><p>Fine\n I guess</div>"


def test_convert_hr_tags_to_paragraphs():
    returned = convert_breaks_to_paragraphs(
        "<div>HI<br><br>How are you?<hr/> \t \n  <br>Fine\n I guess</div>")

    assert returned == "<div>HI</p><p>How are you?</p><p>Fine\n I guess</div>"


def test_readin_min_document():
    """Verify we can read in a min html document"""
    doc = OriginalDocument(load_snippet('document_min.html'))

    assert to_unicode(doc).startswith('<html>')
    assert doc.title == 'Min Document Title'


def test_readin_with_base_url():
    """Passing a url should update links to be absolute links"""
    doc = OriginalDocument(

# Path: breadability/_compat.py
# PY3 = version_info[0] == 3
# def unicode_compatible(cls):
# def to_string(object):
# def to_bytes(object):
# def to_unicode(object):
# def instance_to_bytes(instance):
# def instance_to_unicode(instance):
# 
# Path: breadability/document.py
# def convert_breaks_to_paragraphs(html):
#     """
#     Converts <hr> tag and multiple <br> tags into paragraph.
#     """
#     logger.debug("Converting multiple <br> & <hr> tags into <p>.")
# 
#     return BREAK_TAGS_PATTERN.sub(_replace_break_tags, html)
# 
# def decode_html(html):
#     """
#     Converts bytes stream containing an HTML page into Unicode.
#     Tries to guess character encoding from meta tag of by "chardet" library.
#     """
#     if isinstance(html, unicode):
#         return html
# 
#     match = CHARSET_META_TAG_PATTERN.search(html)
#     if match:
#         declared_encoding = match.group(1).decode("ASCII")
#         # proceed unknown encoding as if it wasn't found at all
#         with ignored(LookupError):
#             return html.decode(declared_encoding, "ignore")
# 
#     # try to enforce UTF-8 firstly
#     with ignored(UnicodeDecodeError):
#         return html.decode("utf8")
# 
#     text = TAG_MARK_PATTERN.sub(to_bytes(" "), html)
#     diff = text.decode("utf8", "ignore").encode("utf8")
#     sizes = len(diff), len(text)
# 
#     # 99% of text is UTF-8
#     if abs(len(text) - len(diff)) < max(sizes) * 0.01:
#         return html.decode("utf8", "ignore")
# 
#     # try detect encoding
#     encoding = "utf8"
#     encoding_detector = chardet.detect(text)
#     if encoding_detector["encoding"]:
#         encoding = encoding_detector["encoding"]
# 
#     return html.decode(encoding, "ignore")
# 
# class OriginalDocument(object):
#     """The original document to process."""
# 
#     def __init__(self, html, url=None):
#         self._html = html
#         self._url = url
# 
#     @property
#     def url(self):
#         """Source URL of HTML document."""
#         return self._url
# 
#     def __unicode__(self):
#         """Renders the document as a string."""
#         return tounicode(self.dom)
# 
#     @cached_property
#     def dom(self):
#         """Parsed HTML document from the input."""
#         html = self._html
#         if not isinstance(html, unicode):
#             html = decode_html(html)
# 
#         html = convert_breaks_to_paragraphs(html)
#         document = build_document(html, self._url)
# 
#         return document
# 
#     @cached_property
#     def links(self):
#         """Links within the document."""
#         return self.dom.findall(".//a")
# 
#     @cached_property
#     def title(self):
#         """Title attribute of the parsed document."""
#         title_element = self.dom.find(".//title")
#         if title_element is None or title_element.text is None:
#             return ""
#         else:
#             return title_element.text.strip()
# 
# Path: tests/utils.py
# def load_snippet(file_name):
#     """Helper to fetch in the content of a test snippet."""
#     file_path = join(TEST_DIR, "data/snippets", file_name)
#     with open(file_path, "rb") as file:
#         return file.read()


# Path: tests/test_orig_document.py
from collections import defaultdict
from breadability._compat import (
    to_unicode,
    to_bytes,
    unicode,
)
from breadability.document import (
    convert_breaks_to_paragraphs,
    decode_html,
    OriginalDocument,
)
from .utils import load_snippet





def test_convert_br_tags_to_paragraphs():
    returned = convert_breaks_to_paragraphs(
        ("<div>HI<br><br>How are you?<br><br> \t \n  <br>"
         "Fine\n I guess</div>"))

    assert returned == "<div>HI</p><p>How are you?</p><p>Fine\n I guess</div>"


def test_convert_hr_tags_to_paragraphs():
    returned = convert_breaks_to_paragraphs(
        "<div>HI<br><br>How are you?<hr/> \t \n  <br>Fine\n I guess</div>")

    assert returned == "<div>HI</p><p>How are you?</p><p>Fine\n I guess</div>"


def test_readin_min_document():
    """Verify we can read in a min html document"""
    doc = OriginalDocument(load_snippet('document_min.html'))

    assert to_unicode(doc).startswith('<html>')
    assert doc.title == 'Min Document Title'


def test_readin_with_base_url():
    """Passing a url should update links to be absolute links"""
    doc = OriginalDocument(

        load_snippet('document_absolute_url.html'),