Passed
Push — master ( d2e6c8...f7c9c1 )
by Guillaume
05:10
created

Tiki::removeLeftSidebarButton()   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 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
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 Illuminate\Support\Collection;
7
use Wa72\HtmlPageDom\HtmlPageCrawler;
8
use Illuminate\Support\Facades\Storage;
9
10
class Tiki extends BaseDocset
11
{
12
    public const CODE = 'tiki';
13
    public const NAME = 'Tiki';
14
    public const URL = 'doc.tiki.org';
15
    public const INDEX = 'All-the-Documentation.html';
16
    public const PLAYGROUND = '';
17
    public const ICON_16 = '../../icons/icon.png';
18
    public const ICON_32 = '../../icons/[email protected]';
19
    public const EXTERNAL_DOMAINS = [
20
        'themes.tiki.org',
21
    ];
22
23
24
    public function grab(): bool
25
    {
26
        system(
27
            "wget doc.tiki.org/All-the-Documentation \
28
                --mirror \
29
                -e robots=off \
30
                --header 'Cookie: javascript_enabled_detect=true' \
31
                --reject-regex='/Plugins-|Plugins\.html|fullscreen=|PDF\.js|tikiversion=|comzone=|structure=|wp_files_sort_mode[0-9]=|offset=|\?refresh|\?session_filters|\?sort_mode' \
32
                --accept-regex='/Plugin|/LIST|Tiki_org_family|\.css|\.js|\.jpg|\.png|\.gif|\.svg|\.ico|\.webmanifest' \
33
                --page-requisites \
34
                --adjust-extension \
35
                --convert-links \
36
                --span-hosts \
37
                --domains={$this->externalDomains()} \
38
                --directory-prefix=storage/{$this->downloadedDirectory()}",
39
            $result
40
        );
41
42
        return $result === 0;
43
    }
44
45 1
    public function entries(string $file): Collection
46
    {
47 1
        $crawler = HtmlPageCrawler::create(Storage::get($file));
48
49 1
        $entries = collect();
50 1
        $entries = $entries->merge($this->pluginEntries($crawler, $file));
51
        // $entries = $entries->merge($this->sectionEntries($crawler, $file));
52
53 1
        return $entries;
54
    }
55
56 1
    protected function pluginEntries(HtmlPageCrawler $crawler, string $file)
57
    {
58 1
        $entries = collect();
59
60 1
        if (! in_array(basename($file), ['All-the-Documentation.html', 'site.webmanifest.html'])) {
61 1
            $path = $crawler->filter('link[rel=canonical]')->attr('href');
62
63
            $crawler->filter('#top h1:first-of-type')->each(function (HtmlPageCrawler $node) use ($entries, $file, $path) {
64 1
                $entries->push([
65 1
                        'name' => $node->text(),
66 1
                        'type' => 'Plugin',
67 1
                        'path' => Str::after($file . '#' . Str::slug($path), $this->innerDirectory()),
68
                    ]);
69 1
            });
70
        }
71
72 1
        return $entries;
73
    }
74
75
    protected function sectionEntries(HtmlPageCrawler $crawler, string $file)
76
    {
77
        $entries = collect();
78
79
        $crawler->filter('h2')->each(function (HtmlPageCrawler $node) use ($entries, $file) {
80
            if (! in_array(basename($file), ['All-the-Documentation.html', 'site.webmanifest.html'])) {
81
                $entries->push([
82
                    'name' => trim($node->text()),
83
                    'type' => 'Section',
84
                    'path' => Str::after($file . '#' . Str::slug($node->text()), $this->innerDirectory()),
85
                ]);
86
            }
87
        });
88
89
        return $entries;
90
    }
91
92 1
    public function format(string $html): string
93
    {
94 1
        $crawler = HtmlPageCrawler::create($html);
95
96 1
        $this->removeNavbar($crawler);
97 1
        $this->removeLeftSidebarButton($crawler);
98 1
        $this->removeFullscreenButton($crawler);
99 1
        $this->removePageTopModules($crawler);
100 1
        $this->removeWikiActionsWrapper($crawler);
101 1
        $this->removeBreadcrumb($crawler);
102 1
        $this->removeTopbar($crawler);
103 1
        $this->removeLeftSidebar($crawler);
104 1
        $this->removeRightSidebar($crawler);
105 1
        $this->removePagebar($crawler);
106 1
        $this->removeFooter($crawler);
107 1
        $this->removeUnwantedJavaScript($crawler);
108
109 1
        $this->updateCss($crawler);
110
111 1
        $this->insertDashTableOfContents($crawler);
112
113 1
        return $crawler->saveHTML();
114
    }
115
116 1
    protected function removeNavbar(HtmlPageCrawler $crawler)
117
    {
118 1
        $crawler->filter('nav.navbar')->remove();
119 1
    }
120
121 1
    protected function removeLeftSidebarButton(HtmlPageCrawler $crawler)
122
    {
123 1
        $crawler->filter('#row-middle > div.side-col-toggle-container')->remove();
124 1
    }
125
126 1
    protected function removeFullscreenButton(HtmlPageCrawler $crawler)
127
    {
128 1
        $crawler->filter('#fullscreenbutton')->remove();
129 1
    }
130
131 1
    protected function removePageTopModules(HtmlPageCrawler $crawler)
132
    {
133 1
        $crawler->filter('#pagetop_modules')->remove();
134 1
    }
135
136 1
    protected function removeWikiActionsWrapper(HtmlPageCrawler $crawler)
137
    {
138 1
        $crawler->filter('#col1 > div.wikiactions_wrapper')->remove();
139 1
    }
140
141 1
    protected function removeBreadcrumb(HtmlPageCrawler $crawler)
142
    {
143 1
        $crawler->filter('nav.nav-breadcrumb')->remove();
144 1
    }
145
146 1
    protected function removeTopbar(HtmlPageCrawler $crawler)
147
    {
148 1
        $crawler->filter('#topbar')->remove();
149 1
    }
150
151 1
    protected function removeLeftSidebar(HtmlPageCrawler $crawler)
152
    {
153 1
        $crawler->filter('#col2')->remove();
154 1
    }
155
156 1
    protected function removeRightSidebar(HtmlPageCrawler $crawler)
157
    {
158 1
        $crawler->filter('script[src*="autoToc.js"]')->remove();
159 1
    }
160
161 1
    protected function removePagebar(HtmlPageCrawler $crawler)
162
    {
163 1
        $crawler->filter('#page-bar')->remove();
164 1
    }
165
166 1
    protected function removeFooter(HtmlPageCrawler $crawler)
167
    {
168 1
        $crawler->filter('#footer')->remove();
169 1
    }
170
171 1
    protected function removeUnwantedJavaScript(HtmlPageCrawler $crawler)
172
    {
173 1
        $crawler->filter('script[src*=autosave]')->remove();
174 1
        $crawler->filter('script[src*=gtag]')->remove();
175 1
        $crawler->filter('noscript')->remove();
176 1
        $crawler->filterXPath("//script[text()[contains(.,'piwik.tiki.org')]]")->remove();
177 1
        $crawler->filterXPath("//script[text()[contains(.,'gtag')]]")->remove();
178 1
    }
179
180 1
    protected function updateCSS(HtmlPageCrawler $crawler)
181
    {
182 1
        $this->updateTopPadding($crawler);
183 1
        $this->updateArticlePadding($crawler);
184 1
    }
185
186 1
    protected function updateTopPadding(HtmlPageCrawler $crawler)
187
    {
188 1
        $crawler->filter('body')
189 1
            ->removeClass('navbar-padding')
190 1
            ->addClass('hide_zone_left')
191 1
            ->css('padding-top', '0')
192
        ;
193 1
    }
194
195 1
    protected function updateArticlePadding(HtmlPageCrawler $crawler)
196
    {
197 1
        $crawler->filter('article#top')
198 1
            ->css('padding-top', '44px')
199
        ;
200 1
    }
201
202 1
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
203
    {
204 1
        $crawler->filter('h1')
205 1
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
206
207
        $crawler->filter('h2')->each(static function (HtmlPageCrawler $node) {
208 1
            $node->before(
209 1
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>'
210
            );
211 1
        });
212 1
    }
213
}
214