# 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/utils.py
# def cached_property(getter):
#     """
#     Decorator that converts a method into memoized property.
#     The decorator works as expected only for classes with
#     attribute '__dict__' and immutable properties.
#     """
#     def decorator(self):
#         key = "_cached_property_" + getter.__name__
# 
#         if not hasattr(self, key):
#             setattr(self, key, getter(self))
# 
#         return getattr(self, key)
# 
#     decorator.__name__ = getter.__name__
#     decorator.__module__ = getter.__module__
#     decorator.__doc__ = getter.__doc__
# 
#     return property(decorator)
# 
# @contextmanager
# def ignored(*exceptions):
#     try:
#         yield
#     except tuple(exceptions):
#         pass


import logging
import re
import chardet
from lxml.etree import ParserError, XMLSyntaxError, tounicode
from lxml.html import HTMLParser, document_fromstring
from ._compat import to_bytes, to_unicode, unicode, unicode_compatible
from .utils import cached_property, ignored

    # try detect encoding
    encoding = "utf8"
    encoding_detector = chardet.detect(text)
    if encoding_detector["encoding"]:
        encoding = encoding_detector["encoding"]

    return html.decode(encoding, "ignore")


BREAK_TAGS_PATTERN = re.compile(
    to_unicode(r"(?:<\s*[bh]r[^>]*>\s*)+"),
    re.IGNORECASE
)


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 _replace_break_tags(match):
    tags = match.group()

    if to_unicode("<hr") in tags:
        return to_unicode("</p><p>")

# 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/utils.py
# def cached_property(getter):
#     """
#     Decorator that converts a method into memoized property.
#     The decorator works as expected only for classes with
#     attribute '__dict__' and immutable properties.
#     """
#     def decorator(self):
#         key = "_cached_property_" + getter.__name__
# 
#         if not hasattr(self, key):
#             setattr(self, key, getter(self))
# 
#         return getattr(self, key)
# 
#     decorator.__name__ = getter.__name__
#     decorator.__module__ = getter.__module__
#     decorator.__doc__ = getter.__doc__
# 
#     return property(decorator)
# 
# @contextmanager
# def ignored(*exceptions):
#     try:
#         yield
#     except tuple(exceptions):
#         pass


# Path: breadability/document.py
import logging
import re
import chardet
from lxml.etree import ParserError, XMLSyntaxError, tounicode
from lxml.html import HTMLParser, document_fromstring
from ._compat import to_bytes, to_unicode, unicode, unicode_compatible
from .utils import cached_property, ignored


    # try detect encoding
    encoding = "utf8"
    encoding_detector = chardet.detect(text)
    if encoding_detector["encoding"]:
        encoding = encoding_detector["encoding"]

    return html.decode(encoding, "ignore")


BREAK_TAGS_PATTERN = re.compile(
    to_unicode(r"(?:<\s*[bh]r[^>]*>\s*)+"),
    re.IGNORECASE
)


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 _replace_break_tags(match):
    tags = match.group()

    if to_unicode("<hr") in tags:
        return to_unicode("</p><p>")

    elif tags.count(to_unicode("<br")) > 1: