Passed
Push — master ( 4bdf38...ace002 )
by Guillaume
03:54
created

LaravelMix::entries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 9
ccs 5
cts 5
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 LaravelMix extends BaseDocset
12
{
13
    public const CODE = 'laravel-mix';
14
    public const NAME = 'Laravel Mix';
15
    public const URL = 'laravel-mix.com';
16
    public const INDEX = 'docs/main/installation.html';
17
    public const PLAYGROUND = '';
18
    public const ICON_16 = 'favicon-16x16.png';
19
    public const ICON_32 = 'favicon-32x32.png';
20
    public const EXTERNAL_DOMAINS = [];
21
22
23
    public function grab(): bool
24
    {
25
        /**
26
         * can't use PCRE regex style, so need to
27
         * type the whole list of shit (versions)
28
         * to ignore.
29
         */
30
        $toIgnore = implode('|', [
31
            'cdn-cgi',
32
            'docs/1.7/',
33
            'docs/2.0/',
34
            'docs/2.1/',
35
            'docs/3.0/',
36
            'docs/4.0/',
37
            'docs/4.1/',
38
            'docs/5.0/',
39
            'docs/6.0/',
40
        ]);
41
42
        system(
43
            "echo; wget laravel-mix.com/docs \
44
                --mirror \
45
                --trust-server-names \
46
                --reject-regex='{$toIgnore}' \
47
                --page-requisites \
48
                --adjust-extension \
49
                --convert-links \
50
                --span-hosts \
51
                --domains={$this->externalDomains()} \
52
                --directory-prefix=storage/{$this->downloadedDirectory()} \
53
                -e robots=off \
54
                --quiet \
55
                --show-progress",
56
            $result
57
        );
58
59
        return $result === 0;
60
    }
61
62 16
    public function entries(string $file): Collection
63
    {
64 16
        $crawler = HtmlPageCrawler::create(Storage::get($file));
65
66 16
        $entries = collect();
67
68 16
        $entries = $entries->union($this->guideEntries($crawler, $file));
69
70 16
        return $entries;
71
    }
72
73 16
    protected function guideEntries(HtmlPageCrawler $crawler, string $file)
74
    {
75 16
        $entries = collect();
76
77 16
        if (Str::contains($file, "{$this->url()}/docs/main/installation.html")) {
78 16
            $crawler->filter('nav#nav li a')->each(function (HtmlPageCrawler $node) use ($entries) {
79 16
                $entries->push([
80 16
                    'name' => trim($node->text()),
81 16
                    'type' => 'Guide',
82 16
                    'path' => $this->url() . '/docs/main/' . $node->attr('href'),
83
                ]);
84 16
            });
85
        }
86
87 16
        return $entries;
88
    }
89
90 16
    public function format(string $file): string
91
    {
92 16
        $crawler = HtmlPageCrawler::create(Storage::get($file));
93
94 16
        $this->removeHeader($crawler);
95 16
        $this->removeLeftSidebar($crawler);
96 16
        $this->removeFooter($crawler);
97
98 16
        $this->insertDashTableOfContents($crawler);
99
100 16
        return $crawler->saveHTML();
101
    }
102
103 16
    protected function removeHeader(HtmlPageCrawler $crawler)
104
    {
105 16
        $crawler->filter('header.sticky')->remove();
106 16
    }
107
108 16
    protected function removeLeftSidebar(HtmlPageCrawler $crawler)
109
    {
110 16
        $crawler->filter('#nav')->remove();
111 16
    }
112
113 16
    protected function removeFooter(HtmlPageCrawler $crawler)
114
    {
115 16
        $crawler->filter('footer.flex')->remove();
116 16
    }
117
118 16
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
119
    {
120 16
        $crawler->filter('body')
121 16
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
122
123 16
        $crawler->filter('h2, h3, h4')->each(function (HtmlPageCrawler $node) {
124 16
            $node->prepend(
125 16
                '<a id="' . Str::slug(
126 16
                    $node->text()
127 16
                ) . '" name="//apple_ref/cpp/Section/' . rawurlencode(
128 16
                    $node->text()
129 16
                ) . '" class="dashAnchor"></a>'
130
            );
131 16
        });
132 16
    }
133
}
134