Completed
Push — master ( b1c748...4efb17 )
by Guillaume
05:04
created

TailwindCSS::updateContainerWidth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 20
ccs 17
cts 17
cp 1
rs 9.7333
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace App\Docsets;
4
5
use Illuminate\Support\Str;
6
use Wa72\HtmlPageDom\HtmlPage;
7
use Illuminate\Support\Collection;
8
use Wa72\HtmlPageDom\HtmlPageCrawler;
9
use Illuminate\Support\Facades\Storage;
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 = 'installation.html';
17
    public const PLAYGROUND = 'https://codesandbox.io/s/github/lbogdan/tailwindcss-playground';
18
    public const ICON_16 = 'favicon-16x16.png';
19
    public const ICON_32 = 'favicon-32x32.png';
20
    public const EXTERNAL_DOMAINS = [
21
        'refactoring-ui.nyc3.cdn.digitaloceanspaces.com'
22
    ];
23
24 1
    public function entries(string $file): Collection
25
    {
26 1
        $crawler = HtmlPageCrawler::create(Storage::get($file));
27
28 1
        $entries = collect();
29
30 1
        $entries = $entries->merge($this->environmentEntries($crawler, $file));
31 1
        $entries = $entries->merge($this->instructionEntries($crawler, $file));
32 1
        $entries = $entries->merge($this->sampleEntries($crawler, $file));
33 1
        $entries = $entries->merge($this->resourceEntries($crawler, $file));
34 1
        $entries = $entries->merge($this->guideEntries($crawler, $file));
35 1
        $entries = $entries->merge($this->sectionEntries($crawler, $file));
36
37 1
        return $entries;
38
    }
39
40 1
    protected function environmentEntries(HtmlPageCrawler $crawler, string $file)
41
    {
42 1
        $entries = collect();
43
44 1
        if (basename($file) === 'community.html') {
45
            $parent = $crawler->filter('h1')->first();
46
47
            $crawler->filter('h2')->each(function (HtmlPageCrawler $node) use ($entries, $file, $parent) {
48
                $entries->push([
49
                    'name' => $this->cleanAnchorText($node->text()) . ' - ' . $parent->text(),
50
                    'type' => 'Environment',
51
                    'path' => basename($file) . '#' . Str::slug($node->text()),
52
                ]);
53
            });
54
55
            return $entries;
56
        }
57 1
    }
58
59 1
    protected function instructionEntries(HtmlPageCrawler $crawler, string $file)
60
    {
61 1
        $entries = collect();
62
63 1
        if (basename($file) === 'screencasts.html') {
64
            $parent = $crawler->filter('h1')->first();
65
66
            $crawler->filter('span.relative')->each(function (HtmlPageCrawler $node) use ($entries, $file, $parent) {
0 ignored issues
show
Unused Code introduced by
The import $file is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
67
                $entries->push([
68
                    'name' => $this->cleanAnchorText($node->text()) . ' - ' . $parent->text(),
69
                    'type' => 'Instruction',
70
                    'path' => $node->parents('a')->first()->attr('href'),
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Component\DomCrawler\Crawler::parents() has too many arguments starting with 'a'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
                    'path' => $node->/** @scrutinizer ignore-call */ parents('a')->first()->attr('href'),

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
71
                ]);
72
            });
73
74
            return $entries;
75
        }
76 1
    }
77
78 1
    protected function sampleEntries(HtmlPageCrawler $crawler, string $file)
79
    {
80 1
        $entries = collect();
81
82 1
        if (in_array(basename($file), [
83 1
            'alerts.html',
84
            'buttons.html',
85
            'cards.html',
86
            'forms.html',
87
            'grids.html',
88
            'navigation.html'
89
        ])) {
90 1
            $parent = $crawler->filter('h1')->first();
91
92 1
            $entries->push([
93 1
                'name' => $this->cleanAnchorText($parent->text()),
94 1
                'type' => 'Sample',
95 1
                'path' => basename($file) . '#' . Str::slug($parent->text()),
96
            ]);
97
98
            $crawler->filter('h2')->each(function (HtmlPageCrawler $node) use ($entries, $file, $parent) {
99 1
                $entries->push([
100 1
                    'name' => $this->cleanAnchorText($node->text()) . ' - ' . $parent->text(),
101 1
                    'type' => 'Sample',
102 1
                    'path' => basename($file) . '#' . Str::slug($node->text()),
103
                ]);
104 1
            });
105
106 1
            return $entries;
107
        }
108 1
    }
109
110 1
    protected function resourceEntries(HtmlPageCrawler $crawler, string $file)
111
    {
112 1
        $entries = collect();
113
114 1
        if (basename($file) === 'resources.html') {
115 1
            $parent = $crawler->filter('h1')->first();
116
117
            $crawler->filter('h2')->each(function (HtmlPageCrawler $node) use ($entries, $file, $parent) {
118 1
                $entries->push([
119 1
                    'name' => $this->cleanAnchorText($node->text()) . ' - ' . $parent->text(),
120 1
                    'type' => 'Resource',
121 1
                    'path' => basename($file) . '#' . Str::slug($node->text()),
122
                ]);
123 1
            });
124
125 1
            return $entries;
126
        }
127 1
    }
128
129 1
    protected function guideEntries(HtmlPageCrawler $crawler, string $file)
