# Path: breadability/readable.py
# class Article(object):
#     """Parsed readable object"""
# 
#     def __init__(self, html, url=None, return_fragment=True):
#         """
#         Create the Article we're going to use.
# 
#         :param html: The string of HTML we're going to parse.
#         :param url: The url so we can adjust the links to still work.
#         :param return_fragment: Should we return a <div> fragment or
#             a full <html> document.
#         """
#         self._original_document = OriginalDocument(html, url=url)
#         self._return_fragment = return_fragment
# 
#     def __str__(self):
#         return tostring(self._readable())
# 
#     def __unicode__(self):
#         return tounicode(self._readable())
# 
#     @cached_property
#     def dom(self):
#         """Parsed lxml tree (Document Object Model) of the given html."""
#         try:
#             dom = self._original_document.dom
#             # cleaning doesn't return, just wipes in place
#             html_cleaner(dom)
#             return leaf_div_elements_into_paragraphs(dom)
#         except ValueError:
#             return None
# 
#     @cached_property
#     def candidates(self):
#         """Generates list of candidates from the DOM."""
#         dom = self.dom
#         if dom is None or len(dom) == 0:
#             return None
# 
#         candidates, unlikely_candidates = find_candidates(dom)
#         drop_nodes_with_parents(unlikely_candidates)
# 
#         return candidates
# 
#     @cached_property
#     def main_text(self):
#         dom = deepcopy(self.readable_dom).get_element_by_id("readabilityBody")
#         return AnnotatedTextHandler.parse(dom)
# 
#     @cached_property
#     def readable(self):
#         return tounicode(self.readable_dom)
# 
#     @cached_property
#     def readable_dom(self):
#         return self._readable()
# 
#     def _readable(self):
#         """The readable parsed article"""
#         if not self.candidates:
#             logger.info("No candidates found in document.")
#             return self._handle_no_candidates()
# 
#         # right now we return the highest scoring candidate content
#         best_candidates = sorted(
#             (c for c in self.candidates.values()),
#             key=attrgetter("content_score"), reverse=True)
# 
#         printer = PrettyPrinter(indent=2)
#         logger.debug(printer.pformat(best_candidates))
# 
#         # since we have several candidates, check the winner's siblings
#         # for extra content
#         winner = best_candidates[0]
#         updated_winner = check_siblings(winner, self.candidates)
#         updated_winner.node = prep_article(updated_winner.node)
#         if updated_winner.node is not None:
#             dom = build_base_document(
#                 updated_winner.node, self._return_fragment)
#         else:
#             logger.info(
#                 'Had candidates but failed to find a cleaned winning DOM.')
#             dom = self._handle_no_candidates()
# 
#         return self._remove_orphans(dom.get_element_by_id("readabilityBody"))
# 
#     def _remove_orphans(self, dom):
#         for node in dom.iterdescendants():
#             if len(node) == 1 and tuple(node)[0].tag == node.tag:
#                 node.drop_tag()
# 
#         return dom
# 
#     def _handle_no_candidates(self):
#         """
#         If we fail to find a good candidate we need to find something else.
#         """
#         # since we've not found a good candidate we're should help this
#         if self.dom is not None and len(self.dom):
#             dom = prep_article(self.dom)
#             dom = build_base_document(dom, self._return_fragment)
#             return self._remove_orphans(
#                 dom.get_element_by_id("readabilityBody"))
#         else:
#             logger.info("No document to use.")
#             return build_error_document(self._return_fragment)


import os
import pytest
from breadability.readable import Article
# -*- coding: utf8 -*-

"""Test the scoring and parsing of the Blog Post"""

from __future__ import absolute_import, division, print_function, unicode_literals





@pytest.fixture(scope="module")
def article():
    """Load up the article for us"""
    article_path = os.path.join(os.path.dirname(__file__), 'article.html')
    with open(article_path) as file:
        return file.read()


def test_parses(article):
    """Verify we can parse the document."""
    doc = Article(article)

    assert 'id="readabilityBody"' in doc.readable


def test_comments_cleaned(article):
    """The div with the comments should be removed."""

