LaravelZero   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 72
dl 0
loc 155
ccs 83
cts 83
cp 1
rs 10
c 0
b 0
f 0
wmc 19

15 Methods

Rating   Name   Duplication   Size   Complexity  
A is404OrIndex() 0 4 2
A insertDashTableOfContents() 0 8 1
A removeHeader() 0 3 1
A isRealPage() 0 3 1
A updateBottomPadding() 0 4 1
A entries() 0 9 1
A removeLeftSidebar() 0 3 1
A sectionEntries() 0 15 2
A removeUnwantedCSS() 0 3 1
A removeFooter() 0 3 1
A updateContainerWidth() 0 11 1
A guideEntries() 0 19 3
A removeUnwantedJavaScript() 0 6 1
A updateTopPadding() 0 4 1
A format() 0 15 1
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\HtmlPage;
10
use Wa72\HtmlPageDom\HtmlPageCrawler;
11
12
class LaravelZero extends BaseDocset
13
{
14
    public const CODE = 'laravel-zero';
15
    public const NAME = 'Laravel Zero';
16
    public const URL = 'laravel-zero.com';
17
    public const INDEX = 'docs/introduction/index.html';
18
    public const PLAYGROUND = '';
19
    public const ICON_16 = '../../icons/icon.png';
20
    public const ICON_32 = '../../icons/[email protected]';
21
    public const EXTERNAL_DOMAINS = [
22
        'raw.githubusercontent.com',
23
        'googleapis.com',
24
    ];
25
26 12
    public function entries(string $file): Collection
27
    {
28 12
        $crawler = HtmlPageCrawler::create(Storage::get($file));
29
30 12
        $entries = collect();
31 12
        $entries = $entries->merge($this->guideEntries($crawler, $file));
32 12
        $entries = $entries->merge($this->sectionEntries($crawler, $file));
33
34 12
        return $entries;
35
    }
36
37 12
    protected function guideEntries(HtmlPageCrawler $crawler, string $file)
38
    {
39 12
        $pageTitle = (new HtmlPage(Storage::get($file)))->getTitle();
40
41 12
        $entries = collect();
42
43 12
        if ($pageTitle === 'Laravel Zero | Introduction') {
44
            $crawler->filter('.lvl0, .lvl1')->each(function (HtmlPageCrawler $node) use ($entries) {
45 6
                if ($this->isRealPage(trim($node->text()))) {
46 6
                    $entries->push([
47 6
                        'name' => trim($node->text()),
48 6
                        'type' => 'Guide',
49 6
                        'path' => $this->url() . '/docs/introduction/' . $node->attr('href')
50
                    ]);
51
                }
52 6
            });
53
        }
54
55 12
        return $entries;
56
    }
57
58 6
    protected function isRealPage($name)
59
    {
60 6
        return ! in_array($name, ['Usage', 'Add-ons']);
61
    }
62
63 12
    protected function sectionEntries(HtmlPageCrawler $crawler, string $file)
64
    {
65 12
        $entries = collect();
66
67 12
        if (! $this->is404OrIndex($file)) {
68
            $crawler->filter('h2, h3, h4')->each(function (HtmlPageCrawler $node) use ($entries, $file) {
69 12
                $entries->push([
70 12
                    'name' => trim($node->text()),
71 12
                    'type' => 'Section',
72 12
                    'path' => Str::after($file . '#' . Str::slug($node->text()), $this->innerDirectory()),
73
                ]);
74 12
            });
75
        }
76
77 12
        return $entries;
78
    }
79
80 12
    protected function is404OrIndex($file)
81
    {
82 12
        return Str::contains($file, "{$this->url()}/index.html")
83 12
            || Str::contains($file, "{$this->url()}/404/index.html");
84
    }
85
86 12
    public function format(string $file): string
87
    {
88 12
        $crawler = HtmlPageCrawler::create(Storage::get($file));
89
90 12
        $this->removeHeader($crawler);
91 12
        $this->removeLeftSidebar($crawler);
92 12
        $this->removeFooter($crawler);
93 12
        $this->updateTopPadding($crawler);
94 12
        $this->updateContainerWidth($crawler);
95 12
        $this->updateBottomPadding($crawler);
96 12
        $this->removeUnwantedCSS($crawler);
97 12
        $this->removeUnwantedJavaScript($crawler);
98 12
        $this->insertDashTableOfContents($crawler);
99
100 12
        return $crawler->saveHTML();
101
    }
102
103 12
    protected function removeHeader(HtmlPageCrawler $crawler)
104
    {
105 12
        $crawler->filter('body > header')->remove();
106 12
    }
107
108 12
    protected function removeLeftSidebar(HtmlPageCrawler $crawler)
109
    {
110 12
        $crawler->filter('#js-nav-menu')->remove();
111 12
    }
112
113 12
    protected function removeFooter(HtmlPageCrawler $crawler)
114
    {
115 12
        $crawler->filter('body > footer')->remove();
116 12
    }
117
118 12
    protected function updateTopPadding(HtmlPageCrawler $crawler)
119
    {
120 12
        $crawler->filter('h1')
121 12
            ->css('margin-top', '1rem')
122
        ;
123 12
    }
124
125 12
    protected function updateContainerWidth(HtmlPageCrawler $crawler)
126
    {
127 12
        $crawler->filter('section.container > div > div')
128 12
            ->removeClass('lg:w-3/5')
129 12
            ->removeClass('lg:pl-4')
130
        ;
131
132 12
        $crawler->filter('section.container')
133 12
            ->removeClass('max-w-4xl')
134 12
            ->removeClass('md:px-8')
135 12
            ->removeClass('container')
136
        ;
137 12
    }
138
139 12
    protected function updateBottomPadding(HtmlPageCrawler $crawler)
140
    {
141 12
        $crawler->filter('section > div > div')
142 12
            ->removeClass('pb-16')
143
        ;
144 12
    }
145
146 12
    protected function removeUnwantedCSS(HtmlPageCrawler $crawler)
147
    {
148 12
        $crawler->filter('link[href*="docsearch.min.css"]')->remove();
149 12
    }
150
151 12
    protected function removeUnwantedJavaScript(HtmlPageCrawler $crawler)
152
    {
153 12
        $crawler->filter('script[src*=docsearch]')->remove();
154 12
        $crawler->filter('script[src*=gtag]')->remove();
155 12
        $crawler->filterXPath("//script[text()[contains(.,'docsearch')]]")->remove();
156 12
        $crawler->filterXPath("//script[text()[contains(.,'gtag')]]")->remove();
157 12
    }
158
159 12
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
160
    {
161 12
        $crawler->filter('h1')
162 12
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
163
164
        $crawler->filter('h2, h3, h4')->each(static function (HtmlPageCrawler $node) {
165 12
            $node->before(
166 12
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>'
167
            );
168 12
        });
169 12
    }
170
}
171