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 Chartjs extends BaseDocset |
12
|
|
|
{ |
13
|
|
|
public const CODE = 'chartjs'; |
14
|
|
|
public const NAME = 'Chart.js'; |
15
|
|
|
public const URL = 'www.chartjs.org'; |
16
|
|
|
public const INDEX = 'docs/latest/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('|', [ |
28
|
|
|
'/cdn-cgi', |
29
|
|
|
'/docs/2.9.3', |
30
|
|
|
'/docs/master', |
31
|
|
|
'/samples/master' |
32
|
|
|
]); |
33
|
|
|
|
34
|
|
|
$toGet = implode('|', [ |
35
|
|
|
'\.css', |
36
|
|
|
'\.ico', |
37
|
|
|
'\.js', |
38
|
|
|
'\.svg', |
39
|
|
|
'/docs', |
40
|
|
|
'/samples' |
41
|
|
|
]); |
42
|
|
|
|
43
|
|
|
system( |
44
|
|
|
"echo; wget www.chartjs.org \ |
45
|
|
|
--mirror \ |
46
|
|
|
--trust-server-names \ |
47
|
|
|
--reject-regex='{$toIgnore}' \ |
48
|
|
|
--accept-regex='{$toGet}' \ |
49
|
|
|
--ignore-case \ |
50
|
|
|
--page-requisites \ |
51
|
|
|
--adjust-extension \ |
52
|
|
|
--convert-links \ |
53
|
|
|
--span-hosts \ |
54
|
|
|
--domains={$this->externalDomains()} \ |
55
|
|
|
--directory-prefix=storage/{$this->downloadedDirectory()} \ |
56
|
|
|
-e robots=off \ |
57
|
|
|
--quiet \ |
58
|
|
|
--show-progress", |
59
|
|
|
$result |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
return $result === 0; |
63
|
|
|
} |
64
|
|
|
|
65
|
12 |
|
public function entries(string $file): Collection |
66
|
|
|
{ |
67
|
12 |
|
$crawler = HtmlPageCrawler::create(Storage::get($file)); |
68
|
|
|
|
69
|
12 |
|
$entries = collect(); |
70
|
|
|
|
71
|
12 |
|
$entries = $entries->merge($this->guideEntries($crawler, $file)); |
72
|
12 |
|
$entries = $entries->merge($this->sectionEntries($crawler, $file)); |
73
|
|
|
|
74
|
12 |
|
return $entries; |
75
|
|
|
} |
76
|
|
|
|
77
|
12 |
|
protected function guideEntries(HtmlPageCrawler $crawler, string $file) |
78
|
|
|
{ |
79
|
12 |
|
$entries = collect(); |
80
|
|
|
|
81
|
12 |
|
if (Str::contains($file, "{$this->url()}/docs/latest/index.html")) { |
82
|
|
|
$crawler->filter('.summary a')->each(function (HtmlPageCrawler $node) use ($entries) { |
83
|
12 |
|
$entries->push([ |
84
|
12 |
|
'name' => $node->text(), |
85
|
12 |
|
'type' => 'Guide', |
86
|
12 |
|
'path' => $this->url() . '/docs/latest/' . $this->cleanUrl($node->attr('href')), |
87
|
|
|
]); |
88
|
12 |
|
}); |
89
|
|
|
} |
90
|
|
|
|
91
|
12 |
|
return $entries; |
92
|
|
|
} |
93
|
|
|
|
94
|
12 |
|
protected function sectionEntries(HtmlPageCrawler $crawler, string $file) |
95
|
|
|
{ |
96
|
12 |
|
$entries = collect(); |
97
|
|
|
|
98
|
|
|
$crawler->filter('h2')->each(function (HtmlPageCrawler $node) use ($entries, $file) { |
99
|
12 |
|
$entries->push([ |
100
|
12 |
|
'name' => $node->text(), |
101
|
12 |
|
'type' => 'Section', |
102
|
12 |
|
'path' => Str::after($file . '#' . Str::slug($node->text()), $this->innerDirectory()) |
103
|
|
|
]); |
104
|
12 |
|
}); |
105
|
|
|
|
106
|
12 |
|
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
|
12 |
|
protected function cleanUrl($url) |
115
|
|
|
{ |
116
|
12 |
|
return Str::after($url, 'https://www.chartjs.org/docs/latest/'); |
117
|
|
|
} |
118
|
|
|
|
119
|
12 |
|
public function format(string $file): string |
120
|
|
|
{ |
121
|
12 |
|
$crawler = HtmlPageCrawler::create(Storage::get($file)); |
122
|
|
|
|
123
|
12 |
|
$this->removeLeftSidebar($crawler); |
124
|
12 |
|
$this->removeMenuAndSharingButtons($crawler); |
125
|
12 |
|
$this->removeNavigation($crawler); |
126
|
12 |
|
$this->makeContentFullWidth($crawler); |
127
|
|
|
|
128
|
12 |
|
$this->removeUnwantedCSS($crawler); |
129
|
12 |
|
$this->removeUnwantedJavaScript($crawler); |
130
|
|
|
|
131
|
12 |
|
$this->insertDashTableOfContents($crawler); |
132
|
|
|
|
133
|
12 |
|
return $crawler->saveHTML(); |
134
|
|
|
} |
135
|
|
|
|
136
|
12 |
|
protected function removeLeftSidebar(HtmlPageCrawler $crawler) |
137
|
|
|
{ |
138
|
12 |
|
$crawler->filter('.book-summary')->remove(); |
139
|
12 |
|
} |
140
|
|
|
|
141
|
12 |
|
protected function removeMenuAndSharingButtons(HtmlPageCrawler $crawler) |
142
|
|
|
{ |
143
|
12 |
|
$crawler->filter('body')->after( |
144
|
12 |
|
"<script>$(document).ready(function () { $('.pull-right.js-toolbar-action, .pull-left.btn').hide(); });</script>" |
145
|
|
|
); |
146
|
12 |
|
} |
147
|
|
|
|
148
|
12 |
|
protected function removeNavigation(HtmlPageCrawler $crawler) |
149
|
|
|
{ |
150
|
12 |
|
$crawler->filter('.navigation')->remove(); |
151
|
12 |
|
} |
152
|
|
|
|
153
|
12 |
|
protected function makeContentFullWidth(HtmlPageCrawler $crawler) |
154
|
|
|
{ |
155
|
12 |
|
$crawler->filter('.book-body')->setStyle('left', '0px !important'); |
156
|
12 |
|
} |
157
|
|
|
|
158
|
12 |
|
protected function removeUnwantedCSS(HtmlPageCrawler $crawler) |
159
|
|
|
{ |
160
|
12 |
|
$crawler->filter('link[href*="search.css"]')->remove(); |
161
|
12 |
|
} |
162
|
|
|
|
163
|
12 |
|
protected function removeUnwantedJavaScript(HtmlPageCrawler $crawler) |
164
|
|
|
{ |
165
|
12 |
|
$crawler->filter('script[src*=analytics]')->remove(); |
166
|
12 |
|
$crawler->filter('script[src*=search\.js]')->remove(); |
167
|
12 |
|
} |
168
|
|
|
|
169
|
12 |
|
protected function insertDashTableOfContents(HtmlPageCrawler $crawler) |
170
|
|
|
{ |
171
|
12 |
|
$crawler->filter('.page-inner') |
172
|
12 |
|
->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>'); |
173
|
|
|
|
174
|
|
|
$crawler->filter('h2, h3')->each(static function (HtmlPageCrawler $node) { |
175
|
12 |
|
$node->before( |
176
|
12 |
|
'<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>' |
177
|
|
|
); |
178
|
12 |
|
}); |
179
|
12 |
|
} |
180
|
|
|
} |
181
|
|
|
|