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 = ''; |
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->insertDashTableOfContents($crawler); |
80
|
|
|
|
81
|
4 |
|
return $crawler->saveHTML(); |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
protected function removeHeaderContentExceptSamples(HtmlPageCrawler $crawler) |
85
|
|
|
{ |
86
|
4 |
|
$header = $crawler->filter('header'); |
87
|
|
|
|
88
|
4 |
|
$header->filter('header > *:not(:last-child)')->remove(); |
89
|
4 |
|
$header->filter('header form')->remove(); |
90
|
4 |
|
$header->filter('header .nav-item:not(:nth-child(3))')->remove(); |
91
|
4 |
|
$header->filter('header nav > a')->remove(); |
92
|
4 |
|
} |
93
|
|
|
|
94
|
4 |
|
protected function removeLeftSidebar(HtmlPageCrawler $crawler) |
95
|
|
|
{ |
96
|
4 |
|
$crawler->filter('.sidebar')->remove(); |
97
|
4 |
|
} |
98
|
|
|
|
99
|
4 |
|
protected function centerContent(HtmlPageCrawler $crawler) |
100
|
|
|
{ |
101
|
4 |
|
$crawler->filter('.page')->css('padding-left', '0'); |
102
|
4 |
|
} |
103
|
|
|
|
104
|
4 |
|
protected function removeEditLink(HtmlPageCrawler $crawler) |
105
|
|
|
{ |
106
|
4 |
|
$crawler->filter('.edit-link')->remove(); |
107
|
4 |
|
} |
108
|
|
|
|
109
|
4 |
|
protected function insertDashTableOfContents(HtmlPageCrawler $crawler) |
110
|
|
|
{ |
111
|
4 |
|
$crawler->filter('head') |
112
|
4 |
|
->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>'); |
113
|
|
|
|
114
|
4 |
|
$crawler->filter('h2, h3')->each(static function (HtmlPageCrawler $node) { |
115
|
4 |
|
$node->before( |
116
|
4 |
|
'<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>' |
117
|
|
|
); |
118
|
4 |
|
}); |
119
|
4 |
|
} |
120
|
|
|
} |
121
|
|
|
|