Passed
Push — master ( 3afcce...e044cf )
by Guillaume
08:13
created

Ploi::removeSidebar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Docsets;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Collection;
7
use Wa72\HtmlPageDom\HtmlPageCrawler;
8
use Illuminate\Support\Facades\Storage;
9
use Godbout\DashDocsetBuilder\Docsets\BaseDocset;
10
11
class Ploi extends BaseDocset
12
{
13
    public const CODE = 'ploi-api';
14
    public const NAME = 'Ploi API';
15
    public const URL = 'developers.ploi.io';
16
    public const INDEX = 'index.html';
17
    public const PLAYGROUND = '';
18
    public const ICON_16 = '../documentator.s3.eu-west-3.amazonaws.com/11/conversions/favicon-favicon-16.png';
19
    public const ICON_32 = '../documentator.s3.eu-west-3.amazonaws.com/11/conversions/favicon-favicon-32.png';
20
    public const EXTERNAL_DOMAINS = [
21
        'documentator.s3.eu-west-3.amazonaws.com'
22
    ];
23
24
25
    public function grab(): bool
26
    {
27
        system(
28
            "echo; wget {$this->url()} \
29
                --mirror \
30
                --trust-server-names \
31
                --page-requisites \
32
                --adjust-extension \
33
                --convert-links \
34
                --span-hosts \
35
                --domains={$this->externalDomains()} \
36
                --directory-prefix=storage/{$this->downloadedDirectory()} \
37
                -e robots=off \
38
                --quiet \
39
                --show-progress",
40
            $result
41
        );
42
43
        return $result === 0;
44
    }
45
46
    public function entries(string $file): Collection
47
    {
48
        $crawler = HtmlPageCrawler::create(Storage::get($file));
49
50
        $entries = collect();
51
        $entries = $entries->merge($this->guideEntries($crawler, $file));
52
53
        return $entries;
54
    }
55
56
    protected function guideEntries(HtmlPageCrawler $crawler, string $file)
57
    {
58
        $entries = collect();
59
60
        if (Str::contains($file, "{$this->url()}/index.html")) {
61
            $crawler->filter('aside a.ml-2')->each(function (HtmlPageCrawler $node) use ($entries) {
62
                $entries->push([
63
                    'name' => trim($node->text()),
64
                    'type' => 'Guide',
65
                    'path' => $this->url() . '/' . $node->attr('href'),
66
                ]);
67
            });
68
        }
69
70
        return $entries;
71
    }
72
73
    public function format(string $html): string
74
    {
75
        $crawler = HtmlPageCrawler::create($html);
76
77
        $this->removeHeader($crawler);
78
        $this->removeSidebar($crawler);
79
        $this->removePreviousAndNextNavigation($crawler);
80
        $this->updateTopPadding($crawler);
81
        $this->removeUnwantedJavaScript($crawler);
82
        $this->insertDashTableOfContents($crawler);
83
84
        return $crawler->saveHTML();
85
    }
86
87
    protected function removeHeader(HtmlPageCrawler $crawler)
88
    {
89
        $crawler->filter('header')->remove();
90
    }
91
92
    protected function removeSidebar(HtmlPageCrawler $crawler)
93
    {
94
        $crawler->filter('aside')->remove();
95
    }
96
97
    protected function removePreviousAndNextNavigation(HtmlPageCrawler $crawler)
98
    {
99
        $crawler->filter('#previous-and-next')->remove();
100
    }
101
102
    protected function updateTopPadding(HtmlPageCrawler $crawler)
103
    {
104
        $crawler->filter('main')
105
            ->addClass('md:pt-12')
106
        ;
107
    }
108
109
    protected function removeUnwantedJavaScript(HtmlPageCrawler $crawler)
110
    {
111
        $crawler->filter("link[href*='analytics.dennissmink.com']")->remove();
112
        $crawler->filterXPath("//script[text()[contains(.,'analytics.dennissmink.com')]]")->remove();
113
        $crawler->filter('noscript')->remove();
114
    }
115
116
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
117
    {
118
        $crawler->filter('h1')
119
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
120
121
        $crawler->filter('h2, h3, h4')->each(static function (HtmlPageCrawler $node) {
122
            $node->before(
123
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>'
124
            );
125
        });
126
127
        $crawler->filterXPath('//p[not(descendant::code)]')->each(static function (HtmlPageCrawler $node) {
128
            $node->before(
129
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>'
130
            );
131
        });
132
    }
133
}
134