Alpinejs::updateBottomPadding()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 1
b 0
f 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\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 8
    public function entries(string $file): Collection
46
    {
47 8
        $crawler = HtmlPageCrawler::create(Storage::get($file));
48
49 8
        $entries = collect();
50
51 8
        $entries = $entries->merge($this->guideEntries($crawler, $file));
52 8
        $entries = $entries->merge($this->sectionEntries($crawler, $file));
53
54 8
        return $entries;
55
    }
56
57 8
    protected function guideEntries(HtmlPageCrawler $crawler, string $file)
58
    {
59 8
        $entries = collect();
60
61 8
        if (Str::contains($file, "{$this->url()}/start-here.html")) {
62 8
            $crawler->filter('body > aside:first-of-type ul > li > ul > li')->each(function (HtmlPageCrawler $node) use ($entries) {
63 8
                $entries->push([
64 8
                    'name' => trim($node->text()),
65 8
                    'type' => 'Guide',
66 8
                    'path' => $this->url() . '/' . $node->children('a')->attr('href')
67 4
                ]);
68 8
            });
69
        }
70
71 8
        return $entries;
72
    }
73
74 8
    protected function sectionEntries(HtmlPageCrawler $crawler, string $file)
75
    {
76 8
        $entries = collect();
77
78 8
        $crawler->filter('main > div > h2')->each(function (HtmlPageCrawler $node) use ($entries, $file) {
79 8
            if ($node->children('a')->count() != 0) {
80 8
                $entries->push([
81 8
                    'name' => trim($node->text()),
82 8
                    'type' => 'Section',
83 8
                    'path' => Str::after($file . '#' . Str::slug($node->text()), $this->innerDirectory()),
84 4
                ]);
85
            }
86 8
        });
87
88 8
        return $entries;
89
    }
90
91 8
    public function format(string $file): string
92
    {
93 8
        $crawler = HtmlPageCrawler::create(Storage::get($file));
94
95 8
        $this->removeHeader($crawler);
96 8
        $this->removeLeftSidebar($crawler);
97 8
        $this->removeRightSidebar($crawler);
98 8
        $this->updateTopPadding($crawler);
99 8
        $this->updateLeftPadding($crawler);
100 8
        $this->updateContainerWidthAndMargins($crawler);
101 8
        $this->updateBottomPadding($crawler);
102
103 8
        $this->insertOnlineRedirection($crawler, $file);
104 8
        $this->insertDashTableOfContents($crawler);
105
106 8
        return $crawler->saveHTML();
107
    }
108
109 8
    protected function removeHeader(HtmlPageCrawler $crawler)
110
    {
111 8
        $crawler->filter('header')->remove();
112 4
    }
113
114 8
    protected function removeLeftSidebar(HtmlPageCrawler $crawler)
115
    {
116 8
        $crawler->filter('body > aside:first-of-type')->remove();
117 4
    }
118
119 8
    protected function removeRightSidebar(HtmlPageCrawler $crawler)
120
    {
121 8
        $crawler->filter('body > aside:last-of-type')->remove();
122 4
    }
123
124 8
    protected function updateTopPadding(HtmlPageCrawler $crawler)
125
    {
126 8
        $crawler->filter('main')
127 8
            ->removeClass('pt-24')
128 8
            ->addClass('pt-6')
129 4
        ;
130 4
    }
131
132 8
    protected function updateLeftPadding(HtmlPageCrawler $crawler)
133
    {
134 8
        $crawler->filter('main')
135 8
            ->removeClass('md:pl-48')
136 8
            ->removeClass('lg:pl-64')
137 8
            ->removeClass('pl-0')
138 8
            ->addClass('pl-6')
139 8
            ->removeClass('xl:pr-64')
140 8
            ->removeClass('pr-0')
141 8
            ->addClass('pr-6')
142 4
        ;
143 4
    }
144
145 8
    protected function updateContainerWidthAndMargins(HtmlPageCrawler $crawler)
146
    {
147 8
        $crawler->filter('main > div:first-of-type')
148 8
            ->removeClass('m-auto')
149 8
            ->removeClass('max-w-3xl')
150 4
        ;
151 4
    }
152
153 8
    protected function updateBottomPadding(HtmlPageCrawler $crawler)
154
    {
155 8
        $crawler->filter('main > div:first-of-type')
156 8
            ->removeClass('pb-24')
157 8
            ->addClass('pb-6')
158 4
        ;
159 4
    }
160
161 8
    protected function insertOnlineRedirection(HtmlPageCrawler $crawler, string $file)
162
    {
163 8
        $onlineUrl = Str::substr(Str::after($file, $this->innerDirectory()), 1, -5);
164
165 8
        $crawler->filter('html')->prepend("<!-- Online page at https://$onlineUrl -->");
166 4
    }
167
168 8
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
169
    {
170 8
        $crawler->filter('head')
171 8
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
172
173 8
        $crawler->filter('h2, h3')->each(function (HtmlPageCrawler $node) {
174 8
            $node->prepend(
175 4
                '<a id="'
176 8
                . Str::slug($node->text())
177 8
                . '" name="//apple_ref/cpp/Section/'
178 8
                . rawurlencode($node->text())
179 8
                . '" class="dashAnchor"></a>'
180 4
            );
181 8
        });
182 4
    }
183
}
184