Passed
Push — bulma ( 5973e8...542731 )
by Guillaume
10:19
created

Bulma   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 247
Duplicated Lines 0 %

Test Coverage

Coverage 89.6%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 25
eloc 115
c 4
b 0
f 2
dl 0
loc 247
ccs 112
cts 125
cp 0.896
rs 10

20 Methods

Rating   Name   Duplication   Size   Complexity  
A removeBulmaBook() 0 3 1
A removeNavTabs() 0 3 1
A removeSupportFooter() 0 3 1
A removeNavPrevNext() 0 3 1
A entries() 0 13 1
A format() 0 19 1
A guideEntries() 0 17 2
A removeImproveOnGitHub() 0 3 1
A sectionEntries() 0 17 2
A removeFooter() 0 3 1
A componentEntries() 0 17 2
A removeBreadcrumb() 0 3 1
A grab() 0 39 1
A elementEntries() 0 17 2
A removeNewsletter() 0 3 1
A helperEntries() 0 17 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
            $crawler->filter('#categories > div:nth-child(3) > ul a')->each(function (HtmlPageCrawler $node) use ($entries) {
88 12
                $entries->push([
89 12
                    'name' => trim($node->text()),
90 12
                    'type' => 'Helper',
91 12
                    'path' => $this->url() . '/documentation/' . $node->attr('href'),
92
                ]);
93 12
            });
94
        }
95
96 12
        return $entries;
97
    }
98
99 12
    protected function elementEntries(HtmlPageCrawler $crawler, string $file)
100
    {
101 12
        $pageTitle = (new HtmlPage(Storage::get($file)))->getTitle();
102
103 12
        $entries = collect();
104
105 12
        if ($pageTitle === 'Documentation | Bulma: Free, open source, and modern CSS framework based on Flexbox') {
106
            $crawler->filter('#categories > div:nth-child(7) > ul a')->each(function (HtmlPageCrawler $node) use ($entries) {
107 12
                $entries->push([
108 12
                    'name' => trim($node->text()),
109 12
                    'type' => 'Element',
110 12
                    'path' => $this->url() . '/documentation/' . $node->attr('href'),
111
                ]);
112 12
            });
113
        }
114
115 12
        return $entries;
116
    }
117
118 12
    protected function componentEntries(HtmlPageCrawler $crawler, string $file)
119
    {
120 12
        $pageTitle = (new HtmlPage(Storage::get($file)))->getTitle();
121
122 12
        $entries = collect();
123
124 12
        if ($pageTitle === 'Documentation | Bulma: Free, open source, and modern CSS framework based on Flexbox') {
125
            $crawler->filter('#categories > div:nth-child(8) > ul a')->each(function (HtmlPageCrawler $node) use ($entries) {
126 12
                $entries->push([
127 12
                    'name' => trim($node->text()),
128 12
                    'type' => 'Component',
129 12
                    'path' => $this->url() . '/documentation/' . $node->attr('href'),
130
                ]);
131 12
            });
132
        }
133
134 12
        return $entries;
135
    }
136
137 12
    protected function guideEntries(HtmlPageCrawler $crawler, string $file)
138
    {
139 12
        $pageTitle = (new HtmlPage(Storage::get($file)))->getTitle();
140
141 12
        $entries = collect();
142
143 12
        if ($pageTitle === 'Documentation | Bulma: Free, open source, and modern CSS framework based on Flexbox') {
144
            $crawler->filter('header.bd-category-header a.bd-category-name')->each(function (HtmlPageCrawler $node) use ($entries) {
145 12
                $entries->push([
146 12
                    'name' => trim($node->text()),
147 12
                    'type' => 'Guide',
148 12
                    'path' => $this->url() . '/documentation/' . $node->attr('href'),
149
                ]);
150 12
            });
151
        }
152
153 12
        return $entries;
154
    }
155
156 12
    protected function sectionEntries(HtmlPageCrawler $crawler, string $file)
