Passed
Push — master ( 29091b...2dc1ac )
by Guillaume
06:12
created

Tiki::styleEntries()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
c 1
b 0
f 1
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.9666
cc 2
nc 2
nop 2
crap 2
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 = 'https://tiki.org/Demo';
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
        $toIgnore = implode('|', [
27
            '\?refresh',
28
            '\?session_filters',
29
            '\?sort_mode',
30
            '/Plugins-',
31
            'comzone=',
32
            'cookietab=',
33
            'fullscreen=',
34
            'offset=',
35
            'todate=',
36
            'viewmode=',
37
            'PDF\.js',
38
            'Plugins$',
39
            'structure=',
40
            'tikiversion=',
41
            'wp_files_sort_mode[0-9]=',
42
        ]);
43
44
        $toGet = implode('|', [
45
            '\.css',
46
            '\.gif',
47
            '\.ico',
48
            '\.jpg',
49
            '\.js',
50
            '\.png',
51
            '\.svg',
52
            '\.webmanifest',
53
            '/display',
54
            '/LIST',
55
            '/Module-',
56
            '/Plugin[^-]',
57
            '-Field$',
58
            'Tiki_org_family',
59
            '[^:=]Wiki-Syntax'
60
        ]);
61
62
        system(
63
            "echo; wget doc.tiki.org/All-the-Documentation \
64
                --mirror \
65
                --trust-server-names \
66
                --header 'Cookie: javascript_enabled_detect=true' \
67
                --reject-regex='{$toIgnore}' \
68
                --accept-regex='{$toGet}' \
69
                --ignore-case \
70
                --page-requisites \
71
                --adjust-extension \
72
                --convert-links \
73
                --span-hosts \
74
                --domains={$this->externalDomains()} \
75
                --directory-prefix=storage/{$this->downloadedDirectory()} \
76
                -e robots=off \
77
                --quiet \
78
                --show-progress",
79
            $result
80
        );
81
82
        return $result === 0;
83
    }
84
85 2
    public function entries(string $file): Collection
86
    {
87 2
        $crawler = HtmlPageCrawler::create(Storage::get($file));
88
89 2
        $entries = collect();
90 2
        $entries = $entries->merge($this->pluginEntries($crawler, $file));
91 2
        $entries = $entries->merge($this->moduleEntries($crawler, $file));
92 2
        $entries = $entries->merge($this->fieldEntries($crawler, $file));
93 2
        $entries = $entries->merge($this->styleEntries($crawler, $file));
94
95 2
        return $entries;
96
    }
97
98 2
    protected function pluginEntries(HtmlPageCrawler $crawler, string $file)
99
    {
100 2
        $entries = collect();
101
102 2
        if (preg_match('/Plugin/i', $file)) {
103 2
            $path = $crawler->filter('link[rel=canonical]')->attr('href');
104
105
            $crawler->filter('#page-data > h1:first-of-type')->each(function (HtmlPageCrawler $node) use ($entries, $file, $path) {
106 2
                $entries->push([
107 2
                        'name' => $node->text(),
108 2
                        'type' => 'Plugin',
109 2
                        'path' => Str::after($file . '#' . Str::slug($path), $this->innerDirectory()),
110
                    ]);
111 2
            });
112
        }
113
114 2
        return $entries;
115
    }
116
117 2
    protected function moduleEntries(HtmlPageCrawler $crawler, string $file)
118
    {
119 2
        $entries = collect();
120
121 2
        if (preg_match('/Module/i', $file)) {
122 1
            $path = $crawler->filter('link[rel=canonical]')->attr('href');
123
124
            $crawler->filter('#page-data > h1:first-of-type')->each(function (HtmlPageCrawler $node) use ($entries, $file, $path) {
125 1
                $entries->push([
126 1
                        'name' => $node->text(),
127 1
                        'type' => 'Module',
128 1
                        'path' => Str::after($file . '#' . Str::slug($path), $this->innerDirectory()),
129
                    ]);
130 1
            });
131
        }
132
133 2
        return $entries;
134
    }
135
136 2
    protected function fieldEntries(HtmlPageCrawler $crawler, string $file)
137
    {
138 2
        $entries = collect();
139
140 2
        if (preg_match('/Tracker-Field/i', $file)) {
141 1
            $path = $crawler->filter('link[rel=canonical]')->attr('href');
142
143
            $crawler->filter('#page-data > h1:first-of-type')->each(function (HtmlPageCrawler $node) use ($entries, $file, $path) {
144 1
                $entries->push([
145 1
                        'name' => $node->text(),
146 1
                        'type' => 'Field',
147 1
                        'path' => Str::after($file . '#' . Str::slug($path), $this->innerDirectory()),
148
                    ]);
149 1
            });
150
        }
151
152 2
        return $entries;
153
    }
154
155 2
    protected function styleEntries(HtmlPageCrawler $crawler, string $file)
156
    {
157 2
        $entries = collect();
158
159 2
        if (preg_match('/Wiki-Syntax/i', $file)) {
160 1
            $path = $crawler->filter('link[rel=canonical]')->attr('href');
161
162
            $crawler->filter('#page-data > h1:first-of-type')->each(function (HtmlPageCrawler $node) use ($entries, $file, $path) {
163 1
                $entries->push([
164 1
                        'name' => $node->text(),
165 1
                        'type' => 'Style',
166 1
                        'path' => Str::after($file . '#' . Str::slug($path), $this->innerDirectory()),
167
                    ]);
168 1
            });
169
        }
170
171 2
        return $entries;
172
    }
