Passed
Push — master ( 54eb84...bbd757 )
by Guillaume
10:35
created

Bulma::removeBulmaBook()   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 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
                --header 'Cookie: javascript_enabled_detect=true' \
49
                --reject-regex='{$toIgnore}' \
50
                --accept-regex='{$toGet}' \
51
                --ignore-case \
52
                --page-requisites \
53
                --adjust-extension \
54
                --convert-links \
55
                --span-hosts \
56
                --domains={$this->externalDomains()} \
57
                --directory-prefix=storage/{$this->downloadedDirectory()} \
58
                -e robots=off \
59
                --quiet \
60
                --show-progress",
61
            $result
62
        );
63
64
        return $result === 0;
65
    }
66
67 12
    public function entries(string $file): Collection
68
    {
69 12
        $crawler = HtmlPageCrawler::create(Storage::get($file));
70
71 12
        $entries = collect();
72
73 12
        $entries = $entries->merge($this->helperEntries($crawler, $file));
74 12
        $entries = $entries->merge($this->elementEntries($crawler, $file));
75 12
        $entries = $entries->merge($this->componentEntries($crawler, $file));
76 12
        $entries = $entries->merge($this->guideEntries($crawler, $file));
77 12
        $entries = $entries->merge($this->sectionEntries($crawler, $file));
78
79 12
        return $entries;
80
    }
81
82 12
    protected function helperEntries(HtmlPageCrawler $crawler, string $file)
83
    {
84 12
        $pageTitle = (new HtmlPage(Storage::get($file)))->getTitle();
85
86 12
        $entries = collect();
87
88 12
        if ($pageTitle === 'Documentation | Bulma: Free, open source, and modern CSS framework based on Flexbox') {
89 12
            $crawler->filter(
90 12
                '#categories > div:nth-child(3) > ul a'
91
            )->each(function (HtmlPageCrawler $node) use ($entries) {
92 12
                $entries->push([
93 12
                    'name' => trim($node->text()),
94 12
                    'type' => 'Helper',
95 12
                    'path' => $this->url() . '/documentation/' . $node->attr('href'),
96
                ]);
97 12
            });
98
        }
99
100 12
        return $entries;
101
    }
102
103 12
    protected function elementEntries(HtmlPageCrawler $crawler, string $file)
104
    {
105 12
        $pageTitle = (new HtmlPage(Storage::get($file)))->getTitle();
106
107 12
        $entries = collect();
108
109 12
        if ($pageTitle === 'Documentation | Bulma: Free, open source, and modern CSS framework based on Flexbox') {
110 12
            $crawler->filter(
111 12
                '#categories > div:nth-child(7) > ul a'
112
            )->each(function (HtmlPageCrawler $node) use ($entries) {
113 12
                $entries->push([
114 12
                    'name' => trim($node->text()),
115 12
                    'type' => 'Element',
116 12
                    'path' => $this->url() . '/documentation/' . $node->attr('href'),
117
                ]);
118 12
            });
119
        }
120
121 12
        return $entries;
122
    }
123
124 12
    protected function componentEntries(HtmlPageCrawler $crawler, string $file)
125
    {
126 12
        $pageTitle = (new HtmlPage(Storage::get($file)))->getTitle();
127
128 12
        $entries = collect();
129
130 12
        if ($pageTitle === 'Documentation | Bulma: Free, open source, and modern CSS framework based on Flexbox') {
131 12
            $crawler->filter(
132 12
                '#categories > div:nth-child(8) > ul a'
133
            )->each(function (HtmlPageCrawler $node) use ($entries) {
134 12
                $entries->push([
135 12
                    'name' => trim($node->text()),
136 12
                    'type' => 'Component',
137 12
                    'path' => $this->url() . '/documentation/' . $node->attr('href'),
138
                ]);
139 12
            });
140
        }
141
142 12
        return $entries;
143
    }
144
145 12
    protected function guideEntries(HtmlPageCrawler $crawler, string $file)
146
    {
147 12
        $pageTitle = (new HtmlPage(Storage::get($file)))->getTitle();
148
149 12
        $entries = collect();
150
151 12
        if ($pageTitle === 'Documentation | Bulma: Free, open source, and modern CSS framework based on Flexbox') {
152 12
            $crawler->filter(
153 12
                'header.bd-category-header a.bd-category-name'
154
            )->each(function (HtmlPageCrawler $node) use ($entries) {
155 12
                $entries->push([
156 12
                    'name' => trim($node->text()),
157 12
                    'type' => 'Guide',
158 12
                    'path' => $this->url() . '/documentation/' . $node->attr('href'),
159
                ]);
160 12
            });
161
        }
162
163 12
        return $entries;
164
    }
