Skip to content

Commit

Permalink
Merge pull request #20 from nadar/strip-tags-handler
Browse files Browse the repository at this point in the history
format content
  • Loading branch information
nadar authored Aug 10, 2021
2 parents 3e6c579 + e5681d0 commit 8eedb04
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).
In order to read more about upgrading and BC breaks have a look at the [UPGRADE Document](UPGRADE.md).

## 1.7.0 (10. August 2021)

+ [#20](https://github.com/nadar/crawler/pull/20) Improve the strip tags for html parser in order to generate a more clean and readable output when `$stripTags` is enabled. Things like `<p>foo</p><p>bar</p>` are now handled as `foo bar` instead of `foobar`.

## 1.6.2 (16. April 2021)

+ [#18](https://github.com/nadar/crawler/pull/18) Fix issue with pages where utf8 chars are in title tag.
Expand Down
40 changes: 38 additions & 2 deletions src/Parsers/HtmlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
class HtmlParser implements ParserInterface
{
/**
* @var boolean Whether the HTML tags should be stripped from $result->content variable or not.
* @var boolean Whether the HTML tags should be stripped from $result->content variable or not. Since version 1.7.0 also spaces will be added in certain circumstances.
* For example space boundings `Hello<br />World` will be `Hello World` instead of `HelloWorld`. In order to recieved the original content from the web request, disable
* $stripTags. The space bounding can also lead to wrong spaces, for example `<a href="#">foobar</a>.` will be `foobar .` instead of `foobar.` as `.` is a normal char.
*/
public $stripTags = true;

Expand Down Expand Up @@ -52,7 +54,7 @@ public function run(Job $job, RequestResponse $requestResponse) : ParserResult


$content = $this->stripCrawlIgnore($body);
$content = $this->stripTags ? strip_tags($content) : $content;
$content = $this->stripTags ? $this->formatContent($content) : $content;

$jobResult = new ParserResult();
$jobResult->content = $jobResult->trim($content); // get only the content between "body" tags
Expand Down Expand Up @@ -291,4 +293,38 @@ public function getDomLinks(DOMDocument $dom, $ignoreRels = [])
unset ($links);
return $refs;
}

/**
* Format Content
*
* + Remove tags
* + Remove unneccsary whitespaces (like double whitespaces)
* + make spaces between words when they are seperated by tags (`<p>foo</p><p>bar</p>` will be `foo bar` instead of `foobar`)
* + Ensure no spaces before punctuation marks.
*
* @param string $content The content to format
* @return string The striped and formated content
* @see https://www.php.net/manual/de/function.strip-tags.php#110280
* @since 1.7.0
*/
public function formatContent($content)
{
// remove HTML TAGs
$string = preg_replace ('/<[^>]*>/', ' ', $content);

// remove control characters
$string = str_replace("\r", '', $string); // --- replace with empty space
$string = str_replace("\n", ' ', $string); // --- replace with space
$string = str_replace("\t", ' ', $string); // --- replace with space

// remove multiple spaces
$string = trim(preg_replace('/ {2,}/', ' ', $string));

// handle wrong control char spacings
$string = preg_replace('/(\s)([\!\,\.\?])/', '$2', $string);

// for security reasons, add strip tags
// https://www.php.net/manual/de/function.strip-tags.php#118183
return strip_tags($string);
}
}
2 changes: 1 addition & 1 deletion tests/Parsers/HtmlParserExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,6 @@ public function testCrawlerContent()
$parser = new HtmlParser();
$result = $parser->run($job, $requestResponse);

$this->assertSame('Immer wieder leben lernen – Willkommen im foobar Berlin Wir begleiten Menschen mit einer Hirnschädigung und /oder Querschnittlähmung nach Unfall oder Krankheit zurück ins Leben. Das foobar Berlin ist eine hochspezialisierte Klinik für Neurofoobarilitation und Paraplegiologie. Wir haben ein breites stationäres Angebot und bieten Ihnen auch Behandlungen in unserer Tagesklinik und im Ambulatorium an. Ganzheitliche foobarilitation Unsere wichtigsten Ziele bei der foobarilitation sind Ihre grösstmögliche Selbstständigkeit und Lebensqualität. Es geht darum, nach einer Erkrankung oder einem schweren Trauma wieder seinen Platz in der Gesellschaft zu finden.Nach Ihrem stationären Aufenthalt setzen wir uns weiterhin für Sie ein - als kompetente Begleiterin und "Schrittmacherin". Auch dann, wenn es um Ihre berufliche Wiedereingliederung geht. Film "Portrait foobar Berlin" (5 Minuten) Information Corona-Virus Besuchsregelung: Bitte beachten Sie das Schreiben vom 18.02.2021 zu Besuchen. Für Angehörige, Patient*innen und Besucher*innen bleibt das Bistro bis 31. März 2021 geschlossen. Weiterlesen Ambulante Long-COVID Sprechstunde Das foobar Berlin bietet ab sofort eine ambulante Long-COVID Sprechstunde für Betroffene mit Langzeitfolgen einer Covid-19 Infektion an. Weiterlesen 4. SW!SS REHA Forum - Folien Hier finden Sie die Präsentationen des SW!SS REHA Forums vom 5./6. November 2020 Weiterlesen Maskenknigge Sehen Sie hier den Maskenknigge des foobar Berlin. Weiterlesen Fortbildungen Bis Ende März 2021 finden keine medizinischen Fortbildungen statt. Weiterlesen', $result->content);
$this->assertSame('Immer wieder leben lernen – Willkommen im foobar Berlin Wir begleiten Menschen mit einer Hirnschädigung und /oder Querschnittlähmung nach Unfall oder Krankheit zurück ins Leben. Das foobar Berlin ist eine hochspezialisierte Klinik für Neurofoobarilitation und Paraplegiologie. Wir haben ein breites stationäres Angebot und bieten Ihnen auch Behandlungen in unserer Tagesklinik und im Ambulatorium an. Ganzheitliche foobarilitation Unsere wichtigsten Ziele bei der foobarilitation sind Ihre grösstmögliche Selbstständigkeit und Lebensqualität. Es geht darum, nach einer Erkrankung oder einem schweren Trauma wieder seinen Platz in der Gesellschaft zu finden. Nach Ihrem stationären Aufenthalt setzen wir uns weiterhin für Sie ein - als kompetente Begleiterin und "Schrittmacherin". Auch dann, wenn es um Ihre berufliche Wiedereingliederung geht. Film "Portrait foobar Berlin" (5 Minuten) Information Corona-Virus Besuchsregelung: Bitte beachten Sie das Schreiben vom 18.02.2021 zu Besuchen. Für Angehörige, Patient*innen und Besucher*innen bleibt das Bistro bis 31. März 2021 geschlossen. Weiterlesen Ambulante Long-COVID Sprechstunde Das foobar Berlin bietet ab sofort eine ambulante Long-COVID Sprechstunde für Betroffene mit Langzeitfolgen einer Covid-19 Infektion an. Weiterlesen 4. SW!SS REHA Forum - Folien Hier finden Sie die Präsentationen des SW!SS REHA Forums vom 5./6. November 2020 Weiterlesen Maskenknigge Sehen Sie hier den Maskenknigge des foobar Berlin. Weiterlesen Fortbildungen Bis Ende März 2021 finden keine medizinischen Fortbildungen statt. Weiterlesen', $result->content);
}
}

0 comments on commit 8eedb04

Please sign in to comment.