# Path: breadability/_compat.py
# def to_bytes(object):
#     try:
#         if isinstance(object, bytes):
#             return object
#         elif isinstance(object, unicode):
#             return object.encode("utf8")
#         else:
#             # try encode instance to bytes
#             return instance_to_bytes(object)
#     except UnicodeError:
#         # recover from codec error and use 'repr' function
#         return to_bytes(repr(object))
# 
# Path: breadability/utils.py
# def normalize_whitespace(text):
#     """
#     Translates multiple whitespace into single space character.
#     If there is at least one new line character chunk is replaced
#     by single LF (Unix new line) character.
#     """
#     return MULTIPLE_WHITESPACE_PATTERN.sub(_replace_whitespace, text)


import re
import logging
from hashlib import md5
from lxml.etree import tostring
from ._compat import to_bytes
from .utils import normalize_whitespace
    Computes the ratio for text in given node and text in links
    contained in the node. It is computed from number of
    characters in the texts.

    :parameter Element node:
        HTML element in which links density is computed.
    :parameter string node_text:
        Text content of given node if it was obtained before.
    :returns float:
        Returns value of computed 0 <= density <= 1, where 0 means
        no links and 1 means that node contains only links.
    """
    if node_text is None:
        node_text = node.text_content()
    node_text = normalize_whitespace(node_text.strip())

    text_length = len(node_text)
    if text_length == 0:
        return 0.0

    links_length = sum(map(_get_normalized_text_length, node.findall(".//a")))
    # Give 50 bonus chars worth of length for each img.
    # Tweaking this 50 down a notch should help if we hit false positives.
    img_bonuses = 50 * len(node.findall(".//img"))
    links_length = max(0, links_length - img_bonuses)

    return links_length / text_length


def _get_normalized_text_length(node):

# Path: breadability/_compat.py
# def to_bytes(object):
#     try:
#         if isinstance(object, bytes):
#             return object
#         elif isinstance(object, unicode):
#             return object.encode("utf8")
#         else:
#             # try encode instance to bytes
#             return instance_to_bytes(object)
#     except UnicodeError:
#         # recover from codec error and use 'repr' function
#         return to_bytes(repr(object))
# 
# Path: breadability/utils.py
# def normalize_whitespace(text):
#     """
#     Translates multiple whitespace into single space character.
#     If there is at least one new line character chunk is replaced
#     by single LF (Unix new line) character.
#     """
#     return MULTIPLE_WHITESPACE_PATTERN.sub(_replace_whitespace, text)


# Path: breadability/scoring.py
import re
import logging
from hashlib import md5
from lxml.etree import tostring
from ._compat import to_bytes
from .utils import normalize_whitespace

    Computes the ratio for text in given node and text in links
    contained in the node. It is computed from number of
    characters in the texts.

    :parameter Element node:
        HTML element in which links density is computed.
    :parameter string node_text:
        Text content of given node if it was obtained before.
    :returns float:
        Returns value of computed 0 <= density <= 1, where 0 means
        no links and 1 means that node contains only links.
    """
    if node_text is None:
        node_text = node.text_content()
    node_text = normalize_whitespace(node_text.strip())

    text_length = len(node_text)
    if text_length == 0:
        return 0.0

    links_length = sum(map(_get_normalized_text_length, node.findall(".//a")))
    # Give 50 bonus chars worth of length for each img.
    # Tweaking this 50 down a notch should help if we hit false positives.
    img_bonuses = 50 * len(node.findall(".//img"))
    links_length = max(0, links_length - img_bonuses)

    return links_length / text_length


def _get_normalized_text_length(node):

    return len(normalize_whitespace(node.text_content().strip()))