Passed
Push — explore-v3 ( 3dd231...950715 )
by Guillaume
17:15
created

Alpinejs   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Importance

Changes 9
Bugs 1 Features 1
Metric Value
eloc 85
c 9
b 1
f 1
dl 0
loc 169
rs 10
wmc 16

14 Methods

Rating   Name   Duplication   Size   Complexity  
A grab() 0 20 1
A entries() 0 10 1
A removeLeftSidebar() 0 3 1
A updateContainerWidthAndMargins() 0 5 1
A removeRightSidebar() 0 3 1
A updateBottomPadding() 0 5 1
A updateTopPadding() 0 5 1
A removeHeader() 0 3 1
A insertDashTableOfContents() 0 12 1
A insertOnlineRedirection() 0 5 1
A format() 0 16 1
A updateLeftPadding() 0 10 1
A sectionEntries() 0 15 2
A guideEntries() 0 15 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\HtmlPageCrawler;
10
11
class Alpinejs extends BaseDocset
12
{
13
    public const CODE = 'alpinejs';
14
    public const NAME = 'Alpine.js';
15
    public const URL = 'alpinejs.dev';
16
    public const INDEX = 'start-here.html';
17
    public const PLAYGROUND = 'https://alpinejs.codewithhugo.com';
18
    public const ICON_16 = '../../icons/icon.png';
19
    public const ICON_32 = '../../icons/[email protected]';
20
    public const EXTERNAL_DOMAINS = [];
21
22
23
    public function grab(): bool
24
    {
25
        system(
26
            "echo; wget github.com/alpinejs/alpine \
27
                --mirror \
28
                --trust-server-names \
29
                --ignore-case \
30
                --page-requisites \
31
                --adjust-extension \
32
                --convert-links \
33
                --span-hosts \
34
                --domains={$this->externalDomains()} \
35
                --directory-prefix=storage/{$this->downloadedDirectory()} \
36
                -e robots=off \
37
                --quiet \
38
                --show-progress",
39
            $result
40
        );
41
42
        return $result === 0;
43
    }
44
45
    public function entries(string $file): Collection
46
    {
47
        $crawler = HtmlPageCrawler::create(Storage::get($file));
48
49
        $entries = collect();
50
51
        $entries = $entries->merge($this->guideEntries($crawler, $file));
52
        $entries = $entries->merge($this->sectionEntries($crawler, $file));
53
54
        return $entries;
55
    }
56
57
    protected function guideEntries(HtmlPageCrawler $crawler, string $file)
58
    {
59
        $entries = collect();
60
61
        if (Str::contains($file, "{$this->url()}/start-here.html")) {
62
            $crawler->filter('body > aside:first-of-type ul > li > ul > li')->each(function (HtmlPageCrawler $node) use ($entries) {
63
                $entries->push([
64
                    'name' => trim($node->text()),
65
                    'type' => 'Guide',
66
                    'path' => $this->url() . '/' . $node->children('a')->attr('href')
67
                ]);
68
            });
69
        }
70
71
        return $entries;
72
    }
73
74
    protected function sectionEntries(HtmlPageCrawler $crawler, string $file)
75
    {
76
        $entries = collect();
77
78
        $crawler->filter('main > div > h2')->each(function (HtmlPageCrawler $node) use ($entries, $file) {
79
            if ($node->children('a')->count() != 0) {
80
                $entries->push([
81
                    'name' => trim($node->text()),
82
                    'type' => 'Section',
83
                    'path' => Str::after($file . '#' . Str::slug($node->text()), $this->innerDirectory()),
84
                ]);
85
            }
86
        });
87
88
        return $entries;
89
    }
90
91
    public function format(string $file): string
92
    {
93
        $crawler = HtmlPageCrawler::create(Storage::get($file));
94
95
        $this->removeHeader($crawler);
96
        $this->removeLeftSidebar($crawler);
97
        $this->removeRightSidebar($crawler);
98
        $this->updateTopPadding($crawler);
99
        $this->updateLeftPadding($crawler);
100
        $this->updateContainerWidthAndMargins($crawler);
101
        $this->updateBottomPadding($crawler);
102
103
        $this->insertOnlineRedirection($crawler, $file);
104
        $this->insertDashTableOfContents($crawler);
105
106
        return $crawler->saveHTML();
107
    }
108
109
    protected function removeHeader(HtmlPageCrawler $crawler)
110
    {
111
        $crawler->filter('header')->remove();
112
    }
113
114
    protected function removeLeftSidebar(HtmlPageCrawler $crawler)
115
    {
116
        $crawler->filter('body > aside:first-of-type')->remove();
117
    }
118
119
    protected function removeRightSidebar(HtmlPageCrawler $crawler)
120
    {
121
        $crawler->filter('body > aside:last-of-type')->remove();
122
    }
123
124
    protected function updateTopPadding(HtmlPageCrawler $crawler)
125
    {
126
        $crawler->filter('main')
127
            ->removeClass('pt-24')
128
            ->addClass('pt-6')
129
        ;
130
    }
131
132
    protected function updateLeftPadding(HtmlPageCrawler $crawler)
133
    {
134
        $crawler->filter('main')
135
            ->removeClass('md:pl-48')
136
            ->removeClass('lg:pl-64')
137
            ->removeClass('pl-0')
138
            ->addClass('pl-6')
139
            ->removeClass('xl:pr-64')
140
            ->removeClass('pr-0')
141
            ->addClass('pr-6')
142
        ;
143
    }
144
145
    protected function updateContainerWidthAndMargins(HtmlPageCrawler $crawler)
146
    {
147
        $crawler->filter('main > div:first-of-type')
148
            ->removeClass('m-auto')
149
            ->removeClass('max-w-3xl')
150
        ;
151
    }
152
153
    protected function updateBottomPadding(HtmlPageCrawler $crawler)
154
    {
155
        $crawler->filter('main > div:first-of-type')
156
            ->removeClass('pb-24')
157
            ->addClass('pb-6')
158
        ;
159
    }
160
161
    protected function insertOnlineRedirection(HtmlPageCrawler $crawler, string $file)
162
    {
163
        $onlineUrl = Str::substr(Str::after($file, $this->innerDirectory()), 1, -5);
164
165
        $crawler->filter('html')->prepend("<!-- Online page at https://$onlineUrl -->");
166
    }
167
168
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
169
    {
170
        $crawler->filter('head')
171
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
172
173
        $crawler->filter('h2, h3')->each(function (HtmlPageCrawler $node) {
174
            $node->prepend(
175
                '<a id="'
176
                . Str::slug($node->text())
177
                . '" name="//apple_ref/cpp/Section/'
178
                . rawurlencode($node->text())
179
                . '" class="dashAnchor"></a>'
180
            );
181
        });
182
    }
183
}
184