Passed
Push — master ( ffcb39...bb709a )
by Guillaume
03:57
created

Jigsaw::removeJavaScript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
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 Illuminate\Support\Collection;
7
use Wa72\HtmlPageDom\HtmlPageCrawler;
8
use Illuminate\Support\Facades\Storage;
9
10
class Jigsaw extends BaseDocset
11
{
12
    public const CODE = 'jigsaw';
13
    public const NAME = 'Jigsaw';
14
    public const URL = 'jigsaw.tighten.co';
15
    public const INDEX = 'installation.html';
16
    public const PLAYGROUND = '';
17
    public const ICON_16 = '../icon.png';
18
    public const ICON_32 = '../[email protected]';
19
    public const EXTERNAL_DOMAINS = [];
20
21
22 1
    public function entries(string $file): Collection
23
    {
24 1
        $crawler = HtmlPageCrawler::create(Storage::get($file));
25
26 1
        $entries = collect();
27 1
        $entries = $entries->merge($this->guideEntries($crawler, $file));
28 1
        $entries = $entries->merge($this->sectionEntries($crawler, $file));
29
30 1
        return $entries;
31
    }
32
33 1
    protected function guideEntries(HtmlPageCrawler $crawler, string $file)
34
    {
35 1
        $entries = collect();
36
37
        $crawler->filter('h2')->each(static function (HtmlPageCrawler $node) use ($entries, $file) {
38 1
            $fileBasename = basename($file);
39
40 1
            if ($fileBasename !== 'index.html') {
41 1
                $entries->push([
42 1
                    'name' => trim($node->text()),
43 1
                    'type' => 'Guide',
44 1
                    'path' => $fileBasename
45
                ]);
46
            }
47 1
        });
48
49 1
        return $entries;
50
    }
51
52 1
    protected function sectionEntries(HtmlPageCrawler $crawler, string $file)
53
    {
54 1
        $entries = collect();
55
56 1
        $parent = $crawler->filter('h4')->first()->text() ?: $crawler->filter('h2')->first()->text();
57
58
        $crawler->filter('h3')->each(static function (HtmlPageCrawler $node) use ($entries, $file, $parent) {
59 1
            $fileBasename = basename($file);
60
61 1
            if ($fileBasename !== 'index.html') {
62 1
                $entries->push([
63 1
                    'name' => trim($node->text() . ' - ' . $parent),
64 1
                    'type' => 'Section',
65 1
                    'path' => $fileBasename . '#' . Str::slug($node->text())
66
                ]);
67
            }
68 1
        });
69
70 1
        return $entries;
71
    }
72
73 1
    public function format(string $html): string
74
    {
75 1
        $crawler = HtmlPageCrawler::create($html);
76
77 1
        $this->removeHeader($crawler);
78 1
        $this->removeFooter($crawler);
79 1
        $this->updateTopPadding($crawler);
80 1
        $this->updateContainer($crawler);
81 1
        $this->updateTextSize($crawler);
82 1
        $this->updateH4Padding($crawler);
83 1
        $this->removeJavaScript($crawler);
84 1
        $this->insertDashTableOfContents($crawler);
85
86 1
        return $crawler->saveHTML();
87
    }
88
89 1
    protected function removeHeader(HtmlPageCrawler $crawler)
90
    {
91 1
        $crawler->filter('#vue-app > div:first-child')->remove();
92 1
        $crawler->filter('#vue-app > header')->remove();
93 1
    }
94
95 1
    protected function removeFooter(HtmlPageCrawler $crawler)
96
    {
97 1
        $crawler->filter('footer')->remove();
98 1
    }
99
100 1
    protected function updateTopPadding(HtmlPageCrawler $crawler)
101
    {
102 1
        $crawler->filter('#vue-app > div > div > div')
103 1
            ->removeClass('pt-4')
104 1
            ->css('margin-top', '-1.5rem')
105
        ;
106 1
    }
107
108 1
    protected function updateContainer(HtmlPageCrawler $crawler)
109
    {
110 1
        $crawler->filter('#vue-app > div')
111 1
            ->removeClass('pt-16')
112 1
            ->removeClass('md:pt-24')
113 1
            ->removeClass('lg:pt-32')
114 1
            ->removeClass('md:px-6')
115
        ;
116
117 1
        $crawler->filter('#vue-app > div > div')
118 1
            ->removeClass('max-w-3xl')
119
        ;
120
121 1
        $crawler->filter('div.markdown')
122 1
            ->removeClass('lg:max-w-md')
123 1
            ->removeClass('xl:max-w-lg')
124 1
            ->removeClass('md:mb-6')
125 1
            ->removeClass('lg:mb-10')
126 1
            ->removeClass('xl:px-10')
127 1
            ->removeClass('sm:shadow')
128 1
            ->removeClass('md:rounded-lg')
129
        ;
130 1
    }
131
132 1
    protected function updateTextSize(HtmlPageCrawler $crawler)
133
    {
134 1
        $crawler->filter('h2')
135 1
            ->addClass('text-3xl')
136
        ;
137
138 1
        $crawler->filter('h3')
139 1
            ->css('font-size', '1.5rem')
140
        ;
141 1
    }
142
143 1
    protected function updateH4Padding(HtmlPageCrawler $crawler)
144
    {
145 1
        $crawler->filter('h4')
146 1
            ->css('margin-top', '2.5rem')
147
        ;
148 1
    }
149
150 1
    protected function removeJavaScript(HtmlPageCrawler $crawler)
151
    {
152 1
        $crawler->filter('script')->remove();
153 1
    }
154
155 1
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
156
    {
157 1
        $crawler->filter('h2')
158 1
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
159
160
        $crawler->filter('h3')->each(static function (HtmlPageCrawler $node) {
161 1
            $node->before(
162 1
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>'
163
            );
164 1
        });
165 1
    }
166
}
167