# Path: hq/string_util.py
# def truncate_string(s, length, one_line=True, suffix='...'):
#     if len(s) <= length:
#         result = s
#     else:
#         result = s[:length + 1].rsplit(' ', 1)[0] + suffix
#     if one_line:
#         result = result.replace('\n', '\\n')
#     return result
# 
# Path: hq/verbosity.py
# def verbose_print(text, indent_after=False, outdent_before=False):
#     if settings.VERBOSE:
#         if outdent_before:
#             pop_indent()
#         if not is_a_string(text):
#             text = text()
#         print(u'{0}{1}'.format(' ' * indent_level, text), file=sys.stderr)
#         if indent_after:
#             push_indent()


import re
from builtins import str
from bs4 import BeautifulSoup
from .string_util import truncate_string
from .verbosity import verbose_print
def is_comment_node(obj):
    return obj.__class__.__name__ == 'Comment'


def is_root_node(obj):
    return obj.__class__.__name__ == 'BeautifulSoup'


def is_tag_node(obj):
    return obj.__class__.__name__ == 'Tag'


def is_text_node(obj):
    return obj.__class__.__name__ == 'NavigableString'


def make_soup(source):
    soup = BeautifulSoup(source, 'html.parser')
    counter = [0]

    def visit_node(node):
        node.hq_doc_index = counter[0]
        counter[0] += 1
        if is_tag_node(node):
            attr_names = sorted(node.attrs.keys(), key=lambda name: name.lower())
            node.hq_attrs = [AttributeNode(name, node.attrs[name]) for name in attr_names]
            for attr in node.hq_attrs:
                visit_node(attr)

    preorder_traverse_node_tree(soup, visit_node, filter=is_any_node)

# Path: hq/string_util.py
# def truncate_string(s, length, one_line=True, suffix='...'):
#     if len(s) <= length:
#         result = s
#     else:
#         result = s[:length + 1].rsplit(' ', 1)[0] + suffix
#     if one_line:
#         result = result.replace('\n', '\\n')
#     return result
# 
# Path: hq/verbosity.py
# def verbose_print(text, indent_after=False, outdent_before=False):
#     if settings.VERBOSE:
#         if outdent_before:
#             pop_indent()
#         if not is_a_string(text):
#             text = text()
#         print(u'{0}{1}'.format(' ' * indent_level, text), file=sys.stderr)
#         if indent_after:
#             push_indent()


# Path: hq/soup_util.py
import re
from builtins import str
from bs4 import BeautifulSoup
from .string_util import truncate_string
from .verbosity import verbose_print

def is_comment_node(obj):
    return obj.__class__.__name__ == 'Comment'


def is_root_node(obj):
    return obj.__class__.__name__ == 'BeautifulSoup'


def is_tag_node(obj):
    return obj.__class__.__name__ == 'Tag'


def is_text_node(obj):
    return obj.__class__.__name__ == 'NavigableString'


def make_soup(source):
    soup = BeautifulSoup(source, 'html.parser')
    counter = [0]

    def visit_node(node):
        node.hq_doc_index = counter[0]
        counter[0] += 1
        if is_tag_node(node):
            attr_names = sorted(node.attrs.keys(), key=lambda name: name.lower())
            node.hq_attrs = [AttributeNode(name, node.attrs[name]) for name in attr_names]
            for attr in node.hq_attrs:
                visit_node(attr)

    preorder_traverse_node_tree(soup, visit_node, filter=is_any_node)

    verbose_print('Loaded HTML document containing {0} indexed nodes.'.format(counter[0]))