| 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 Alpinejs extends BaseDocset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     public const CODE = 'alpinejs'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public const NAME = 'Alpine.js'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public const URL = 'alpinejs.dev'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     public const INDEX = 'start-here.html'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public const PLAYGROUND = 'https://alpinejs.codewithhugo.com'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public const ICON_16 = '../../icons/icon.png'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public const ICON_32 = '../../icons/[email protected]'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public const EXTERNAL_DOMAINS = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function grab(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         system( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             "echo; wget github.com/alpinejs/alpine \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                 --mirror \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 --trust-server-names \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                 --ignore-case \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                 --page-requisites \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                 --adjust-extension \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 --convert-links \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 --span-hosts \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 --domains={$this->externalDomains()} \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 --directory-prefix=storage/{$this->downloadedDirectory()} \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 -e robots=off \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 --quiet \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 --show-progress", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         return $result === 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function entries(string $file): Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $crawler = HtmlPageCrawler::create(Storage::get($file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $entries = collect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $entries = $entries->merge($this->guideEntries($crawler, $file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 8 |  |         $entries = $entries->merge($this->sectionEntries($crawler, $file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 8 |  |         return $entries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 8 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     protected function guideEntries(HtmlPageCrawler $crawler, string $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 8 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 8 |  |         $entries = collect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 8 |  |         if (Str::contains($file, "{$this->url()}/start-here.html")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $crawler->filter('body > aside:first-of-type ul > li > ul > li')->each(function (HtmlPageCrawler $node) use ($entries) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 $entries->push([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 8 |  |                     'name' => trim($node->text()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                     'type' => 'Guide', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 8 |  |                     'path' => $this->url() . '/' . $node->children('a')->attr('href') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 8 |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 8 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 8 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 8 |  |         return $entries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 8 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 8 |  |     protected function sectionEntries(HtmlPageCrawler $crawler, string $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 8 |  |         $entries = collect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $crawler->filter('main > div > h2')->each(function (HtmlPageCrawler $node) use ($entries, $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 8 |  |             if ($node->children('a')->count() != 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 $entries->push([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 8 |  |                     'name' => trim($node->text()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                     'type' => 'Section', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 8 |  |                     'path' => Str::after($file . '#' . Str::slug($node->text()), $this->innerDirectory()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 8 |  |                 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 8 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 8 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 8 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return $entries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 8 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 8 |  |     public function format(string $file): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $crawler = HtmlPageCrawler::create(Storage::get($file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 8 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $this->removeHeader($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 8 |  |         $this->removeLeftSidebar($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $this->removeRightSidebar($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 8 |  |         $this->updateTopPadding($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 8 |  |         $this->updateLeftPadding($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 8 |  |         $this->updateContainerWidthAndMargins($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 8 |  |         $this->updateBottomPadding($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 8 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 8 |  |         $this->insertOnlineRedirection($crawler, $file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 8 |  |         $this->insertDashTableOfContents($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 8 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 8 |  |         return $crawler->saveHTML(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 8 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     protected function removeHeader(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 8 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $crawler->filter('header')->remove(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 8 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 8 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     protected function removeLeftSidebar(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 8 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $crawler->filter('body > aside:first-of-type')->remove(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 8 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     protected function removeRightSidebar(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 8 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 8 |  |         $crawler->filter('body > aside:last-of-type')->remove(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 123 | 8 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |     protected function updateTopPadding(HtmlPageCrawler $crawler) | 
            
                                                                        
                            
            
                                    
            
            
                | 125 | 8 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 8 |  |         $crawler->filter('main') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             ->removeClass('pt-24') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 8 |  |             ->addClass('pt-6') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 8 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 8 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     protected function updateLeftPadding(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 8 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         $crawler->filter('main') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 8 |  |             ->removeClass('md:pl-48') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 8 |  |             ->removeClass('lg:pl-64') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             ->removeClass('pl-0') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 8 |  |             ->addClass('pl-6') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             ->removeClass('xl:pr-64') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 8 |  |             ->removeClass('pr-0') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 8 |  |             ->addClass('pr-6') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 8 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 8 |  |     protected function updateContainerWidthAndMargins(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 8 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $crawler->filter('main > div:first-of-type') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 8 |  |             ->removeClass('m-auto') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             ->removeClass('max-w-3xl') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 8 |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 8 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 8 |  |     protected function updateBottomPadding(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 8 |  |         $crawler->filter('main > div:first-of-type') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 8 |  |             ->removeClass('pb-24') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             ->addClass('pb-6') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 8 |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 8 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 8 |  |     protected function insertOnlineRedirection(HtmlPageCrawler $crawler, string $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 8 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         $onlineUrl = Str::substr(Str::after($file, $this->innerDirectory()), 1, -5); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 8 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         $crawler->filter('html')->prepend("<!-- Online page at https://$onlineUrl -->"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 8 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 8 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     protected function insertDashTableOfContents(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 8 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         $crawler->filter('head') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 8 |  |             ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 8 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 8 |  |         $crawler->filter('h2, h3')->each(function (HtmlPageCrawler $node) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 8 |  |             $node->prepend( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |                 '<a id="' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 8 |  |                 . Str::slug($node->text()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |                 . '" name="//apple_ref/cpp/Section/' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 8 |  |                 . rawurlencode($node->text()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 8 |  |                 . '" class="dashAnchor"></a>' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 8 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 183 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 184 |  |  |  |