Passed
Push — master ( 6cc72c...338b59 )
by Guillaume
12:27 queued 09:09
created

Alpinejs::insertOnlineRedirection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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