Bulma::grab()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 22
c 2
b 0
f 1
dl 0
loc 38
ccs 0
cts 13
cp 0
rs 9.568
cc 1
nc 1
nop 0
crap 2
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\HtmlPage;
10
use Wa72\HtmlPageDom\HtmlPageCrawler;
11
12
class Bulma extends BaseDocset
13
{
14
    public const CODE = 'bulma';
15
    public const NAME = 'Bulma';
16
    public const URL = 'bulma.io';
17
    public const INDEX = 'documentation/index.html';
18
    public const PLAYGROUND = '';
19
    public const ICON_16 = 'favicons/favicon-16x16.png?v=201701041855';
20
    public const ICON_32 = 'favicons/favicon-32x32.png?v=201701041855';
21
    public const EXTERNAL_DOMAINS = [
22
        'use.fontawesome.com',
23
    ];
24
25
26
    public function grab(): bool
27
    {
28
        $toIgnore = implode('|', [
29
            '/versions.bulma.io',
30
        ]);
31
32
        $toGet = implode('|', [
33
            '\.css',
34
            '\.gif',
35
            '\.ico',
36
            '\.jpg',
37
            '\.js',
38
            '\.png',
39
            '\.svg',
40
            '\.webmanifest',
41
            '/documentation',
42
        ]);
43
44
        system(
45
            "echo; wget bulma.io/documentation \
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 12
    public function entries(string $file): Collection
67
    {
68 12
        $crawler = HtmlPageCrawler::create(Storage::get($file));
69
70 12
        $entries = collect();
71
72 12
        $entries = $entries->merge($this->helperEntries($crawler, $file));
73 12
        $entries = $entries->merge($this->elementEntries($crawler, $file));
74 12
        $entries = $entries->merge($this->componentEntries($crawler, $file));
75 12
        $entries = $entries->merge($this->guideEntries($crawler, $file));
76 12
        $entries = $entries->merge($this->sectionEntries($crawler, $file));
77
78 12
        return $entries;
79
    }
80
81 12
    protected function helperEntries(HtmlPageCrawler $crawler, string $file)
82
    {
83 12
        $pageTitle = (new HtmlPage(Storage::get($file)))->getTitle();
84
85 12
        $entries = collect();
86
87 12
        if ($pageTitle === 'Documentation | Bulma: Free, open source, and modern CSS framework based on Flexbox') {
88 12
            $crawler->filter(
89 12
                '#categories > div:nth-child(3) > ul a'
90
            )->each(function (HtmlPageCrawler $node) use ($entries) {
91 12
                $entries->push([
92 12
                    'name' => trim($node->text()),
93 12
                    'type' => 'Helper',
94 12
                    'path' => $this->url() . '/documentation/' . $node->attr('href'),
95
                ]);
96 12
            });
97
        }
98
99 12
        return $entries;
100
    }
101
102 12
    protected function elementEntries(HtmlPageCrawler $crawler, string $file)
103
    {
104 12
        $pageTitle = (new HtmlPage(Storage::get($file)))->getTitle();
105
106 12
        $entries = collect();
107
108 12
        if ($pageTitle === 'Documentation | Bulma: Free, open source, and modern CSS framework based on Flexbox') {
109 12
            $crawler->filter(
110 12
                '#categories > div:nth-child(7) > ul a'
111
            )->each(function (HtmlPageCrawler $node) use ($entries) {
112 12
                $entries->push([
113 12
                    'name' => trim($node->text()),
114 12
                    'type' => 'Element',
115 12
                    'path' => $this->url() . '/documentation/' . $node->attr('href'),
116
                ]);
117 12
            });
118
        }
119
120 12
        return $entries;
121
    }
122
123 12
    protected function componentEntries(HtmlPageCrawler $crawler, string $file)
124
    {
125 12
        $pageTitle = (new HtmlPage(Storage::get($file)))->getTitle();
126
127 12
        $entries = collect();
128
129 12
        if ($pageTitle === 'Documentation | Bulma: Free, open source, and modern CSS framework based on Flexbox') {
130 12
            $crawler->filter(
131 12
                '#categories > div:nth-child(8) > ul a'
132
            )->each(function (HtmlPageCrawler $node) use ($entries) {
133 12
                $entries->push([
134 12
                    'name' => trim($node->text()),
135 12
                    'type' => 'Component',
136 12
                    'path' => $this->url() . '/documentation/' . $node->attr('href'),
137
                ]);
138 12
            });
139
        }
140
141 12
        return $entries;
142
    }
143
144 12
    protected function guideEntries(HtmlPageCrawler $crawler, string $file)
145
    {
146 12
        $pageTitle = (new HtmlPage(Storage::get($file)))->getTitle();
147
148 12
        $entries = collect();
149
150 12
        if ($pageTitle === 'Documentation | Bulma: Free, open source, and modern CSS framework based on Flexbox') {
151 12
            $crawler->filter(
152 12
                'header.bd-category-header a.bd-category-name'
153
            )->each(function (HtmlPageCrawler $node) use ($entries) {
154 12
                $entries->push([
155 12
                    'name' => trim($node->text()),
156 12
                    'type' => 'Guide',
157 12
                    'path' => $this->url() . '/documentation/' . $node->attr('href'),
158
                ]);
159 12
            });
160
        }
161
162 12
        return $entries;
163
    }
164
165 12
    protected function sectionEntries(HtmlPageCrawler $crawler, string $file)
166
    {
167 12
        $pageTitle = (new HtmlPage(Storage::get($file)))->getTitle();
168
169 12
        $entries = collect();
170
171 12
        if ($pageTitle === 'Documentation | Bulma: Free, open source, and modern CSS framework based on Flexbox') {
172 12
            $crawler->filter(
173 12
                '#categories > div:not(:nth-child(3)):not(:nth-child(7)):not(:nth-child(8)) > ul a'
174
            )->each(function (HtmlPageCrawler $node) use ($entries) {
175 12
                $entries->push([
176 12
                    'name' => trim($node->text()),
177 12
                    'type' => 'Section',
178 12
                    'path' => $this->url() . '/documentation/' . $node->attr('href'),
179
                ]);
180 12
            });
181
        }
182
183 12
        return $entries;
184
    }
185
186 12
    public function format(string $file): string
187
    {
188 12
        $crawler = HtmlPageCrawler::create(Storage::get($file));
189
190 12
        $this->removeTopNotice($crawler);
191 12
        $this->removeNavbar($crawler);
192 12
        $this->removeBreadcrumb($crawler);
193 12
        $this->removeRightSidebar($crawler);
194 12
        $this->removeNavTabs($crawler);
195 12
        $this->removeNavPrevNext($crawler);
196 12
        $this->removeImproveOnGitHub($crawler);
197 12
        $this->removeSupportFooter($crawler);
198 12
        $this->removeBulmaBook($crawler);
199 12
        $this->removeNewsletter($crawler);
200 12
        $this->removeFooter($crawler);
201
202 12
        $this->removeUnwantedJavaScript($crawler);
203
204 12
        $this->insertDashTableOfContents($crawler);
205
206 12
        return $crawler->saveHTML();
207
    }
208
209 12
    protected function removeTopNotice(HtmlPageCrawler $crawler)
210
    {
211 12
        $crawler->filter('.bd-notice')->remove();
212 12
    }
213
214 12
    protected function removeNavbar(HtmlPageCrawler $crawler)
215
    {
216 12
        $crawler->filter('#navbar')->remove();
217 12
    }
218
219 12
    protected function removeBreadcrumb(HtmlPageCrawler $crawler)
220
    {
221 12
        $crawler->filter('.bd-breadcrumb')->remove();
222 12
    }
223
224 12
    protected function removeRightSidebar(HtmlPageCrawler $crawler)
225
    {
226 12
        $crawler->filter('aside.bd-side')->remove();
227 12
    }
228
229 12
    protected function removeNavTabs(HtmlPageCrawler $crawler)
230
    {
231 12
        $crawler->filter('nav.bd-tabs')->remove();
232 12
    }
233
234 12
    protected function removeNavPrevNext(HtmlPageCrawler $crawler)
235
    {
236 12
        $crawler->filter('nav.bd-prev-next-bis')->remove();
237 12
    }
238
239 12
    protected function removeImproveOnGitHub(HtmlPageCrawler $crawler)
240
    {
241 12
        $crawler->filter('#typo')->remove();
242 12
    }
243
244 12
    protected function removeSupportFooter(HtmlPageCrawler $crawler)
245
    {
246 12
        $crawler->filter('.bd-footer-support')->remove();
247 12
    }
248
249 12
    protected function removeFooter(HtmlPageCrawler $crawler)
250
    {
251 12
        $crawler->filter('footer.footer')->remove();
252 12
    }
253
254 12
    protected function removeUnwantedJavaScript(HtmlPageCrawler $crawler)
255
    {
256 12
        $crawler->filter("script[src*='native.js']")->remove();
257 12
        $crawler->filterXPath("//script[text()[contains(.,'connect.facebook.net')]]")->remove();
258 12
        $crawler->filter("script[src*='platform.twitter.com']")->remove();
259 12
        $crawler->filterXPath("//script[text()[contains(.,'www.google-analytics.com')]]")->remove();
260 12
    }
261
262 12
    protected function removeBulmaBook(HtmlPageCrawler $crawler)
263
    {
264 12
        $crawler->filter('#bulma-book')->remove();
265 12
    }
266
267 12
    protected function removeNewsletter(HtmlPageCrawler $crawler)
268
    {
269 12
        $crawler->filter('#newsletter')->remove();
270 12
    }
271
272 12
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
273
    {
274 12
        $crawler->filter('head')
275 12
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
276
277
        $crawler->filter('h3.bd-anchor-title')->each(static function (HtmlPageCrawler $node) {
278 6
            $node->before(
279 6
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>'
280
            );
281 12
        });
282 12
    }
283
}
284