Passed
Push — bulma ( 542731...288db5 )
by Guillaume
10:41
created

Bulma   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 257
Duplicated Lines 0 %

Test Coverage

Coverage 90.37%

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 25
eloc 125
c 5
b 1
f 2
dl 0
loc 257
ccs 122
cts 135
cp 0.9037
rs 10

20 Methods

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