Passed
Push — master ( 75280b...57fac3 )
by Guillaume
06:18 queued 04:08
created

Alpinejs::removeWeirdBoxHeader()   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->insertDashTableOfContents($crawler);
126
127 8
        return $crawler->saveHTML();
128
    }
129
130 8
    protected function removeGitHubNavbar(HtmlPageCrawler $crawler)
131
    {
132 8
        $crawler->filter('.js-header-wrapper')->remove();
133 8
    }
134
135 8
    protected function removeRepositoryNavigation(HtmlPageCrawler $crawler)
136
    {
137 8
        $crawler->filter('.bg-gray-light.pt-3.hide-full-screen.mb-5')->remove();
138 8
    }
139
140 8
    protected function removeSignupPrompt(HtmlPageCrawler $crawler)
141
    {
142 8
        $crawler->filter('signup-prompt')->remove();
143 8
    }
144
145 8
    protected function removeFileNavigation(HtmlPageCrawler $crawler)
146
    {
147 8
        $crawler->filter('.file-navigation')->remove();
148 8
    }
149
150 8
    protected function removeRepositoryDetails(HtmlPageCrawler $crawler)
151
    {
152 8
        $crawler->filter('.Box.mb-3')->remove();
153 8
    }
154
155 8
    protected function removeRightSidebar(HtmlPageCrawler $crawler)
156
    {
157 8
        $crawler->filter('.flex-shrink-0.col-12.col-md-3')->remove();
158 8
    }
159
160 8
    protected function removeWeirdBoxHeader(HtmlPageCrawler $crawler)
161
    {
162 8
        $crawler->filter('.Box-header')->remove();
163 8
    }
164
165 8
    protected function removeContentBorder(HtmlPageCrawler $crawler)
166
    {
167 8
        $crawler->filter('#readme')->addClass('border-0 !important');
168 8
    }
169
170 8
    protected function removeFooter(HtmlPageCrawler $crawler)
171
    {
172 8
        $crawler->filter('.footer')->remove();
173 8
    }
174
175 8
    protected function removeCrossOriginAndIntegrity(HtmlPageCrawler $crawler)
176
    {
177 8
        $crawler->filter('script, link')
178 8
            ->removeAttribute('integrity')
179 8
            ->removeAttribute('crossorigin');
180 8
    }
181
182 8
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
183
    {
184 8
        $crawler->filter('head')
185 8
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
186
187 4
        $crawler->filter('h3')->each(function (HtmlPageCrawler $node) {
188 8
            $node->prepend(
189
                '<a id="'
190 8
                . Str::slug($node->text())
191 8
                . '" name="//apple_ref/cpp/Section/'
192 8
                . rawurlencode($node->text())
193 8
                . '" class="dashAnchor"></a>'
194
            );
195 8
        });
196 8
    }
197
}
198