130
    {
131 1
        $pageTitle = (new HtmlPage(Storage::get($file)))->getTitle();
132
133 1
        $entries = collect();
134
135 1
        if ($pageTitle === 'Installation - Tailwind CSS') {
136
            $crawler->filter('#navWrapper li a')->each(static function (HtmlPageCrawler $node) use ($entries) {
137 1
                $entries->push([
138 1
                    'name' => trim($node->text()),
139 1
                    'type' => 'Guide',
140 1
                    'path' => $node->attr('href'),
141
                ]);
142 1
            });
143
        }
144
145 1
        return $entries;
146
    }
147
148 1
    protected function sectionEntries(HtmlPageCrawler $crawler, string $file)
149
    {
150 1
        $entries = collect();
151
152 1
        $parent = $crawler->filter('h1')->first();
153
154
        $crawler->filter('h2')->each(function (HtmlPageCrawler $node) use ($entries, $file, $parent) {
155 1
            $entries->push([
156 1
                'name' => $this->cleanAnchorText($node->text()) . ' - ' . $parent->text(),
157 1
                'type' => 'Section',
158 1
                'path' => basename($file) . '#' . Str::slug($node->text()),
159
            ]);
160 1
        });
161
162 1
        return $entries;
163
    }
164
165 1
    public function format(string $html): string
166
    {
167 1
        $crawler = HtmlPageCrawler::create($html);
168
169 1
        $this->removeNavbarAndHeader($crawler);
170 1
        $this->removeLeftSidebar($crawler);
171 1
        $this->removeRightSidebar($crawler);
172 1
        $this->updateCSS($crawler);
173 1
        $this->insertDashTableOfContents($crawler);
174
175 1
        return $crawler->saveHTML();
176
    }
177
178 1
    protected function removeNavbarAndHeader(HtmlPageCrawler $crawler)
179
    {
180 1
        $crawler->filter('body > div:first-child')->remove();
181 1
    }
182
183 1
    protected function removeLeftSidebar(HtmlPageCrawler $crawler)
184
    {
185 1
        $crawler->filter('#sidebar')->remove();
186 1
    }
187
188 1
    protected function removeRightSidebar(HtmlPageCrawler $crawler)
189
    {
190 1
        $crawler->filter('#app div.flex > div.hidden')->remove();
191 1
    }
192
193 1
    protected function updateCSS(HtmlPageCrawler $crawler)
194
    {
195 1
        $this->updateTopPadding($crawler);
196 1
        $this->updateHeader($crawler);
197 1
        $this->updateContainerWidth($crawler);
198 1
        $this->updateBottomPadding($crawler);
199 1
    }
200
201 1
    protected function updateTopPadding(HtmlPageCrawler $crawler)
202
    {
203 1
        $crawler->filter('#app > div')
204 1
            ->removeClass('pt-12')
205 1
            ->removeClass('pt-24')
206 1
            ->removeClass('pb-16')
207 1
            ->removeClass('lg:pt-28')
208 1
            ->removeClass('lg:pt-12')
209
        ;
210 1
    }
211
212 1
    protected function updateHeader(HtmlPageCrawler $crawler)
213
    {
214 1
        $crawler->filter('#app > div > div.markdown')
215 1
            ->removeClass('lg:ml-0')
216 1
            ->removeClass('lg:mr-auto')
217 1
            ->removeClass('xl:mx-0')
218 1
            ->removeClass('xl:w-3/4')
219 1
            ->removeClass('max-w-3xl')
220 1
            ->removeClass('xl:px-12')
221
        ;
222 1
    }
223
224 1
    protected function updateContainerWidth(HtmlPageCrawler $crawler)
225
    {
226 1
        $crawler->filter('body > div:first-child')
227 1
            ->removeClass('max-w-screen-xl');
228
229 1
        $crawler->filter('#content-wrapper')
230 1
            ->removeClass('lg:static')
231 1
            ->removeClass('lg:max-h-full')
232 1
            ->removeClass('lg:overflow-visible')
233 1
            ->removeClass('lg:w-3/4')
234 1
            ->removeClass('xl:w-4/5');
235
236 1
        $crawler->filter('#app > div > div.flex > div.markdown')
237 1
            ->removeClass('xl:p-12')
238 1
            ->removeClass('max-w-3xl')
239 1
            ->removeClass('lg:ml-0')
240 1
            ->removeClass('lg:mr-auto')
241 1
            ->removeClass('xl:w-3/4')
242 1
            ->removeClass('xl:px-12')
243 1
            ->removeClass('xl:mx-0');
244 1
    }
245
246 1
    protected function updateBottomPadding(HtmlPageCrawler $crawler)
247
    {
248 1
        $crawler->filter('body')
249 1
            ->addClass('pb-8');
250 1
    }
251
252 1
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
253
    {
254 1
        $crawler->filter('h1')
255 1
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
256
257
        $crawler->filter('h2, h3')->each(function (HtmlPageCrawler $node) {
258 1
            $node->prepend(
259 1
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($this->cleanAnchorText($node->text())) . '" class="dashAnchor"></a>'
260
            );
261 1
        });
262 1
    }
263
264 2
    protected function cleanAnchorText($anchorText)
265
    {
266 2
        return trim(preg_replace('/\s+/', ' ', $anchorText));
267
    }
268
}
269