GeezerVison is showing github.com — code linting · lsx88so/python-readability@bd8293e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: readability/cleaners.py +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 14 | 14 | ">" # end | 15 | 15 | , re.I) | 16 | 16 |
| | 17 | + | 17 | 18 | def clean_attributes(html): | 18 | 19 | while htmlstrip.search(html): | 19 | 20 | html = htmlstrip.sub('<\\1\\2>', html) | 20 | 21 | return html | 21 | 22 |
| | 23 | + | 22 | 24 | def normalize_spaces(s): | 23 | 25 | if not s: | 24 | 26 | return '' | 25 | 27 | """replace any sequence of whitespace | 26 | 28 | characters with a single space""" | 27 | 29 | return ' '.join(s.split()) | 28 | 30 |
| | 31 | + | 29 | 32 | html_cleaner = Cleaner(scripts=True, javascript=True, comments=True, | 30 | 33 | style=True, links=True, meta=False, add_nofollow=False, | 31 | 34 | page_structure=False, processing_instructions=True, embedded=False, | |
Collapse file Copy file name to clipboardExpand all lines: readability/debug.py -2Lines changed: 0 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -49,5 +49,3 @@ def text_content(elem, length=40): | 49 | 49 | if len(content) < length: | 50 | 50 | return content | 51 | 51 | return content[:length] + '...' | 52 | | - | 53 | | - |
Collapse file Copy file name to clipboardExpand all lines: readability/htmls.py +12-3Lines changed: 12 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | from lxml.html import tostring | 2 | | -import logging | 3 | 2 | import lxml.html | 4 | | -import re, sys | | 3 | +import re | 5 | 4 |
| 6 | 5 | from .cleaners import normalize_spaces, clean_attributes | 7 | 6 | from .encoding import get_encoding | 8 | 7 | from .compat import str_ | 9 | 8 |
| 10 | 9 | utf8_parser = lxml.html.HTMLParser(encoding='utf-8') | 11 | 10 |
| | 11 | + | 12 | 12 | def build_doc(page): | 13 | 13 | if isinstance(page, str_): | 14 | 14 | encoding = None | 15 | 15 | decoded_page = page | 16 | 16 | else: | 17 | 17 | encoding = get_encoding(page) or 'utf-8' | 18 | 18 | decoded_page = page.decode(encoding, 'replace') | 19 | | - | | 19 | + | 20 | 20 | # XXX: we have to do .decode and .encode even for utf-8 pages to remove bad characters | 21 | 21 | doc = lxml.html.document_fromstring(decoded_page.encode('utf-8', 'replace'), parser=utf8_parser) | 22 | 22 | return doc, encoding | 23 | 23 |
| | 24 | + | 24 | 25 | def js_re(src, pattern, flags, repl): | 25 | 26 | return re.compile(pattern, flags).sub(src, repl.replace('$', '\\')) | 26 | 27 |
| | 28 | + | 27 | 29 | def normalize_entities(cur_title): | 28 | 30 | entities = { | 29 | 31 | u'\u2014':'-', | @@ -41,26 +43,31 @@ def normalize_entities(cur_title): | 41 | 43 |
| 42 | 44 | return cur_title | 43 | 45 |
| | 46 | + | 44 | 47 | def norm_title(title): | 45 | 48 | return normalize_entities(normalize_spaces(title)) | 46 | 49 |
| | 50 | + | 47 | 51 | def get_title(doc): | 48 | 52 | title = doc.find('.//title') | 49 | 53 | if title is None or title.text is None or len(title.text) == 0: | 50 | 54 | return '[no-title]' | 51 | 55 |
| 52 | 56 | return norm_title(title.text) | 53 | 57 |
| | 58 | + | 54 | 59 | def add_match(collection, text, orig): | 55 | 60 | text = norm_title(text) | 56 | 61 | if len(text.split()) >= 2 and len(text) >= 15: | 57 | 62 | if text.replace('"', '') in orig.replace('"', ''): | 58 | 63 | collection.add(text) | 59 | 64 |
| | 65 | + | 60 | 66 | TITLE_CSS_HEURISTICS = ['#title', '#head', '#heading', '.pageTitle', | 61 | 67 | '.news_title', '.title', '.head', '.heading', | 62 | 68 | '.contentheading', '.small_header_red'] | 63 | 69 |
| | 70 | + | 64 | 71 | def shorten_title(doc): | 65 | 72 | title = doc.find('.//title') | 66 | 73 | if title is None or title.text is None or len(title.text) == 0: | @@ -109,6 +116,8 @@ def shorten_title(doc): | 109 | 116 |
| 110 | 117 | return title | 111 | 118 |
| | 119 | + | | 120 | +# is it necessary? Cleaner from LXML is initialized correctly in cleaners.py | 112 | 121 | def get_body(doc): | 113 | 122 | for elem in doc.xpath('.//script | .//link | .//style'): | 114 | 123 | elem.drop_tree() | |
Collapse file Copy file name to clipboardExpand all lines: readability/readability.py +7-9Lines changed: 7 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | import re | 5 | 5 | import sys | 6 | 6 |
| 7 | | -from collections import defaultdict | 8 | 7 | from lxml.etree import tostring | 9 | 8 | from lxml.etree import tounicode | 10 | 9 | from lxml.html import document_fromstring | @@ -56,7 +55,6 @@ def to_int(x): | 56 | 55 | def clean(text): | 57 | 56 | # Many spaces make the following regexes run forever | 58 | 57 | text = re.sub(r'\s{255,}', ' ' * 255, text) | 59 | | - | 60 | 58 | text = re.sub(r'\s*\n\s*', '\n', text) | 61 | 59 | text = re.sub(r'\t|[ \t]{2,}', ' ', text) | 62 | 60 | return text.strip() | @@ -65,12 +63,11 @@ def clean(text): | 65 | 63 | def text_length(i): | 66 | 64 | return len(clean(i.text_content() or "")) | 67 | 65 |
| 68 | | -regexp_type = type(re.compile('hello, world')) | 69 | 66 |
| 70 | 67 | def compile_pattern(elements): | 71 | 68 | if not elements: | 72 | 69 | return None | 73 | | - elif isinstance(elements, regexp_type): | | 70 | + elif isinstance(elements, re._pattern_type): | 74 | 71 | return elements | 75 | 72 | elif isinstance(elements, (str_, bytes_)): | 76 | 73 | if isinstance(elements, bytes_): | @@ -82,6 +79,7 @@ def compile_pattern(elements): | 82 | 79 | raise Exception("Unknown type for the pattern: {}".format(type(elements))) | 83 | 80 | # assume string or string like object | 84 | 81 |
| | 82 | + | 85 | 83 | class Document: | 86 | 84 | """Class to build a etree document out of html.""" | 87 | 85 |
| @@ -98,9 +96,9 @@ def __init__(self, input, positive_keywords=None, negative_keywords=None, | 98 | 96 | :param xpath: If set to True, adds x="..." attribute to each HTML node, | 99 | 97 | containing xpath path pointing to original document path (allows to | 100 | 98 | reconstruct selected summary in original document). | 101 | | - :param handle_failures: Parameter passed to `lxml` for handling failure during exception. | | 99 | + :param handle_failures: Parameter passed to `lxml` for handling failure during exception. | 102 | 100 | Support options = ["discard", "ignore", None] | 103 | | - | | 101 | +
| 104 | 102 | Examples: | 105 | 103 | positive_keywords=["news-item", "block"] | 106 | 104 | positive_keywords=["news-item, block"] | @@ -290,7 +288,7 @@ def select_best_candidate(self, candidates): | 290 | 288 | return None | 291 | 289 |
| 292 | 290 | sorted_candidates = sorted( | 293 | | - candidates.values(), | | 291 | + candidates.values(), | 294 | 292 | key=lambda x: x['content_score'], | 295 | 293 | reverse=True | 296 | 294 | ) | @@ -517,10 +515,10 @@ def sanitize(self, node, candidates): | 517 | 515 |
| 518 | 516 | #if el.tag == 'div' and counts["img"] >= 1: | 519 | 517 | # continue | 520 | | - if counts["p"] and counts["img"] > 1+counts["p"]*1.3: | | 518 | + if counts["p"] and counts["img"] > 1 + counts["p"]*1.3: | 521 | 519 | reason = "too many images (%s)" % counts["img"] | 522 | 520 | to_remove = True | 523 | | - elif counts["li"] > counts["p"] and tag != "ul" and tag != "ol": | | 521 | + elif counts["li"] > counts["p"] and tag not in ("ol", "ul"): | 524 | 522 | reason = "more <li>s than <p>s" | 525 | 523 | to_remove = True | 526 | 524 | elif counts["input"] > (counts["p"] / 3): | |
|