157
    {
158 12
        $pageTitle = (new HtmlPage(Storage::get($file)))->getTitle();
159
160 12
        $entries = collect();
161
162 12
        if ($pageTitle === 'Documentation | Bulma: Free, open source, and modern CSS framework based on Flexbox') {
163
            $crawler->filter('aside li a')->each(function (HtmlPageCrawler $node) use ($entries) {
164 12
                $entries->push([
165 12
                    'name' => trim($node->text()),
166 12
                    'type' => 'Section',
167 12
                    'path' => $this->url() . '/documentation/' . $node->attr('href'),
168
                ]);
169 12
            });
170
        }
171
172 12
        return $entries;
173
    }
174
175 12
    public function format(string $file): string
176
    {
177 12
        $crawler = HtmlPageCrawler::create(Storage::get($file));
178
179 12
        $this->removeTopNotice($crawler);
180 12
        $this->removeNavbar($crawler);
181 12
        $this->removeBreadcrumb($crawler);
182 12
        $this->removeRightSidebar($crawler);
183 12
        $this->removeNavTabs($crawler);
184 12
        $this->removeNavPrevNext($crawler);
185 12
        $this->removeImproveOnGitHub($crawler);
186 12
        $this->removeSupportFooter($crawler);
187 12
        $this->removeBulmaBook($crawler);
188 12
        $this->removeNewsletter($crawler);
189 12
        $this->removeFooter($crawler);
190
191 12
        $this->insertDashTableOfContents($crawler);
192
193 12
        return $crawler->saveHTML();
194
    }
195
196 12
    protected function removeTopNotice(HtmlPageCrawler $crawler)
197
    {
198 12
        $crawler->filter('.bd-notice')->remove();
199 12
    }
200
201 12
    protected function removeNavbar(HtmlPageCrawler $crawler)
202
    {
203 12
        $crawler->filter('#navbar')->remove();
204 12
    }
205
206 12
    protected function removeBreadcrumb(HtmlPageCrawler $crawler)
207
    {
208 12
        $crawler->filter('.bd-breadcrumb')->remove();
209 12
    }
210
211 12
    protected function removeRightSidebar(HtmlPageCrawler $crawler)
212
    {
213 12
        $crawler->filter('aside.bd-side')->remove();
214 12
    }
215
216 12
    protected function removeNavTabs(HtmlPageCrawler $crawler)
217
    {
218 12
        $crawler->filter('nav.bd-tabs')->remove();
219 12
    }
220
221 12
    protected function removeNavPrevNext(HtmlPageCrawler $crawler)
222
    {
223 12
        $crawler->filter('nav.bd-prev-next-bis')->remove();
224 12
    }
225
226 12
    protected function removeImproveOnGitHub(HtmlPageCrawler $crawler)
227
    {
228 12
        $crawler->filter('#typo')->remove();
229 12
    }
230
231 12
    protected function removeSupportFooter(HtmlPageCrawler $crawler)
232
    {
233 12
        $crawler->filter('.bd-footer-support')->remove();
234 12
    }
235
236 12
    protected function removeFooter(HtmlPageCrawler $crawler)
237
    {
238 12
        $crawler->filter('footer.footer')->remove();
239 12
    }
240
241 12
    protected function removeBulmaBook(HtmlPageCrawler $crawler)
242
    {
243 12
        $crawler->filter('#bulma-book')->remove();
244 12
    }
245
246 12
    protected function removeNewsletter(HtmlPageCrawler $crawler)
247
    {
248 12
        $crawler->filter('#newsletter')->remove();
249 12
    }
250
251 12
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
252
    {
253 12
        $crawler->filter('head')
254 12
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
255
256
        $crawler->filter('h3.bd-anchor-title')->each(static function (HtmlPageCrawler $node) {
257 6
            $node->before(
258 6
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>'
259
            );
260 12
        });
261 12
    }
262
}
263