# Path: talon/utils.py
# def get_delimiter(msg_body):
#     delimiter = RE_DELIMITER.search(msg_body)
#     if delimiter:
#         delimiter = delimiter.group()
#     else:
#         delimiter = '\n'
# 
#     return delimiter
# 
# def html_tree_to_text(tree):
#     for style in CSSSelector('style')(tree):
#         style.getparent().remove(style)
# 
#     for c in tree.xpath('//comment()'):
#         parent = c.getparent()
# 
#         # comment with no parent does not impact produced text
#         if parent is None:
#             continue
# 
#         parent.remove(c)
# 
#     text = ""
#     for el in tree.iter():
#         el_text = (el.text or '') + (el.tail or '')
#         if len(el_text) > 1:
#             if el.tag in _BLOCKTAGS + _HARDBREAKS:
#                 text += "\n"
#             if el.tag == 'li':
#                 text += "  * "
#             text += el_text.strip() + " "
# 
#             # add href to the output
#             href = el.attrib.get('href')
#             if href:
#                 text += "(%s) " % href
# 
#         if (el.tag in _HARDBREAKS and text and
#             not text.endswith("\n") and not el_text):
#             text += "\n"
# 
#     retval = _rm_excessive_newlines(text)
#     return _encode_utf8(retval)
# 
# def html_document_fromstring(s):
#     """Parse html tree from string. Return None if the string can't be parsed.
#     """
#     if isinstance(s, six.text_type):
#         s = s.encode('utf8')
#     try:
#         return html5parser.document_fromstring(s, parser=_html5lib_parser())
#     except Exception:
#         pass
# 
# Path: talon/html_quotations.py
# CHECKPOINT_PREFIX = '#!%!'
# CHECKPOINT_SUFFIX = '!%!#'
# CHECKPOINT_PATTERN = re.compile(CHECKPOINT_PREFIX + '\d+' + CHECKPOINT_SUFFIX)
# QUOTE_IDS = ['OLK_SRC_BODY_SECTION']
# RE_FWD = re.compile("^[-]+[ ]*Forwarded message[ ]*[-]+$", re.I | re.M)
# def add_checkpoint(html_note, counter):
# def delete_quotation_tags(html_note, counter, quotation_checkpoints):
# def cut_gmail_quote(html_message):
# def cut_microsoft_quote(html_message):
# def cut_by_id(html_message):
# def cut_blockquote(html_message):
# def cut_from_block(html_message):
# def cut_zimbra_quote(html_message):


import regex as re
import logging
import six
from copy import deepcopy
from lxml import html, etree
from talon.utils import (get_delimiter, html_tree_to_text,
                         html_document_fromstring)
from talon import html_quotations
from six.moves import range
    """
    Corrects markers by removing splitlines deemed to be inside header blocks.
    """
    updated_markers = ""
    i = 0
    in_header_block = False
    for m in markers:
        # Only set in_header_block flag when we hit an 's' and line is a header
        if m == 's':
            if not in_header_block:
                if bool(re.search(RE_HEADER, lines[i])):
                    in_header_block = True
            else:
                if QUOT_PATTERN.match(lines[i]):
                    m = 'm'
                else:
                    m = 't'

        # If the line is not a header line, set in_header_block false.
        if not bool(re.search(RE_HEADER, lines[i])):
            in_header_block = False

        # Add the marker to the new updated markers string.
        updated_markers += m
        i += 1

    return updated_markers


def _readable_text_empty(html_tree):

# Path: talon/utils.py
# def get_delimiter(msg_body):
#     delimiter = RE_DELIMITER.search(msg_body)
#     if delimiter:
#         delimiter = delimiter.group()
#     else:
#         delimiter = '\n'
# 
#     return delimiter
# 
# def html_tree_to_text(tree):
#     for style in CSSSelector('style')(tree):
#         style.getparent().remove(style)
# 
#     for c in tree.xpath('//comment()'):
#         parent = c.getparent()
# 
#         # comment with no parent does not impact produced text
#         if parent is None:
#             continue
# 
#         parent.remove(c)
# 
#     text = ""
#     for el in tree.iter():
#         el_text = (el.text or '') + (el.tail or '')
#         if len(el_text) > 1:
#             if el.tag in _BLOCKTAGS + _HARDBREAKS:
#                 text += "\n"
#             if el.tag == 'li':
#                 text += "  * "
#             text += el_text.strip() + " "
# 
#             # add href to the output
#             href = el.attrib.get('href')
#             if href:
#                 text += "(%s) " % href
# 
#         if (el.tag in _HARDBREAKS and text and
#             not text.endswith("\n") and not el_text):
#             text += "\n"
# 
#     retval = _rm_excessive_newlines(text)
#     return _encode_utf8(retval)
# 
# def html_document_fromstring(s):
#     """Parse html tree from string. Return None if the string can't be parsed.
#     """
#     if isinstance(s, six.text_type):
#         s = s.encode('utf8')
#     try:
#         return html5parser.document_fromstring(s, parser=_html5lib_parser())
#     except Exception:
#         pass
# 
# Path: talon/html_quotations.py
# CHECKPOINT_PREFIX = '#!%!'
# CHECKPOINT_SUFFIX = '!%!#'
# CHECKPOINT_PATTERN = re.compile(CHECKPOINT_PREFIX + '\d+' + CHECKPOINT_SUFFIX)
# QUOTE_IDS = ['OLK_SRC_BODY_SECTION']
# RE_FWD = re.compile("^[-]+[ ]*Forwarded message[ ]*[-]+$", re.I | re.M)
# def add_checkpoint(html_note, counter):
# def delete_quotation_tags(html_note, counter, quotation_checkpoints):
# def cut_gmail_quote(html_message):
# def cut_microsoft_quote(html_message):
# def cut_by_id(html_message):
# def cut_blockquote(html_message):
# def cut_from_block(html_message):
# def cut_zimbra_quote(html_message):


# Path: talon/quotations.py
import regex as re
import logging
import six
from copy import deepcopy
from lxml import html, etree
from talon.utils import (get_delimiter, html_tree_to_text,
                         html_document_fromstring)
from talon import html_quotations
from six.moves import range

    """
    Corrects markers by removing splitlines deemed to be inside header blocks.
    """
    updated_markers = ""
    i = 0
    in_header_block = False
    for m in markers:
        # Only set in_header_block flag when we hit an 's' and line is a header
        if m == 's':
            if not in_header_block:
                if bool(re.search(RE_HEADER, lines[i])):
                    in_header_block = True
            else:
                if QUOT_PATTERN.match(lines[i]):
                    m = 'm'
                else:
                    m = 't'

        # If the line is not a header line, set in_header_block false.
        if not bool(re.search(RE_HEADER, lines[i])):
            in_header_block = False

        # Add the marker to the new updated markers string.
        updated_markers += m
        i += 1

    return updated_markers


def _readable_text_empty(html_tree):

    return not bool(html_tree_to_text(html_tree).strip())