Passed
Push — master ( b676e0...6487bc )
by Guillaume
01:50
created

Alpinejs::removeVersionSpecificBadges()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Docsets;
4
5
use Godbout\DashDocsetBuilder\Docsets\BaseDocset;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Storage;
8
use Illuminate\Support\Str;
9
use Wa72\HtmlPageDom\HtmlPageCrawler;
10
11
class Alpinejs extends BaseDocset
12
{
13
    public const CODE = 'alpinejs';
14
    public const NAME = 'Alpine.js';
15
    public const URL = 'github.com';
16
    public const INDEX = 'alpinejs/alpine.html';
17
    public const PLAYGROUND = 'https://alpinejs.codewithhugo.com/?type=components';
18
    public const ICON_16 = '../../icons/icon.png';
19
    public const ICON_32 = '../../icons/[email protected]';
20
    public const EXTERNAL_DOMAINS = [
21
        'githubassets.com',
22
    ];
23
24
25
    public function grab(): bool
26
    {
27
        $toGet = implode('|', [
28
            'github\.githubassets\.com/assets/',
29
        ]);
30
31
        system(
32
            "echo; wget github.com/alpinejs/alpine \
33
                --mirror \
34
                --trust-server-names \
35
                --accept-regex='{$toGet}' \
36
                --ignore-case \
37
                --page-requisites \
38
                --adjust-extension \
39
                --convert-links \
40
                --span-hosts \
41
                --domains={$this->externalDomains()} \
42
                --directory-prefix=storage/{$this->downloadedDirectory()} \
43
                -e robots=off \
44
                --quiet \
45
                --show-progress",
46
            $result
47
        );
48
49
        return $result === 0;
50
    }
51
52 8
    public function entries(string $file): Collection
53
    {
54 8
        $crawler = HtmlPageCrawler::create(Storage::get($file));
55
56 8
        $entries = collect();
57
58 8
        $entries = $entries->merge($this->guideEntries($crawler));
59 8
        $entries = $entries->merge($this->sectionEntries($crawler));
60
61 8
        return $entries;
62
    }
63
64 8
    protected function guideEntries(HtmlPageCrawler $crawler)
65
    {
66 8
        $entries = collect();
67
68 8
        $crawler->filter('.entry-content h2')->each(function (HtmlPageCrawler $node) use ($entries) {
69 8
            $entries->push([
70 8
                'name' => trim($node->text()),
71 8
                'type' => 'Guide',
72 8
                'path' => $this->url() . '/alpinejs/' . $node->children('a')->attr('href')
73
            ]);
74 8
        });
75
76 8
        return $entries;
77
    }
78
79 8
    protected function sectionEntries(HtmlPageCrawler $crawler)
80
    {
81 8
        $entries = collect();
82
83 8
        $crawler->filter('.entry-content h3')->each(function (HtmlPageCrawler $node) use ($entries) {
84 8
            $entries->push([
85 8
                'name' => trim($node->text()),
86 8
                'type' => 'Section',
87 8
                'path' => $this->url() . '/alpinejs/' . $node->children('a')->attr('href')
88
            ]);
89 8
        });
90
91 8
        return $entries;
92
    }
93
94 8
    public function format(string $file): string
95
    {
96 8
        $crawler = HtmlPageCrawler::create(Storage::get($file));
97
98 8
        $this->removeGitHubNavbar($crawler);
99 8
        $this->removeRepositoryNavigation($crawler);
100 8
        $this->removeSignupPrompt($crawler);
101 8
        $this->removeFileNavigation($crawler);
102 8
        $this->removeRepositoryDetails($crawler);
103 8
        $this->removeRightSidebar($crawler);
104 8
        $this->removeWeirdBoxHeader($crawler);
105 8
        $this->removeContentBorder($crawler);
106 8
        $this->removeVersionSpecificBadges($crawler);
107
108 8
        $this->removeFooter($crawler);
109
110 8
        $this->removeCrossOriginAndIntegrity($crawler);
111
112 8
        $this->insertOnlineRedirection($crawler);
113 8
        $this->insertDashTableOfContents($crawler);
114
115 8
        return $crawler->saveHTML();
116
    }
117
118 8
    protected function removeGitHubNavbar(HtmlPageCrawler $crawler)
119
    {
120 8
        $crawler->filter('.js-header-wrapper')->remove();
121 8
    }
122
123 8
    protected function removeRepositoryNavigation(HtmlPageCrawler $crawler)
124
    {
125 8
        $crawler->filter('.bg-gray-light.pt-3.hide-full-screen.mb-5')->remove();
126 8
    }
127
128 8
    protected function removeSignupPrompt(HtmlPageCrawler $crawler)
129
    {
130 8
        $crawler->filter('signup-prompt')->remove();
131 8
    }
132
133 8
    protected function removeFileNavigation(HtmlPageCrawler $crawler)
134
    {
135 8
        $crawler->filter('.file-navigation')->remove();
136 8
    }
137
138 8
    protected function removeRepositoryDetails(HtmlPageCrawler $crawler)
139
    {
140 8
        $crawler->filter('.Box.mb-3')->remove();
141 8
    }
142
143 8
    protected function removeRightSidebar(HtmlPageCrawler $crawler)
144
    {
145 8
        $crawler->filter('.flex-shrink-0.col-12.col-md-3')->remove();
146 8
    }
147
148 8
    protected function removeWeirdBoxHeader(HtmlPageCrawler $crawler)
149
    {
150 8
        $crawler->filter('.Box-header')->remove();
151 8
    }
152
153 8
    protected function removeContentBorder(HtmlPageCrawler $crawler)
154
    {
155 8
        $crawler->filter('#readme')->addClass('border-0 !important');
156 8
    }
157
158 8
    protected function removeVersionSpecificBadges(HtmlPageCrawler $crawler)
159
    {
160 8
        $crawler->filter('[alt="npm bundle size"]')->remove();
161 8
        $crawler->filter('[alt="npm version"]')->remove();
162 8
    }
163
164 8
    protected function removeFooter(HtmlPageCrawler $crawler)
165
    {
166 8
        $crawler->filter('.footer')->remove();
167 8
    }
168
169 8
    protected function removeCrossOriginAndIntegrity(HtmlPageCrawler $crawler)
170
    {
171 8
        $crawler->filter('script, link')
172 8
            ->removeAttribute('integrity')
173 8
            ->removeAttribute('crossorigin');
174 8
    }
175
176 8
    protected function insertOnlineRedirection(HtmlPageCrawler $crawler)
177
    {
178 8
        $crawler->filter('html')->prepend("<!-- Online page at https://github.com/alpinejs/alpine -->");
179 8
    }
180
181 8
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
182
    {
183 8
        $crawler->filter('head')
184 8
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
185
186 8
        $crawler->filter('h3')->each(function (HtmlPageCrawler $node) {
187 8
            $node->prepend(
188
                '<a id="'
189 8
                . Str::slug($node->text())
190 8
                . '" name="//apple_ref/cpp/Section/'
191 8
                . rawurlencode($node->text())
192 8
                . '" class="dashAnchor"></a>'
193
            );
194 8
        });
195 8
    }
196
}
197