|
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://chartjs-plugin-datalabels.netlify.app/samples/'; |
|
18
|
|
|
public const ICON_16 = '../../icons/icon.png'; |
|
19
|
|
|
public const ICON_32 = '../../icons/[email protected]'; |
|
20
|
|
|
public const EXTERNAL_DOMAINS = [ |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
8 |
|
public function entries(string $file): Collection |
|
25
|
|
|
{ |
|
26
|
8 |
|
$crawler = HtmlPageCrawler::create(Storage::get($file)); |
|
27
|
|
|
|
|
28
|
8 |
|
$entries = collect(); |
|
29
|
|
|
|
|
30
|
8 |
|
$entries = $entries->merge($this->guideEntries($crawler, $file)); |
|
31
|
8 |
|
$entries = $entries->merge($this->sectionEntries($crawler, $file)); |
|
32
|
|
|
|
|
33
|
8 |
|
return $entries; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
8 |
|
protected function guideEntries(HtmlPageCrawler $crawler, string $file) |
|
37
|
|
|
{ |
|
38
|
8 |
|
$entries = collect(); |
|
39
|
|
|
|
|
40
|
8 |
|
if (Str::contains($file, "{$this->url()}/guide/index.html")) { |
|
41
|
|
|
$crawler |
|
42
|
8 |
|
->filter('.sidebar-links > li > a') |
|
43
|
8 |
|
->each(function (HtmlPageCrawler $node) use ($entries) { |
|
44
|
8 |
|
$entries->push([ |
|
45
|
8 |
|
'name' => $node->text(), |
|
46
|
8 |
|
'type' => 'Guide', |
|
47
|
8 |
|
'path' => $this->url() . '/guide/' . $node->attr('href'), |
|
48
|
|
|
]); |
|
49
|
8 |
|
}); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
8 |
|
return $entries; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
8 |
|
protected function sectionEntries(HtmlPageCrawler $crawler, string $file) |
|
56
|
|
|
{ |
|
57
|
8 |
|
$entries = collect(); |
|
58
|
|
|
|
|
59
|
8 |
|
$crawler->filter('h2')->each(function (HtmlPageCrawler $node) use ($entries, $file) { |
|
60
|
8 |
|
$entries->push([ |
|
61
|
8 |
|
'name' => $node->text(), |
|
62
|
8 |
|
'type' => 'Section', |
|
63
|
8 |
|
'path' => Str::after($file . '#' . Str::slug($node->text()), $this->innerDirectory()) |
|
64
|
|
|
]); |
|
65
|
8 |
|
}); |
|
66
|
|
|
|
|
67
|
8 |
|
return $entries; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
4 |
|
public function format(string $file): string |
|
71
|
|
|
{ |
|
72
|
4 |
|
$crawler = HtmlPageCrawler::create(Storage::get($file)); |
|
73
|
|
|
|
|
74
|
4 |
|
$this->removeHeaderContentExceptSamples($crawler); |
|
75
|
4 |
|
$this->removeLeftSidebar($crawler); |
|
76
|
4 |
|
$this->centerContent($crawler); |
|
77
|
4 |
|
$this->removeEditLink($crawler); |
|
78
|
|
|
|
|
79
|
4 |
|
$this->removePackageVersionBadges($crawler); |
|
80
|
|
|
|
|
81
|
4 |
|
$this->insertDashTableOfContents($crawler); |
|
82
|
|
|
|
|
83
|
4 |
|
return $crawler->saveHTML(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
4 |
|
protected function removeHeaderContentExceptSamples(HtmlPageCrawler $crawler) |
|
87
|
|
|
{ |
|
88
|
4 |
|
$header = $crawler->filter('header'); |
|
89
|
|
|
|
|
90
|
4 |
|
$header->filter('header > *:not(:last-child)')->remove(); |
|
91
|
4 |
|
$header->filter('header form')->remove(); |
|
92
|
4 |
|
$header->filter('header .nav-item:not(:nth-child(3))')->remove(); |
|
93
|
4 |
|
$header->filter('header nav > a')->remove(); |
|
94
|
4 |
|
} |
|
95
|
|
|
|
|
96
|
4 |
|
protected function removeLeftSidebar(HtmlPageCrawler $crawler) |
|
97
|
|
|
{ |
|
98
|
4 |
|
$crawler->filter('.sidebar')->remove(); |
|
99
|
4 |
|
} |
|
100
|
|
|
|
|
101
|
4 |
|
protected function centerContent(HtmlPageCrawler $crawler) |
|
102
|
|
|
{ |
|
103
|
4 |
|
$crawler->filter('.page')->css('padding-left', '0'); |
|
104
|
4 |
|
} |
|
105
|
|
|
|
|
106
|
4 |
|
protected function removeEditLink(HtmlPageCrawler $crawler) |
|
107
|
|
|
{ |
|
108
|
4 |
|
$crawler->filter('.edit-link')->remove(); |
|
109
|
4 |
|
} |
|
110
|
|
|
|
|
111
|
4 |
|
protected function removePackageVersionBadges(HtmlPageCrawler $crawler) |
|
112
|
|
|
{ |
|
113
|
|
|
$crawler |
|
114
|
4 |
|
->filter('p > a:first-child > img') |
|
115
|
4 |
|
->each(function ($node) { |
|
116
|
4 |
|
$node->closest('a')->remove(); |
|
117
|
4 |
|
}); |
|
118
|
4 |
|
} |
|
119
|
|
|
|
|
120
|
4 |
|
protected function insertDashTableOfContents(HtmlPageCrawler $crawler) |
|
121
|
|
|
{ |
|
122
|
4 |
|
$crawler->filter('head') |
|
123
|
4 |
|
->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>'); |
|
124
|
|
|
|
|
125
|
4 |
|
$crawler->filter('h2, h3')->each(static function (HtmlPageCrawler $node) { |
|
126
|
4 |
|
$node->before( |
|
127
|
4 |
|
'<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>' |
|
128
|
|
|
); |
|
129
|
4 |
|
}); |
|
130
|
4 |
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|