Passed
Push — master ( ffcb39...bb709a )
by Guillaume
03:57
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 0
Metric Value
eloc 16
dl 0
loc 20
ccs 17
cts 17
cp 1
rs 9.7333
c 0
b 0
f 0
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->sampleEntries($crawler, $file));
31 1
        $entries = $entries->merge($this->resourceEntries($crawler, $file));
32 1
        $entries = $entries->merge($this->guideEntries($crawler, $file));
33 1
        $entries = $entries->merge($this->sectionEntries($crawler, $file));
34
35 1
        return $entries;
36
    }
37
38 1
    protected function sampleEntries(HtmlPageCrawler $crawler, string $file)
39
    {
40 1
        $entries = collect();
41
42 1
        if (in_array(basename($file), [
43 1
            'alerts.html',
44
            'buttons.html',
45
            'cards.html',
46
            'forms.html',
47
            'grids.html',
48
            'navigation.html'
49
        ])) {
50
            $parent = $crawler->filter('h1')->first();
51
52
            $entries->push([
53
                'name' => $this->cleanAnchorText($parent->text()),
54
                'type' => 'Sample',
55
                'path' => basename($file) . '#' . Str::slug($parent->text()),
56
            ]);
57
58
            $crawler->filter('h2')->each(function (HtmlPageCrawler $node) use ($entries, $file, $parent) {
59
                $entries->push([
60
                    'name' => $this->cleanAnchorText($node->text()) . ' - ' . $parent->text(),
61
                    'type' => 'Sample',
62
                    'path' => basename($file) . '#' . Str::slug($node->text()),
63
                ]);
64
            });
65
66
            return $entries;
67
        }
68 1
    }
69
70 1
    protected function resourceEntries(HtmlPageCrawler $crawler, string $file)
71
    {
72 1
        $entries = collect();
73
74 1
        if (basename($file) === 'resources.html') {
75
            $parent = $crawler->filter('h1')->first();
76
77
            $entries->push([
78
                'name' => $this->cleanAnchorText($parent->text()),
79
                'type' => 'Resource',
80
                'path' => basename($file) . '#' . Str::slug($parent->text()),
81
            ]);
82
83
            $crawler->filter('h2')->each(function (HtmlPageCrawler $node) use ($entries, $file, $parent) {
84
                $entries->push([
85
                    'name' => $this->cleanAnchorText($node->text()) . ' - ' . $parent->text(),
86
                    'type' => 'Resource',
87
                    'path' => basename($file) . '#' . Str::slug($node->text()),
88
                ]);
89
            });
90
91
            return $entries;
92
        }
93 1
    }
94
95 1
    protected function guideEntries(HtmlPageCrawler $crawler, string $file)
96
    {
97 1
        $pageTitle = (new HtmlPage(Storage::get($file)))->getTitle();
98
99 1
        $entries = collect();
100
101 1
        if ($pageTitle === 'Installation - Tailwind CSS') {
102
            $crawler->filter('#navWrapper li a')->each(static function (HtmlPageCrawler $node) use ($entries) {
103 1
                $entries->push([
104 1
                    'name' => trim($node->text()),
105 1
                    'type' => 'Guide',
106 1
                    'path' => $node->attr('href'),
107
                ]);
108 1
            });
109
        }
110
111 1
        return $entries;
112
    }
113
114 1
    protected function sectionEntries(HtmlPageCrawler $crawler, string $file)
115
    {
116 1
        $entries = collect();
117
118 1
        $parent = $crawler->filter('h1')->first();
119
120
        $crawler->filter('h2')->each(function (HtmlPageCrawler $node) use ($entries, $file, $parent) {
121 1
            $entries->push([
122 1
                'name' => $this->cleanAnchorText($node->text()) . ' - ' . $parent->text(),
123 1
                'type' => 'Section',
124 1
                'path' => basename($file) . '#' . Str::slug($node->text()),
125
            ]);
126 1
        });
127
128 1
        return $entries;
129
    }
130
131 1
    public function format(string $html): string
