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\HtmlPage; |
10
|
|
|
use Wa72\HtmlPageDom\HtmlPageCrawler; |
11
|
|
|
|
12
|
|
|
class LaravelZero extends BaseDocset |
13
|
|
|
{ |
14
|
|
|
public const CODE = 'laravel-zero'; |
15
|
|
|
public const NAME = 'Laravel Zero'; |
16
|
|
|
public const URL = 'laravel-zero.com'; |
17
|
|
|
public const INDEX = 'docs/introduction.html'; |
18
|
|
|
public const PLAYGROUND = ''; |
19
|
|
|
public const ICON_16 = '../../icons/icon.png'; |
20
|
|
|
public const ICON_32 = '../../icons/[email protected]'; |
21
|
|
|
public const EXTERNAL_DOMAINS = [ |
22
|
|
|
'raw.githubusercontent.com', |
23
|
|
|
'googleapis.com', |
24
|
|
|
]; |
25
|
|
|
|
26
|
12 |
|
public function entries(string $file): Collection |
27
|
|
|
{ |
28
|
12 |
|
$crawler = HtmlPageCrawler::create(Storage::get($file)); |
29
|
|
|
|
30
|
12 |
|
$entries = collect(); |
31
|
12 |
|
$entries = $entries->merge($this->guideEntries($crawler, $file)); |
32
|
12 |
|
$entries = $entries->merge($this->sectionEntries($crawler, $file)); |
33
|
|
|
|
34
|
12 |
|
return $entries; |
35
|
|
|
} |
36
|
|
|
|
37
|
12 |
|
protected function guideEntries(HtmlPageCrawler $crawler, string $file) |
38
|
|
|
{ |
39
|
12 |
|
$entries = collect(); |
40
|
|
|
|
41
|
12 |
|
if (Str::contains($file, "{$this->url()}/docs/introduction.html")) { |
42
|
6 |
|
$crawler->filter('.docs-nav a')->each(function (HtmlPageCrawler $node) use ($entries) { |
43
|
6 |
|
$entries->push([ |
44
|
6 |
|
'name' => trim($node->text()), |
45
|
6 |
|
'type' => 'Guide', |
46
|
6 |
|
'path' => $this->url() . '/docs/' . $node->attr('href') |
47
|
|
|
]); |
48
|
6 |
|
}); |
49
|
|
|
} |
50
|
|
|
|
51
|
12 |
|
return $entries; |
52
|
|
|
} |
53
|
|
|
|
54
|
12 |
|
protected function sectionEntries(HtmlPageCrawler $crawler, string $file) |
55
|
|
|
{ |
56
|
12 |
|
$entries = collect(); |
57
|
|
|
|
58
|
12 |
|
if (! $this->is404OrIndex($file)) { |
59
|
12 |
|
$crawler->filter('h2, h3, h4')->each(function (HtmlPageCrawler $node) use ($entries, $file) { |
60
|
12 |
|
$entries->push([ |
61
|
12 |
|
'name' => trim($node->text()), |
62
|
12 |
|
'type' => 'Section', |
63
|
12 |
|
'path' => Str::after($file . '#' . Str::slug($node->text()), $this->innerDirectory()), |
64
|
|
|
]); |
65
|
12 |
|
}); |
66
|
|
|
} |
67
|
|
|
|
68
|
12 |
|
return $entries; |
69
|
|
|
} |
70
|
|
|
|
71
|
12 |
|
protected function is404OrIndex($file) |
72
|
|
|
{ |
73
|
12 |
|
return Str::contains($file, "{$this->url()}/index.html") |
74
|
12 |
|
|| Str::contains($file, "{$this->url()}/404/index.html"); |
75
|
|
|
} |
76
|
|
|
|
77
|
12 |
|
public function format(string $file): string |
78
|
|
|
{ |
79
|
12 |
|
$crawler = HtmlPageCrawler::create(Storage::get($file)); |
80
|
|
|
|
81
|
12 |
|
$this->hideHeader($crawler); |
82
|
12 |
|
$this->removeLeftSidebar($crawler); |
83
|
12 |
|
$this->removeEditThisPageLink($crawler); |
84
|
12 |
|
$this->removeFooter($crawler); |
85
|
|
|
|
86
|
12 |
|
$this->updateTopMargin($crawler); |
87
|
12 |
|
$this->updateContainerWidth($crawler); |
88
|
12 |
|
$this->updateBottomMargin($crawler); |
89
|
|
|
|
90
|
12 |
|
$this->insertOnlineRedirection($crawler, $file); |
91
|
12 |
|
$this->insertDashTableOfContents($crawler); |
92
|
|
|
|
93
|
12 |
|
return $crawler->saveHTML(); |
94
|
|
|
} |
95
|
|
|
|
96
|
12 |
|
protected function hideHeader(HtmlPageCrawler $crawler) |
97
|
|
|
{ |
98
|
|
|
/** |
99
|
|
|
* hide rather than remove |
100
|
|
|
* |
101
|
|
|
* if we remove the header, the code formatting stops working |
102
|
|
|
* so we hide it instead. genius. |
103
|
|
|
*/ |
104
|
12 |
|
$crawler->filter('body > header')->addClass('hidden'); |
105
|
12 |
|
} |
106
|
|
|
|
107
|
12 |
|
protected function removeLeftSidebar(HtmlPageCrawler $crawler) |
108
|
|
|
{ |
109
|
12 |
|
$crawler->filter('.docs-nav')->remove(); |
110
|
12 |
|
$crawler->filter('nav.hidden.mt-1')->remove(); |
111
|
12 |
|
} |
112
|
|
|
|
113
|
12 |
|
protected function removeEditThisPageLink(HtmlPageCrawler $crawler) |
114
|
|
|
{ |
115
|
12 |
|
$crawler->filter('.DocSearch-content > div')->remove(); |
116
|
12 |
|
} |
117
|
|
|
|
118
|
12 |
|
protected function removeFooter(HtmlPageCrawler $crawler) |
119
|
|
|
{ |
120
|
12 |
|
$crawler->filter('body > footer')->remove(); |
121
|
12 |
|
} |
122
|
|
|
|
123
|
12 |
|
protected function updateTopMargin(HtmlPageCrawler $crawler) |
124
|
|
|
{ |
125
|
12 |
|
$crawler->filter('h1') |
126
|
12 |
|
->css('margin-top', '1rem') |
127
|
|
|
; |
128
|
12 |
|
} |
129
|
|
|
|
130
|
12 |
|
protected function updateContainerWidth(HtmlPageCrawler $crawler) |
131
|
|
|
{ |
132
|
12 |
|
$container = $crawler->filter('.DocSearch-content'); |
133
|
|
|
|
134
|
12 |
|
$container->removeClass('lg:ml-10') |
135
|
12 |
|
->removeClass('lg:px-0') |
136
|
12 |
|
->removeClass('xl:ml-16') |
137
|
12 |
|
->removeClass('px-3') |
138
|
|
|
; |
139
|
|
|
|
140
|
12 |
|
$container->addClass('px-6'); |
141
|
12 |
|
} |
142
|
|
|
|
143
|
12 |
|
protected function updateBottomMargin(HtmlPageCrawler $crawler) |
144
|
|
|
{ |
145
|
12 |
|
$content = $crawler->filter('section > div > div'); |
146
|
|
|
|
147
|
12 |
|
$content->removeClass('mb-20'); |
148
|
|
|
|
149
|
12 |
|
$content->css('margin-bottom', '4rem'); |
150
|
12 |
|
} |
151
|
|
|
|
152
|
12 |
|
protected function insertOnlineRedirection(HtmlPageCrawler $crawler, string $file) |
153
|
|
|
{ |
154
|
12 |
|
$onlineUrl = Str::substr(Str::after($file, $this->innerDirectory()), 1, -5); |
155
|
|
|
|
156
|
12 |
|
$crawler->filter('html')->prepend("<!-- Online page at https://$onlineUrl -->"); |
157
|
12 |
|
} |
158
|
|
|
|
159
|
12 |
|
protected function insertDashTableOfContents(HtmlPageCrawler $crawler) |
160
|
|
|
{ |
161
|
12 |
|
$crawler->filter('h1') |
162
|
12 |
|
->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>'); |
163
|
|
|
|
164
|
12 |
|
$crawler->filter('h2, h3, h4')->each(static function (HtmlPageCrawler $node) { |
165
|
12 |
|
$node->before( |
166
|
12 |
|
'<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>' |
167
|
|
|
); |
168
|
12 |
|
}); |
169
|
12 |
|
} |
170
|
|
|
} |
171
|
|
|
|