|
46 | 46 | #: are stripped they would remain only as empty tags. |
47 | 47 | NON_CONTENT_TAGS = ("script", "style", "noscript", "template", "meta", "link", "base") |
48 | 48 |
|
| 49 | +#: Bibliography containers inside an article region. A cited paper's title is |
| 50 | +#: not evidence for the citing article, so their text is never extracted: |
| 51 | +#: PLOS ``ol.references``, PMC ``section.ref-list``, Springer |
| 52 | +#: ``ol.c-article-references``, Wiley ``section.article-section__references``, |
| 53 | +#: and the DPUB-ARIA ``doc-bibliography`` role. Whole class tokens on block |
| 54 | +#: containers only: a substring match would also delete an inline citation |
| 55 | +#: marker or a wrapper such as ``body-with-references`` around the whole |
| 56 | +#: body. A layout not listed here keeps its bibliography text; extend the list |
| 57 | +#: as publishers are seen, never with a substring. |
| 58 | +BIBLIOGRAPHY_SELECTOR = ( |
| 59 | + ":is(section, div, ol, ul, aside)" |
| 60 | + ":is(.ref-list, .references, .c-article-references," |
| 61 | + " .article-section__references, .bibliography), " |
| 62 | + '[role="doc-bibliography"]' |
| 63 | +) |
| 64 | + |
49 | 65 | #: The only attributes that carry meaning for a quoted excerpt: table structure. |
50 | 66 | KEPT_ATTRIBUTES = frozenset({"rowspan", "colspan", "scope"}) |
51 | 67 |
|
@@ -124,10 +140,10 @@ def html_to_text(content: str) -> str: |
124 | 140 | a browser would render them. Entities are unescaped. |
125 | 141 |
|
126 | 142 | Unlike :class:`HTMLExtractor`, which serves article text, nothing is |
127 | | - narrowed to ``<article>`` or ``<main>`` and no ``<p>``-only shortcut is |
128 | | - taken. Every text node of the sanitized markup appears, contiguous, in |
129 | | - the result, so a quote that matched the markup still matches, and a quote |
130 | | - through a link or bold text now matches too. |
| 143 | + narrowed to ``<article>`` or ``<main>``. Every text node of the sanitized |
| 144 | + markup appears, contiguous, in the result, so a quote that matched the |
| 145 | + markup still matches, and a quote through a link or bold text now matches |
| 146 | + too. |
131 | 147 |
|
132 | 148 | Examples: |
133 | 149 | >>> html_to_text('<p>Produced by <a href="/x">the City of Flint</a>.</p>') |
@@ -257,9 +273,10 @@ def _cell_text(cell: Tag) -> str: |
257 | 273 | class HTMLExtractor(Extractor): |
258 | 274 | """Extract readable text from HTML markup, as bytes or as a string. |
259 | 275 |
|
260 | | - Prefers an ``<article>`` or main content region; falls back to all paragraph |
261 | | - text, then to the whole document text. Callers holding an already-parsed |
262 | | - region of their own use :meth:`extract_scope` instead. |
| 276 | + Prefers an ``<article>`` or main content region, else the whole document. |
| 277 | + All of the region's text is kept, a line per block: paragraphs, headings, |
| 278 | + list items, table rows and bare ``<div>`` text alike (#121). Callers holding |
| 279 | + an already-parsed region of their own use :meth:`extract_scope` instead. |
263 | 280 |
|
264 | 281 | Whitespace around inline markup is preserved, so text spanning italicised |
265 | 282 | gene symbols survives extraction intact. |
@@ -426,38 +443,18 @@ def _extract_scope(self, scope: Tag) -> Optional[str]: |
426 | 443 | # Dropped here rather than in extract(), because the PMC paths enter |
427 | 444 | # through this method and a JSON-LD blob or stylesheet landing in |
428 | 445 | # cached article text would corrupt every snippet checked against it. |
429 | | - # bs4's get_text() also skips Script/Stylesheet strings by default, |
430 | | - # but that is a library default to rely on, not a guarantee this |
431 | | - # extractor makes. |
432 | | - for tag in scope.find_all(["script", "style"]): |
433 | | - tag.decompose() |
| 446 | + # Required, not defensive: _lines reads a <script>'s or <noscript>'s |
| 447 | + # text like any other string. A bibliography goes too: before #121 its |
| 448 | + # entries were dropped only by accident, as non-<p> text. |
| 449 | + for tag in [*scope.find_all(NON_CONTENT_TAGS), *scope.select(BIBLIOGRAPHY_SELECTOR)]: |
| 450 | + if not tag.decomposed: # already gone with an enclosing tag |
| 451 | + tag.decompose() |
434 | 452 |
|
435 | | - # Before either branch, so both agree: <br> carries no text of its own, |
436 | | - # so bare get_text() would weld the lines it separates into |
437 | | - # "Line oneLine two" - the same welding this extractor exists to avoid. |
438 | | - for line_break in scope.find_all("br"): |
439 | | - line_break.replace_with("\n") |
440 | | - |
441 | | - paragraphs = scope.find_all("p") |
442 | | - if paragraphs: |
443 | | - # get_text() with no arguments, deliberately: strip=True would trim |
444 | | - # every markup-delimited run before joining them, welding words to |
445 | | - # their neighbours across inline tags. "(<i>GUSB</i>, <i>GRN</i>, |
446 | | - # and <i>NEU1</i>)" became "(GUSB,GRN, andNEU1)", breaking every |
447 | | - # curated snippet that spans an italicised gene symbol. Trimming |
448 | | - # the finished paragraph instead keeps the source's own spacing. |
449 | | - texts = (p.get_text().strip() for p in paragraphs) |
450 | | - text = "\n\n".join(t for t in texts if t) |
451 | | - if text: |
452 | | - return text |
453 | | - |
454 | | - # Same hazard as the paragraph branch, one level down: a blanket |
455 | | - # separator lands between *every* pair of markup-delimited runs, so |
456 | | - # "(<i>GUSB</i>, <i>GRN</i>)" would come out as "(\nGUSB\n,\nGRN\n)". |
457 | | - # Marking block boundaries first means get_text() can then run bare, |
458 | | - # keeping the source's own spacing inside each block. |
459 | | - for block in scope.find_all(BLOCK_LEVEL_TAGS): |
460 | | - block.insert_after("\n") |
461 | | - |
462 | | - text = scope.get_text().strip() |
| 453 | + # The same block-aware walk html_to_text uses: every text node of the |
| 454 | + # scope appears once, each block on a line of its own, and inline |
| 455 | + # markup is unwrapped in place so "(<i>GUSB</i>, <i>GRN</i>)" keeps |
| 456 | + # the source's own spacing. Lists, tables, headings and bare <div> |
| 457 | + # text are kept: a scope's <p> text alone dropped them, and a nested |
| 458 | + # or unclosed <p> repeated and welded its children (#121). |
| 459 | + text = "\n".join(_lines(scope)) |
463 | 460 | return text if text else None |
0 commit comments