Passed
Push — master ( b06614...b676e0 )
by Guillaume
01:48
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
    ];
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
107 8
        $this->removeFooter($crawler);
108
109 8
        $this->removeCrossOriginAndIntegrity($crawler);
110
111 8
        $this->insertOnlineRedirection($crawler);
112 8
        $this->insertDashTableOfContents($crawler);
113
114 8
        return $crawler->saveHTML();
115
    }
116
117 8
    protected function removeGitHubNavbar(HtmlPageCrawler $crawler)
118
    {
119 8
        $crawler->filter('.js-header-wrapper')->remove();
120 8
    }
121
122 8
    protected function removeRepositoryNavigation(HtmlPageCrawler $crawler)
123
    {
124 8
        $crawler->filter('.bg-gray-light.pt-3.hide-full-screen.mb-5')->remove();
125 8
    }
126
127 8
    protected function removeSignupPrompt(HtmlPageCrawler $crawler)
128
    {
129 8
        $crawler->filter('signup-prompt')->remove();
130 8
    }
131
132 8
    protected function removeFileNavigation(HtmlPageCrawler $crawler)
133
    {
134 8
        $crawler->filter('.file-navigation')->remove();
135 8
    }
136
137 8
    protected function removeRepositoryDetails(HtmlPageCrawler $crawler)
138
    {
139 8
        $crawler->filter('.Box.mb-3')->remove();
140 8
    }
141
142 8
    protected function removeRightSidebar(HtmlPageCrawler $crawler)
143
    {
144 8
        $crawler->filter('.flex-shrink-0.col-12.col-md-3')->remove();
145 8
    }
146
147 8
    protected function removeWeirdBoxHeader(HtmlPageCrawler $crawler)
148
    {
149 8
        $crawler->filter('.Box-header')->remove();
150 8
    }
151
152 8
    protected function removeContentBorder(HtmlPageCrawler $crawler)
153
    {
154 8
        $crawler->filter('#readme')->addClass('border-0 !important');
155 8
    }
156
157 8
    protected function removeFooter(HtmlPageCrawler $crawler)
158
    {
159 8
        $crawler->filter('.footer')->remove();
160 8
    }
161
162 8
    protected function removeCrossOriginAndIntegrity(HtmlPageCrawler $crawler)
163
    {
164 8
        $crawler->filter('script, link')
165 8
            ->removeAttribute('integrity')
166 8
            ->removeAttribute('crossorigin');
167 8
    }
168
169 8
    protected function insertOnlineRedirection(HtmlPageCrawler $crawler)
170
    {
171 8
        $crawler->filter('html')->prepend("<!-- Online page at https://github.com/alpinejs/alpine -->");
172 8
    }
173
174 8
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
175
    {
176 8
        $crawler->filter('head')
177 8
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
178
179 8
        $crawler->filter('h3')->each(function (HtmlPageCrawler $node) {
180 8
            $node->prepend(
181
                '<a id="'
182 8
                . Str::slug($node->text())
183 8
                . '" name="//apple_ref/cpp/Section/'
184 8
                . rawurlencode($node->text())
185 8
                . '" class="dashAnchor"></a>'
186
            );
187 8
        });
188 8
    }
189
}
190