Passed
Pull Request — master (#1)
by Guillaume
17:35 queued 15:15
created

removeMenuAndSharingButtons()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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\HtmlPageCrawler;
10
11
class ChartjsPluginDatalabels extends BaseDocset
12
{
13
    public const CODE = 'chartjs-plugin-datalabels';
14
    public const NAME = 'chartjs-plugin-datalabels';
15
    public const URL = 'chartjs-plugin-datalabels.netlify.app';
16
    public const INDEX = 'guide/index.html';
17
    public const PLAYGROUND = 'https://codepen.io/lucusc/pen/pgQRdd';
18
    public const ICON_16 = '../../icons/icon.png';
19
    public const ICON_32 = '../../icons/[email protected]';
20
    public const EXTERNAL_DOMAINS = [
21
        'use.typekit.net',
22
    ];
23
24
25
    public function grab(): bool
26
    {
27
        $toIgnore = implode('|', [
0 ignored issues
show
Unused Code introduced by
The assignment to $toIgnore is dead and can be removed.
Loading history...
28
            '/cdn-cgi',
29
            '/docs/2.9.3',
30
            '/docs/master',
31
            '/samples/master'
32
        ]);
33
34
        $toGet = implode('|', [
0 ignored issues
show
Unused Code introduced by
The assignment to $toGet is dead and can be removed.
Loading history...
35
            '\.css',
36
            '\.ico',
37
            '\.js',
38
            '\.svg',
39
            '/docs',
40
            '/samples'
41
        ]);
42
43
        system(
44
            "echo; wget chartjs-plugin-datalabels.netlify.app \
45
                --mirror \
46
                --trust-server-names \
47
                --ignore-case \
48
                --page-requisites \
49
                --adjust-extension \
50
                --convert-links \
51
                --span-hosts \
52
                --domains={$this->externalDomains()} \
53
                --directory-prefix=storage/{$this->downloadedDirectory()} \
54
                -e robots=off \
55
                --quiet \
56
                --show-progress",
57
            $result
58
        );
59
60
        return $result === 0;
61
    }
62
63 8
    public function entries(string $file): Collection
64
    {
65 8
        $crawler = HtmlPageCrawler::create(Storage::get($file));
66
67 8
        $entries = collect();
68
69 8
        $entries = $entries->merge($this->guideEntries($crawler, $file));
70 8
        $entries = $entries->merge($this->sectionEntries($crawler, $file));
71
72 8
        return $entries;
73
    }
74
75 8
    protected function guideEntries(HtmlPageCrawler $crawler, string $file)
76
    {
77 8
        $entries = collect();
78
79 8
        if (Str::contains($file, "{$this->url()}/docs/latest/index.html")) {
80
            $crawler
81
                ->filter('.summary > li.chapter:not(:first-child) a')
82
                ->each(function (HtmlPageCrawler $node) use ($entries) {
83
                    $entries->push([
84
                        'name' => $node->text(),
85
                        'type' => 'Guide',
86
                        'path' => $this->url() . '/docs/latest/' . $this->cleanUrl($node->attr('href')),
87
                    ]);
88
                });
89
        }
90
91 8
        return $entries;
92
    }
93
94 8
    protected function sectionEntries(HtmlPageCrawler $crawler, string $file)
95
    {
96 8
        $entries = collect();
97
98 8
        $crawler->filter('h2')->each(function (HtmlPageCrawler $node) use ($entries, $file) {
99 8
            $entries->push([
100 8
                'name' => $node->text(),
101 8
                'type' => 'Section',
102 8
                'path' => Str::after($file . '#' . Str::slug($node->text()), $this->innerDirectory())
103
            ]);
104 8
        });
105
106 8
        return $entries;
107
    }
108
109
    /**
110
     * Some links are not converted correctly by wget once
111
     * the download is finished. no idea why, but this cleans
112
     * the few that fail.
113
     */
114
    protected function cleanUrl($url)
115
    {
116
        return Str::after($url, 'https://www.chartjs.org/docs/latest/');
117
    }
118
119 4
    public function format(string $file): string
120
    {
121 4
        $crawler = HtmlPageCrawler::create(Storage::get($file));
122
123 4
        $this->removeLeftSidebar($crawler);
124 4
        $this->removeMenuAndSharingButtons($crawler);
125 4
        $this->removeNavigation($crawler);
126 4
        $this->makeContentFullWidth($crawler);
127 4
        $this->removeSearchResults($crawler);
128
129 4
        $this->removeUnwantedCSS($crawler);
130 4
        $this->removeUnwantedJavaScript($crawler);
131
132 4
        $this->insertDashTableOfContents($crawler);
133
134 4
        return $crawler->saveHTML();
135
    }
136
137 4
    protected function removeLeftSidebar(HtmlPageCrawler $crawler)
138
    {
139 4
        $crawler->filter('.book-summary')->remove();
140 4
    }
141
142 4
    protected function removeMenuAndSharingButtons(HtmlPageCrawler $crawler)
143
    {
144 4
        $crawler->filter('body')->after(
145 4
            "<script>$(document).ready(function () { $('.pull-right.js-toolbar-action, .pull-left.btn').hide(); });</script>"
146
        );
147 4
    }
148
149 4
    protected function removeNavigation(HtmlPageCrawler $crawler)
150
    {
151 4
        $crawler->filter('.navigation')->remove();
152 4
    }
153
154 4
    protected function makeContentFullWidth(HtmlPageCrawler $crawler)
155
    {
156 4
        $crawler->filter('.book-body')->setStyle('left', '0px !important');
157 4
    }
158
159 4
    protected function removeSearchResults(HtmlPageCrawler $crawler)
160
    {
161 4
        $crawler->filter('.search-results')->remove();
162 4
    }
163
164 4
    protected function removeUnwantedCSS(HtmlPageCrawler $crawler)
165
    {
166 4
        $crawler->filter('link[href*="search.css"]')->remove();
167 4
    }
168
169 4
    protected function removeUnwantedJavaScript(HtmlPageCrawler $crawler)
170
    {
171 4
        $crawler->filter('script[src*="search.js"]')->remove();
172 4
    }
173
174 4
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
175
    {
176 4
        $crawler->filter('.page-inner')
177 4
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
178
179 4
        $crawler->filter('h2, h3')->each(static function (HtmlPageCrawler $node) {
180 4
            $node->before(
181 4
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>'
182
            );
183 4
        });
184 4
    }
185
}
186