Passed
Push — explore-fixing-classes ( f0f836...72f25e )
by Guillaume
17:18
created

TailwindCSS::removeFooter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
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 TailwindCSS extends BaseDocset
12
{
13
    public const CODE = 'tailwindcss';
14
    public const NAME = 'Tailwind CSS';
15
    public const URL = 'tailwindcss.com';
16
    public const INDEX = 'docs/installation.html';
17
    public const PLAYGROUND = 'https://play.tailwindcss.com/';
18
    public const ICON_16 = 'favicon-16x16.png';
19
    public const ICON_32 = 'favicon-32x32.png';
20
    public const EXTERNAL_DOMAINS = [
21
    ];
22
23
24
    public function grab(): bool
25
    {
26
        $toIgnore = implode('|', [
27
            'blog.tailwindcss.com',
28
            'v1.tailwindcss.com',
29
            'v2.tailwindcss.com'
30
        ]);
31
32
        system(
33
            "echo; wget tailwindcss.com/docs \
34
                --mirror \
35
                --trust-server-names \
36
                --reject-regex='{$toIgnore}' \
37
                --page-requisites \
38
                --adjust-extension \
39
                --convert-links \
40
                --span-hosts \
41
                --domains={$this->externalDomains()} \
42
                --directory-prefix=storage/{$this->downloadedDirectory()} \
43
                -e robots=off \
44
                --quiet \
45
                --show-progress",
46
            $result
47
        );
48
49
        return $result === 0;
50
    }
51
52
    public function entries(string $file): Collection
53
    {
54
        $crawler = HtmlPageCrawler::create(Storage::get($file));
55
56
        $entries = collect();
57
58
        $entries = $entries->union($this->resourceEntries($crawler, $file));
59
        $entries = $entries->union($this->guideEntries($crawler, $file));
60
        $entries = $entries->union($this->sectionEntries($crawler, $file));
61
62
        return $entries;
63
    }
64
65
    protected function resourceEntries(HtmlPageCrawler $crawler, string $file)
66
    {
67
        $entries = collect();
68
69
        if (Str::contains($file, "{$this->url()}/resources.html")) {
70
            $crawler->filter('h2')->each(function (HtmlPageCrawler $node) use ($entries, $file) {
71
                $entries->push([
72
                    'name' => $this->cleanAnchorText($node->text()),
73
                    'type' => 'Resource',
74
                    'path' => Str::after($file . '#' . Str::slug($node->text()), $this->innerDirectory()),
75
                ]);
76
            });
77
78
            $crawler->filter('h3')->each(function (HtmlPageCrawler $node) use ($entries, $file) {
79
                $entries->push([
80
                    'name' => $this->cleanAnchorText($node->text()),
81
                    'type' => 'Section',
82
                    'path' => Str::after($file . '#' . Str::slug($node->text()), $this->innerDirectory()),
83
                ]);
84
            });
85
86
            return $entries;
87
        }
88
    }
89
90
    protected function guideEntries(HtmlPageCrawler $crawler, string $file)
91
    {
92
        $entries = collect();
93
94
        if (Str::contains($file, "{$this->url()}/docs.html")) {
95
            $itemsToIgnore = collect(['Release Notes', 'Typography', 'Forms', 'Aspect Ratio', 'Line Clamp']);
96
97
            $crawler
98
                ->filter('nav#nav li.mt-8 a')
99
                ->each(function (HtmlPageCrawler $node) use ($entries, $itemsToIgnore) {
100
                    if (! $itemsToIgnore->contains($node->text())) {
101
                        $entries->push([
102
                            'name' => trim($node->text()),
103
                            'type' => 'Guide',
104
                            'path' => $this->url() . '/' . $node->attr('href'),
105
                        ]);
106
                    }
107
                });
108
        }
109
110
        return $entries;
111
    }
112
113
    protected function sectionEntries(HtmlPageCrawler $crawler, string $file)
114
    {
115
        $entries = collect();
116
117
        $crawler->filter('h2')->each(function (HtmlPageCrawler $node) use ($entries, $file) {
118
            $entries->push([
119
                'name' => $this->cleanAnchorText($node->text()),
120
                'type' => 'Section',
121
                'path' => Str::after($file . '#' . Str::slug($node->text()), $this->innerDirectory()),
122
            ]);
123
        });
124
125
        return $entries;
126
    }
127
128
    public function format(string $file): string
129
    {
130
        $crawler = HtmlPageCrawler::create(Storage::get($file));
131
132
        $this->removeTopbar($crawler);
133
        $this->removeLeftSidebar($crawler);
134
        $this->removeRightSidebar($crawler);
135
        $this->removeClassesOverflow($crawler);
136
137
        $this->updateContainerWidth($crawler);
138
        $this->removeFooter($crawler);
139
140
        $this->ignoreDarkModeForSomeColors($crawler);
141
142
        $this->removeUnwantedJavaScript($crawler);
143
144
        $this->insertOnlineRedirection($crawler, $file);
145
        $this->insertDashTableOfContents($crawler, $file);
146
147
        return $crawler->saveHTML();
148
    }
149
150
    protected function removeTopbar(HtmlPageCrawler $crawler)
151
    {
152
        $crawler->filter('div.sticky.top-0')->remove();
153
    }
154
155
    protected function removeLeftSidebar(HtmlPageCrawler $crawler)
156
    {
157
        $crawler->filter('div.hidden.fixed.z-20.inset-0.py-10.px-8.overflow-y-auto')->remove();
158
    }
159
160
    protected function removeRightSidebar(HtmlPageCrawler $crawler)
161
    {
162
        $crawler->filter('div.fixed.z-20.bottom-0.py-10.px-8.overflow-y-auto.hidden')->remove();
163
    }
164
165
    protected function removeClassesOverflow(HtmlPageCrawler $crawler)
166
    {
167
        $crawler->filter('#class-reference + div')
168
            ->removeClass('overflow-hidden')
169
            ->removeClass('lg:overflow-auto')
170
            ->addClass('overflow-auto')
171
        ;
172
173
        $crawler->filter('#class-reference ~ div:nth-of-type(2)')
174
            ->remove()
175
        ;
176
    }
177
178
    protected function updateContainerWidth(HtmlPageCrawler $crawler)
179
    {
180
        $crawler->filter('div.max-w-8xl.mx-auto.px-4 > div:first-child')
181
            ->removeClass('lg:pl-[19.5rem]')
182
        ;
183
        $crawler->filter('div.max-w-3xl.mx-auto.pt-10')
184
            ->removeClass('max-w-3xl')
185
            ->removeClass('mx-auto')
186
            ->removeClass('xl:max-w-none')
187
            ->removeClass('xl:mr-[15.5rem]')
188
            ->removeClass('xl:pr-16')
189
        ;
190
    }
191
192
    protected function removeFooter(HtmlPageCrawler $crawler)
193
    {
194
        $crawler->filter('div.pt-10.pb-28.border-t.border-gray-200.justify-between.text-gray-500')
195
            ->remove()
196
        ;
197
    }
198
199
    protected function ignoreDarkModeForSomeColors(HtmlPageCrawler $crawler)
200
    {
201
        $this->ignoreDarkModeForDefaultColorPaletteSection($crawler);
202
        $this->ignoreDarkModeForVariousColorTables($crawler);
203
    }
204
205
    protected function ignoreDarkModeForDefaultColorPaletteSection(HtmlPageCrawler $crawler)
206
    {
207
        $crawler->filter('div.h-10.w-full.rounded.ring-1.ring-inset')->addClass('dash-ignore-dark-mode');
208
    }
209
210
    protected function ignoreDarkModeForVariousColorTables(HtmlPageCrawler $crawler)
211
    {
212
        $crawler->filter('h2 + div td:last-child')->addClass('dash-ignore-dark-mode');
213
    }
214
215
    protected function removeUnwantedJavaScript(HtmlPageCrawler $crawler)
216
    {
217
        $crawler->filter('script')->remove();
218
    }
219
220
    protected function insertOnlineRedirection(HtmlPageCrawler $crawler, string $file)
221
    {
222
        $onlineUrl = Str::substr(Str::after($file, $this->innerDirectory()), 1, -5);
223
224
        $crawler->filter('html')->prepend("<!-- Online page at https://$onlineUrl -->");
225
    }
226
227
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler, string $file)
228
    {
229
        if (! Str::contains($file, "{$this->url()}/docs.html")) {
230
            $crawler->filter('body')
231
                ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
232
233
            $crawler->filter('h2, h3')->each(function (HtmlPageCrawler $node) {
234
                $node->prepend(
235
                    '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($this->cleanAnchorText($node->text())) . '" class="dashAnchor"></a>'
236
                );
237
            });
238
        }
239
    }
240
241
    protected function cleanAnchorText($anchorText)
242
    {
243
        return trim(preg_replace('/\s+/', ' ', $anchorText));
244
    }
245
}
246