Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of post_strip_attr #211

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Extractor/ContentExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,13 @@ function ($element, $currentEntity) {
$this->success = true;
}

foreach ($this->siteConfig->post_strip_attr as $pattern) {
$this->logger->log('debug', 'Trying {pattern} to strip attribute', ['pattern' => $pattern]);
$elems = $this->xpath->query($pattern, $this->body);

$this->removeAttributes($elems, 'Stripping {length} attributes (post_strip_attr)');
}

// if we've had no success and we've used tidy, there's a chance
// that tidy has messed up. So let's try again without tidy...
if (!$this->success && $tidied && $smartTidy) {
Expand Down
4 changes: 2 additions & 2 deletions src/SiteConfig/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public function loadSiteConfig($host, $exactHostMatch = false)
public function mergeConfig(SiteConfig $currentConfig, SiteConfig $newConfig)
{
// check for commands where we accept multiple statements (no test_url)
foreach (['title', 'body', 'strip', 'strip_id_or_class', 'strip_image_src', 'single_page_link', 'next_page_link', 'date', 'author'] as $var) {
foreach (['title', 'body', 'strip', 'strip_id_or_class', 'strip_image_src', 'single_page_link', 'next_page_link', 'date', 'author', 'post_strip_attr'] as $var) {
// append array elements for this config variable from $newConfig to this config
$currentConfig->$var = array_unique(array_merge($currentConfig->$var, $newConfig->$var));
}
Expand Down Expand Up @@ -372,7 +372,7 @@ public function parseLines(array $lines)
}

// check for commands where we accept multiple statements
if (\in_array($command, ['title', 'body', 'strip', 'strip_id_or_class', 'strip_image_src', 'single_page_link', 'next_page_link', 'test_url', 'find_string', 'replace_string', 'login_extra_fields', 'native_ad_clue', 'date', 'author'], true)) {
if (\in_array($command, ['title', 'body', 'strip', 'strip_id_or_class', 'strip_image_src', 'single_page_link', 'next_page_link', 'test_url', 'find_string', 'replace_string', 'login_extra_fields', 'native_ad_clue', 'date', 'author', 'post_strip_attr'], true)) {
array_push($config->$command, $val);
// check for single statement commands that evaluate to true or false
} elseif (\in_array($command, ['tidy', 'prune', 'autodetect_on_failure', 'requires_login', 'skip_json_ld'], true)) {
Expand Down
3 changes: 3 additions & 0 deletions src/SiteConfig/SiteConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ class SiteConfig
*/
public $skip_json_ld = false;

// Strip attributes matching these xpath expressions (0 or more) after content extraction (post-processing)
public $post_strip_attr = [];

protected $default_tidy = true; // used if undeclared
protected $default_autodetect_on_failure = true; // used if undeclared
protected $default_prune = true; // used if undeclared
Expand Down