165
166 12
    protected function sectionEntries(HtmlPageCrawler $crawler, string $file)
167
    {
168 12
        $pageTitle = (new HtmlPage(Storage::get($file)))->getTitle();
169
170 12
        $entries = collect();
171
172 12
        if ($pageTitle === 'Documentation | Bulma: Free, open source, and modern CSS framework based on Flexbox') {
173 12
            $crawler->filter(
174 12
                '#categories > div:not(:nth-child(3)):not(:nth-child(7)):not(:nth-child(8)) > ul a'
175
            )->each(function (HtmlPageCrawler $node) use ($entries) {
176 12
                $entries->push([
177 12
                    'name' => trim($node->text()),
178 12
                    'type' => 'Section',
179 12
                    'path' => $this->url() . '/documentation/' . $node->attr('href'),
180
                ]);
181 12
            });
182
        }
183
184 12
        return $entries;
185
    }
186
187 12
    public function format(string $file): string
188
    {
189 12
        $crawler = HtmlPageCrawler::create(Storage::get($file));
190
191 12
        $this->removeTopNotice($crawler);
192 12
        $this->removeNavbar($crawler);
193 12
        $this->removeBreadcrumb($crawler);
194 12
        $this->removeRightSidebar($crawler);
195 12
        $this->removeNavTabs($crawler);
196 12
        $this->removeNavPrevNext($crawler);
197 12
        $this->removeImproveOnGitHub($crawler);
198 12
        $this->removeSupportFooter($crawler);
199 12
        $this->removeBulmaBook($crawler);
200 12
        $this->removeNewsletter($crawler);
201 12
        $this->removeFooter($crawler);
202
203 12
        $this->removeUnwantedJavaScript($crawler);
204
205 12
        $this->insertDashTableOfContents($crawler);
206
207 12
        return $crawler->saveHTML();
208
    }
209
210 12
    protected function removeTopNotice(HtmlPageCrawler $crawler)
211
    {
212 12
        $crawler->filter('.bd-notice')->remove();
213 12
    }
214
215 12
    protected function removeNavbar(HtmlPageCrawler $crawler)
216
    {
217 12
        $crawler->filter('#navbar')->remove();
218 12
    }
219
220 12
    protected function removeBreadcrumb(HtmlPageCrawler $crawler)
221
    {
222 12
        $crawler->filter('.bd-breadcrumb')->remove();
223 12
    }
224
225 12
    protected function removeRightSidebar(HtmlPageCrawler $crawler)
226
    {
227 12
        $crawler->filter('aside.bd-side')->remove();
228 12
    }
229
230 12
    protected function removeNavTabs(HtmlPageCrawler $crawler)
231
    {
232 12
        $crawler->filter('nav.bd-tabs')->remove();
233 12
    }
234
235 12
    protected function removeNavPrevNext(HtmlPageCrawler $crawler)
236
    {
237 12
        $crawler->filter('nav.bd-prev-next-bis')->remove();
238 12
    }
239
240 12
    protected function removeImproveOnGitHub(HtmlPageCrawler $crawler)
241
    {
242 12
        $crawler->filter('#typo')->remove();
243 12
    }
244
245 12
    protected function removeSupportFooter(HtmlPageCrawler $crawler)
246
    {
247 12
        $crawler->filter('.bd-footer-support')->remove();
248 12
    }
249
250 12
    protected function removeFooter(HtmlPageCrawler $crawler)
251
    {
252 12
        $crawler->filter('footer.footer')->remove();
253 12
    }
254
255 12
    protected function removeUnwantedJavaScript(HtmlPageCrawler $crawler)
256
    {
257 12
        $crawler->filter("script[src*='native.js']")->remove();
258 12
        $crawler->filterXPath("//script[text()[contains(.,'connect.facebook.net')]]")->remove();
259 12
        $crawler->filter("script[src*='platform.twitter.com']")->remove();
260 12
        $crawler->filterXPath("//script[text()[contains(.,'www.google-analytics.com')]]")->remove();
261 12
    }
262
263 12
    protected function removeBulmaBook(HtmlPageCrawler $crawler)
264
    {
265 12
        $crawler->filter('#bulma-book')->remove();
266 12
    }
267
268 12
    protected function removeNewsletter(HtmlPageCrawler $crawler)
269
    {
270 12
        $crawler->filter('#newsletter')->remove();
271 12
    }
272
273 12
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
274
    {
275 12
        $crawler->filter('head')
276 12
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
277
278
        $crawler->filter('h3.bd-anchor-title')->each(static function (HtmlPageCrawler $node) {
279 6
            $node->before(
280 6
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>'
281
            );
282 12
        });
283 12
    }
284
}
285