173
174 2
    public function format(string $html): string
175
    {
176 2
        $crawler = HtmlPageCrawler::create($html);
177
178 2
        $this->removeNavbar($crawler);
179 2
        $this->removeLeftSidebarButton($crawler);
180 2
        $this->removeFullscreenButton($crawler);
181 2
        $this->removePageTopModules($crawler);
182 2
        $this->removeWikiActionsWrapper($crawler);
183 2
        $this->removeBreadcrumb($crawler);
184 2
        $this->removeTopbar($crawler);
185 2
        $this->removeLeftSidebar($crawler);
186 2
        $this->removeRightSidebar($crawler);
187 2
        $this->removePagebar($crawler);
188 2
        $this->removeFooter($crawler);
189 2
        $this->removeUnwantedJavaScript($crawler);
190
191 2
        $this->updateCss($crawler);
192
193 2
        $this->insertOnlineRedirection($crawler);
194 2
        $this->insertDashTableOfContents($crawler);
195
196 2
        return $crawler->saveHTML();
197
    }
198
199 2
    protected function removeNavbar(HtmlPageCrawler $crawler)
200
    {
201 2
        $crawler->filter('nav.navbar')->remove();
202 2
    }
203
204 2
    protected function removeLeftSidebarButton(HtmlPageCrawler $crawler)
205
    {
206 2
        $crawler->filter('#row-middle > div.side-col-toggle-container')->remove();
207 2
    }
208
209 2
    protected function removeFullscreenButton(HtmlPageCrawler $crawler)
210
    {
211 2
        $crawler->filter('#fullscreenbutton')->remove();
212 2
    }
213
214 2
    protected function removePageTopModules(HtmlPageCrawler $crawler)
215
    {
216 2
        $crawler->filter('#pagetop_modules')->remove();
217 2
    }
218
219 2
    protected function removeWikiActionsWrapper(HtmlPageCrawler $crawler)
220
    {
221 2
        $crawler->filter('#col1 > div.wikiactions_wrapper')->remove();
222 2
    }
223
224 2
    protected function removeBreadcrumb(HtmlPageCrawler $crawler)
225
    {
226 2
        $crawler->filter('nav.nav-breadcrumb')->remove();
227 2
    }
228
229 2
    protected function removeTopbar(HtmlPageCrawler $crawler)
230
    {
231 2
        $crawler->filter('#topbar')->remove();
232 2
    }
233
234 2
    protected function removeLeftSidebar(HtmlPageCrawler $crawler)
235
    {
236 2
        $crawler->filter('#col2')->remove();
237 2
    }
238
239 2
    protected function removeRightSidebar(HtmlPageCrawler $crawler)
240
    {
241 2
        $crawler->filter('script[src*="autoToc.js"]')->remove();
242 2
    }
243
244 2
    protected function removePagebar(HtmlPageCrawler $crawler)
245
    {
246 2
        $crawler->filter('#page-bar')->remove();
247 2
    }
248
249 2
    protected function removeFooter(HtmlPageCrawler $crawler)
250
    {
251 2
        $crawler->filter('#footer')->remove();
252 2
    }
253
254 2
    protected function removeUnwantedJavaScript(HtmlPageCrawler $crawler)
255
    {
256 2
        $crawler->filter('script[src*=autosave]')->remove();
257 2
        $crawler->filter('script[src*=gtag]')->remove();
258 2
        $crawler->filter('noscript')->remove();
259 2
        $crawler->filterXPath("//script[text()[contains(.,'piwik.tiki.org')]]")->remove();
260 2
        $crawler->filterXPath("//script[text()[contains(.,'gtag')]]")->remove();
261 2
    }
262
263 2
    protected function updateCSS(HtmlPageCrawler $crawler)
264
    {
265 2
        $this->updateTopPadding($crawler);
266 2
        $this->updateArticlePadding($crawler);
267 2
    }
268
269 2
    protected function updateTopPadding(HtmlPageCrawler $crawler)
270
    {
271 2
        $crawler->filter('body')
272 2
            ->removeClass('navbar-padding')
273 2
            ->addClass('hide_zone_left')
274 2
            ->css('padding-top', '0')
275
        ;
276 2
    }
277
278 2
    protected function updateArticlePadding(HtmlPageCrawler $crawler)
279
    {
280 2
        $crawler->filter('article#top')
281 2
            ->css('padding-top', '44px')
282
        ;
283 2
    }
284
285 2
    protected function insertOnlineRedirection(HtmlPageCrawler $crawler)
286
    {
287 2
        $onlineUrl = '';
288 2
        $meta = $crawler->filter('meta[property="og:url"]');
289
290 2
        if ($meta->getDOMDocument()) {
291 2
            $onlineUrl = $meta->attr('content');
292
        }
293
294 2
        $crawler->filter('html')->prepend("<!-- Online page at $onlineUrl -->");
295 2
    }
296
297 2
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
298
    {
299 2
        $crawler->filter('#page-data > h1:first-of-type')
300 2
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
301
302
        $crawler->filter('h2')->each(static function (HtmlPageCrawler $node) {
303 2
            $node->before(
304 2
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>'
305
            );
306 2
        });
307 2
    }
308
}
309