132
    {
133 1
        $crawler = HtmlPageCrawler::create($html);
134
135 1
        $this->removeNavbarAndHeader($crawler);
136 1
        $this->removeLeftSidebar($crawler);
137 1
        $this->removeRightSidebar($crawler);
138 1
        $this->updateCSS($crawler);
139 1
        $this->insertDashTableOfContents($crawler);
140
141 1
        return $crawler->saveHTML();
142
    }
143
144 1
    protected function removeNavbarAndHeader(HtmlPageCrawler $crawler)
145
    {
146 1
        $crawler->filter('body > div:first-child')->remove();
147 1
    }
148
149 1
    protected function removeLeftSidebar(HtmlPageCrawler $crawler)
150
    {
151 1
        $crawler->filter('#sidebar')->remove();
152 1
    }
153
154 1
    protected function removeRightSidebar(HtmlPageCrawler $crawler)
155
    {
156 1
        $crawler->filter('#app div.flex > div.hidden')->remove();
157 1
    }
158
159 1
    protected function updateCSS(HtmlPageCrawler $crawler)
160
    {
161 1
        $this->updateTopPadding($crawler);
162 1
        $this->updateHeader($crawler);
163 1
        $this->updateContainerWidth($crawler);
164 1
        $this->updateBottomPadding($crawler);
165 1
    }
166
167 1
    protected function updateTopPadding(HtmlPageCrawler $crawler)
168
    {
169 1
        $crawler->filter('#app > div')
170 1
            ->removeClass('pt-12')
171 1
            ->removeClass('pt-24')
172 1
            ->removeClass('pb-16')
173 1
            ->removeClass('lg:pt-28')
174 1
            ->removeClass('lg:pt-12')
175
        ;
176 1
    }
177
178 1
    protected function updateHeader(HtmlPageCrawler $crawler)
179
    {
180 1
        $crawler->filter('#app > div > div.markdown')
181 1
            ->removeClass('lg:ml-0')
182 1
            ->removeClass('lg:mr-auto')
183 1
            ->removeClass('xl:mx-0')
184 1
            ->removeClass('xl:w-3/4')
185 1
            ->removeClass('max-w-3xl')
186 1
            ->removeClass('xl:px-12')
187
        ;
188 1
    }
189
190 1
    protected function updateContainerWidth(HtmlPageCrawler $crawler)
191
    {
192 1
        $crawler->filter('body > div:first-child')
193 1
            ->removeClass('max-w-screen-xl');
194
195 1
        $crawler->filter('#content-wrapper')
196 1
            ->removeClass('lg:static')
197 1
            ->removeClass('lg:max-h-full')
198 1
            ->removeClass('lg:overflow-visible')
199 1
            ->removeClass('lg:w-3/4')
200 1
            ->removeClass('xl:w-4/5');
201
202 1
        $crawler->filter('#app > div > div.flex > div.markdown')
203 1
            ->removeClass('xl:p-12')
204 1
            ->removeClass('max-w-3xl')
205 1
            ->removeClass('lg:ml-0')
206 1
            ->removeClass('lg:mr-auto')
207 1
            ->removeClass('xl:w-3/4')
208 1
            ->removeClass('xl:px-12')
209 1
            ->removeClass('xl:mx-0');
210 1
    }
211
212 1
    protected function updateBottomPadding(HtmlPageCrawler $crawler)
213
    {
214 1
        $crawler->filter('body')
215 1
            ->addClass('pb-8');
216 1
    }
217
218 1
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
219
    {
220 1
        $crawler->filter('h1')
221 1
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
222
223
        $crawler->filter('h2, h3')->each(function (HtmlPageCrawler $node) {
224 1
            $node->prepend(
225 1
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($this->cleanAnchorText($node->text())) . '" class="dashAnchor"></a>'
226
            );
227 1
        });
228 1
    }
229
230 2
    protected function cleanAnchorText($anchorText)
231
    {
232 2
        return trim(preg_replace('/\s+/', ' ', $anchorText));
233
    }
234
}
235