# Path: breadability/readable.py
# class Article(object):
#     """Parsed readable object"""
# 
#     def __init__(self, html, url=None, return_fragment=True):
#         """
#         Create the Article we're going to use.
# 
#         :param html: The string of HTML we're going to parse.
#         :param url: The url so we can adjust the links to still work.
#         :param return_fragment: Should we return a <div> fragment or
#             a full <html> document.
#         """
#         self._original_document = OriginalDocument(html, url=url)
#         self._return_fragment = return_fragment
# 
#     def __str__(self):
#         return tostring(self._readable())
# 
#     def __unicode__(self):
#         return tounicode(self._readable())
# 
#     @cached_property
#     def dom(self):
#         """Parsed lxml tree (Document Object Model) of the given html."""
#         try:
#             dom = self._original_document.dom
#             # cleaning doesn't return, just wipes in place
#             html_cleaner(dom)
#             return leaf_div_elements_into_paragraphs(dom)
#         except ValueError:
#             return None
# 
#     @cached_property
#     def candidates(self):
#         """Generates list of candidates from the DOM."""
#         dom = self.dom
#         if dom is None or len(dom) == 0:
#             return None
# 
#         candidates, unlikely_candidates = find_candidates(dom)
#         drop_nodes_with_parents(unlikely_candidates)
# 
#         return candidates
# 
#     @cached_property
#     def main_text(self):
#         dom = deepcopy(self.readable_dom).get_element_by_id("readabilityBody")
#         return AnnotatedTextHandler.parse(dom)
# 
#     @cached_property
#     def readable(self):
#         return tounicode(self.readable_dom)
# 
#     @cached_property
#     def readable_dom(self):
#         return self._readable()
# 
#     def _readable(self):
#         """The readable parsed article"""
#         if not self.candidates:
#             logger.info("No candidates found in document.")
#             return self._handle_no_candidates()
# 
#         # right now we return the highest scoring candidate content
#         best_candidates = sorted(
#             (c for c in self.candidates.values()),
#             key=attrgetter("content_score"), reverse=True)
# 
#         printer = PrettyPrinter(indent=2)
#         logger.debug(printer.pformat(best_candidates))
# 
#         # since we have several candidates, check the winner's siblings
#         # for extra content
#         winner = best_candidates[0]
#         updated_winner = check_siblings(winner, self.candidates)
#         updated_winner.node = prep_article(updated_winner.node)
#         if updated_winner.node is not None:
#             dom = build_base_document(
#                 updated_winner.node, self._return_fragment)
#         else:
#             logger.info(
#                 'Had candidates but failed to find a cleaned winning DOM.')
#             dom = self._handle_no_candidates()
# 
#         return self._remove_orphans(dom.get_element_by_id("readabilityBody"))
# 
#     def _remove_orphans(self, dom):
#         for node in dom.iterdescendants():
#             if len(node) == 1 and tuple(node)[0].tag == node.tag:
#                 node.drop_tag()
# 
#         return dom
# 
#     def _handle_no_candidates(self):
#         """
#         If we fail to find a good candidate we need to find something else.
#         """
#         # since we've not found a good candidate we're should help this
#         if self.dom is not None and len(self.dom):
#             dom = prep_article(self.dom)
#             dom = build_base_document(dom, self._return_fragment)
#             return self._remove_orphans(
#                 dom.get_element_by_id("readabilityBody"))
#         else:
#             logger.info("No document to use.")
#             return build_error_document(self._return_fragment)


# Path: tests/test_articles/test_antipope_org/test_article.py
import os
import pytest
from breadability.readable import Article

# -*- coding: utf8 -*-

"""Test the scoring and parsing of the Blog Post"""

from __future__ import absolute_import, division, print_function, unicode_literals





@pytest.fixture(scope="module")
def article():
    """Load up the article for us"""
    article_path = os.path.join(os.path.dirname(__file__), 'article.html')
    with open(article_path) as file:
        return file.read()


def test_parses(article):
    """Verify we can parse the document."""
    doc = Article(article)

    assert 'id="readabilityBody"' in doc.readable


def test_comments_cleaned(article):
    """The div with the comments should be removed."""

    doc = Article(article)