RickAstley::removeUnwantedJavaScript()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 13
ccs 12
cts 12
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Godbout\DashDocsetBuilder\Docsets;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Facades\Storage;
7
use Illuminate\Support\Str;
8
use Wa72\HtmlPageDom\HtmlPageCrawler;
9
10
class RickAstley extends BaseDocset
11
{
12
    public const CODE = 'rick-astley';
13
    public const NAME = 'Rick Astley';
14
    public const URL = 'rickastley.co.uk';
15
    public const INDEX = 'index.html';
16
    public const PLAYGROUND = '';
17
    public const ICON_16 = 'icons/favicon-16x16.png';
18
    public const ICON_32 = 'icons/favicon-32x32.png';
19
    public const EXTERNAL_DOMAINS = [
20
        'fonts.googleapis.com',
21
        'widget.songkick.com',
22
        'cdn-images.mailchimp.com',
23
        's3.amazonaws.com',
24
        'ajax.googleapis.com'
25
    ];
26
27
28 16
    public function entries(string $file): Collection
29
    {
30 16
        $entries = collect();
31
32 16
        $crawler = HtmlPageCrawler::create(Storage::get($file));
33
34 16
        if (Str::contains($file, "{$this->url()}/index.html")) {
35 16
            $crawler->filter('#main_menu li:not(:first-child) a')->each(function () use ($entries) {
36 16
                $entries->push([
37 16
                    'name' => 'Rick Astley - Official Website',
38 16
                    'type' => 'Guide',
39 16
                    'path' => $this->url() . '/index.html'
40
                ]);
41 16
            });
42
        }
43
44 16
        $crawler->filter('#main_menu li:not(:first-child) a')->each(function (HtmlPageCrawler $node) use ($entries) {
45 16
            $entries->push([
46 16
                'name' => $node->text(),
47 16
                'type' => 'Section',
48 16
                'path' => $this->url() . '/' . $node->attr('href')
49
            ]);
50 16
        });
51
52 16
        return $entries;
53
    }
54
55 16
    public function format(string $file): string
56
    {
57 16
        $crawler = HtmlPageCrawler::create(Storage::get($file));
58
59 16
        $this->removeHeader($crawler);
60 16
        $this->removeFooter($crawler);
61
62 16
        $this->removeUnwantedHTML($crawler);
63 16
        $this->removeUnwantedJavaScript($crawler);
64
65 16
        $this->insertDashTableOfContents($crawler);
66
67 16
        return $crawler->saveHTML();
68
    }
69
70 16
    protected function removeHeader(HtmlPageCrawler $crawler)
71
    {
72 16
        $crawler->filter('#header')->remove();
73 16
    }
74
75 16
    protected function removeFooter(HtmlPageCrawler $crawler)
76
    {
77 16
        $crawler->filter('#footer')->remove();
78 16
    }
79
80 16
    protected function removeUnwantedHTML(HtmlPageCrawler $crawler)
81
    {
82 16
        $crawler->filterXPath("//img[@src[contains(.,'secure.adnxs.com')]]")->remove();
83 16
    }
84
85 16
    protected function removeUnwantedJavaScript(HtmlPageCrawler $crawler)
86
    {
87 16
        $crawler->filter('noscript')->remove();
88 16
        $crawler->filterXPath("//script[@src[contains(.,'platform.twitter.com')]]")->remove();
89 16
        $crawler->filterXPath("//script[@src[contains(.,'googletagmanager')]]")->remove();
90 16
        $crawler->filterXPath("//script[@src[contains(.,'googleadservices')]]")->remove();
91 16
        $crawler->filterXPath("//script[text()[contains(.,'googletagmanager')]]")->remove();
92 16
        $crawler->filterXPath("//script[text()[contains(.,'gtag')]]")->remove();
93 16
        $crawler->filterXPath("//script[text()[contains(.,'connect.facebook.net')]]")->remove();
94 16
        $crawler->filterXPath("//script[text()[contains(.,'google_conversion_id')]]")->remove();
95 16
        $crawler->filterXPath("//script[text()[contains(.,'googleadservices')]]")->remove();
96 16
        $crawler->filterXPath("//script[text()[contains(.,'platform.twitter.com')]]")->remove();
97 16
        $crawler->filterXPath("//script[text()[contains(.,'twttr.conversion')]]")->remove();
98 16
    }
99
100 16
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
101
    {
102 16
        $crawler->filter('head')
103 16
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
104
105 16
        $crawler->filter('div.page_title, div.product_title')->each(static function (HtmlPageCrawler $node) {
106 16
            $node->before(
107 16
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>'
108
            );
109 16
        });
110 16
    }
111
}
112