# Path: breadability/utils.py
# def is_blank(text):
#     """
#     Returns ``True`` if string contains only whitespace characters
#     or is empty. Otherwise ``False`` is returned.
#     """
#     return not text or text.isspace()
# 
# def shrink_text(text):
#     return normalize_whitespace(text.strip())
# 
# Path: breadability/_compat.py
# def to_unicode(object):
#     try:
#         if isinstance(object, unicode):
#             return object
#         elif isinstance(object, bytes):
#             return object.decode("utf8")
#         else:
#             # try decode instance to unicode
#             return instance_to_unicode(object)
#     except UnicodeError:
#         # recover from codec error and use 'repr' function
#         return to_unicode(repr(object))


from itertools import groupby
from lxml.sax import saxify, ContentHandler
from .utils import is_blank, shrink_text
from ._compat import to_unicode
    def endElementNS(self, name, qname):
        namespace, name = name

        if name == "p" and self._paragraph:
            self._append_paragraph(self._paragraph)
        elif name in ("ol", "ul", "pre") and self._paragraph:
            self._append_paragraph(self._paragraph)
            self._dom_path.pop()
        elif name in _SEMANTIC_TAGS:
            self._dom_path.pop()

    def endDocument(self):
        if self._paragraph:
            self._append_paragraph(self._paragraph)

    def _append_paragraph(self, paragraph):
        paragraph = self._process_paragraph(paragraph)
        self._content.append(paragraph)
        self._paragraph = []

    def _process_paragraph(self, paragraph):
        current_paragraph = []

        for annotation, items in groupby(paragraph, key=lambda i: i[1]):
            if annotation and "li" in annotation:
                for text, _ in items:
                    text = shrink_text(text)
                    current_paragraph.append((text, annotation))
            else:
                text = "".join(i[0] for i in items)

# Path: breadability/utils.py
# def is_blank(text):
#     """
#     Returns ``True`` if string contains only whitespace characters
#     or is empty. Otherwise ``False`` is returned.
#     """
#     return not text or text.isspace()
# 
# def shrink_text(text):
#     return normalize_whitespace(text.strip())
# 
# Path: breadability/_compat.py
# def to_unicode(object):
#     try:
#         if isinstance(object, unicode):
#             return object
#         elif isinstance(object, bytes):
#             return object.decode("utf8")
#         else:
#             # try decode instance to unicode
#             return instance_to_unicode(object)
#     except UnicodeError:
#         # recover from codec error and use 'repr' function
#         return to_unicode(repr(object))


# Path: breadability/annotated_text.py
from itertools import groupby
from lxml.sax import saxify, ContentHandler
from .utils import is_blank, shrink_text
from ._compat import to_unicode

    def endElementNS(self, name, qname):
        namespace, name = name

        if name == "p" and self._paragraph:
            self._append_paragraph(self._paragraph)
        elif name in ("ol", "ul", "pre") and self._paragraph:
            self._append_paragraph(self._paragraph)
            self._dom_path.pop()
        elif name in _SEMANTIC_TAGS:
            self._dom_path.pop()

    def endDocument(self):
        if self._paragraph:
            self._append_paragraph(self._paragraph)

    def _append_paragraph(self, paragraph):
        paragraph = self._process_paragraph(paragraph)
        self._content.append(paragraph)
        self._paragraph = []

    def _process_paragraph(self, paragraph):
        current_paragraph = []

        for annotation, items in groupby(paragraph, key=lambda i: i[1]):
            if annotation and "li" in annotation:
                for text, _ in items:
                    text = shrink_text(text)
                    current_paragraph.append((text, annotation))
            else:
                text = "".join(i[0] for i in items)

                text = shrink_text(text)