| 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 |  |  |         $entries = $entries->merge($this->sectionEntries($crawler, $file)); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         return $entries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     protected function guideEntries(HtmlPageCrawler $crawler, string $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $entries = collect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         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 |  |  |                 var_dump($this->url() . '/' . $node->children('a')->attr('href')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 $entries->push([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                     'name' => trim($node->text()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     'type' => 'Guide', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     'path' => $this->url() . '/' . $node->children('a')->attr('href') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return $entries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     protected function sectionEntries(HtmlPageCrawler $crawler, string $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $entries = collect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $crawler->filter('main > div > h2')->each(function (HtmlPageCrawler $node) use ($entries, $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             if ($node->children('a')->count() != 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 $entries->push([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                     'name' => trim($node->text()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                     'type' => 'Section', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     'path' => Str::after($file . '#' . Str::slug($node->text()), $this->innerDirectory()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         return $entries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     public function format(string $file): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $crawler = HtmlPageCrawler::create(Storage::get($file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $this->removeHeader($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $this->removeLeftSidebar($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->removeRightSidebar($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $this->insertOnlineRedirection($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $this->insertDashTableOfContents($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         return $crawler->saveHTML(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     protected function removeHeader(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $crawler->filter('header')->remove(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     protected function removeLeftSidebar(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $crawler->filter('body > aside:first-of-type')->remove(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     protected function removeRightSidebar(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $crawler->filter('body > aside:last-of-type')->remove(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     protected function insertOnlineRedirection(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $crawler->filter('html')->prepend("<!-- Online page at https://github.com/alpinejs/alpine -->"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     protected function insertDashTableOfContents(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $crawler->filter('head') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $crawler->filter('h3')->each(function (HtmlPageCrawler $node) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             $node->prepend( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 '<a id="' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                 . Str::slug($node->text()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                 . '" name="//apple_ref/cpp/Section/' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 . rawurlencode($node->text()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                 . '" class="dashAnchor"></a>' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |  |