Completed
Push — master ( 4efb17...530cdd )
by Guillaume
04:48
created

TailwindCSS::updateTopPadding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 10
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 (basename($file) === 'components.html') {
83
            $parent = $crawler->filter('h1')->first();
84
85
            $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...
86
                $entries->push([
87
                    'name' => $this->cleanAnchorText($node->text()) . ' - ' . $parent->text(),
88
                    'type' => 'Sample',
89
                    '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

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