| 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 Chartjs extends BaseDocset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     public const CODE = 'chartjs'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public const NAME = 'Chart.js'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public const URL = 'www.chartjs.org'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     public const INDEX = 'docs/latest/index.html'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public const PLAYGROUND = 'https://codepen.io/lucusc/pen/pgQRdd'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public const ICON_16 = '../../icons/icon.png'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public const ICON_32 = '../../icons/[email protected]'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public const EXTERNAL_DOMAINS = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         'use.typekit.net', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function grab(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $toIgnore = implode('|', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             '/cdn-cgi', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             '/docs/2.9.3', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             '/docs/master', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             '/samples/master' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $toGet = implode('|', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             '\.css', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             '\.ico', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             '\.js', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             '\.svg', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             '/docs', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             '/samples' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         system( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             "echo; wget www.chartjs.org \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 --mirror \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 --trust-server-names \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 --reject-regex='{$toIgnore}' \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 --accept-regex='{$toGet}' \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 --ignore-case \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 --page-requisites \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 --adjust-extension \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 --convert-links \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 --span-hosts \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 --domains={$this->externalDomains()} \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 --directory-prefix=storage/{$this->downloadedDirectory()} \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 -e robots=off \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 --quiet \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 --show-progress", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return $result === 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 12 |  |     public function entries(string $file): Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 12 |  |         $crawler = HtmlPageCrawler::create(Storage::get($file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 12 |  |         $entries = collect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 12 |  |         $entries = $entries->merge($this->guideEntries($crawler, $file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 12 |  |         $entries = $entries->merge($this->sectionEntries($crawler, $file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 12 |  |         return $entries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 12 |  |     protected function guideEntries(HtmlPageCrawler $crawler, string $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 12 |  |         $entries = collect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 12 |  |         if (Str::contains($file, "{$this->url()}/docs/latest/index.html")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $crawler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 12 |  |                 ->filter('.summary > li.chapter:not(:first-child) a') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 ->each(function (HtmlPageCrawler $node) use ($entries) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 12 |  |                     $entries->push([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 12 |  |                         'name' => $node->text(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 12 |  |                         'type' => 'Guide', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 12 |  |                         'path' => $this->url() . '/docs/latest/' . $this->cleanUrl($node->attr('href')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                     ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 12 |  |                 }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 12 |  |         return $entries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 12 |  |     protected function sectionEntries(HtmlPageCrawler $crawler, string $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 12 |  |         $entries = collect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $crawler->filter('h2')->each(function (HtmlPageCrawler $node) use ($entries, $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 12 |  |             $entries->push([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 12 |  |                 'name' => $node->text(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 12 |  |                 'type' => 'Section', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 12 |  |                 'path' => Str::after($file . '#' . Str::slug($node->text()), $this->innerDirectory()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 12 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 12 |  |         return $entries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * Some links are not converted correctly by wget once | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * the download is finished. no idea why, but this cleans | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * the few that fail. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 12 |  |     protected function cleanUrl($url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 12 |  |         return Str::after($url, 'https://www.chartjs.org/docs/latest/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 12 |  |     public function format(string $file): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 12 |  |         $crawler = HtmlPageCrawler::create(Storage::get($file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 12 |  |         $this->removeLeftSidebar($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 12 |  |         $this->removeMenuAndSharingButtons($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 12 |  |         $this->removeNavigation($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 12 |  |         $this->makeContentFullWidth($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 12 |  |         $this->removeSearchResults($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 12 |  |         $this->removeUnwantedCSS($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 12 |  |         $this->removeUnwantedJavaScript($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 12 |  |         $this->insertDashTableOfContents($crawler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 12 |  |         return $crawler->saveHTML(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 12 |  |     protected function removeLeftSidebar(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 12 |  |         $crawler->filter('.book-summary')->remove(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 12 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 12 |  |     protected function removeMenuAndSharingButtons(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 12 |  |         $crawler->filter('body')->after( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 12 |  |             "<script>$(document).ready(function () { $('.pull-right.js-toolbar-action, .pull-left.btn').hide(); });</script>" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 12 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 12 |  |     protected function removeNavigation(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 12 |  |         $crawler->filter('.navigation')->remove(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 12 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 12 |  |     protected function makeContentFullWidth(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 12 |  |         $crawler->filter('.book-body')->setStyle('left', '0px !important'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 12 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 161 | 12 |  |     protected function removeSearchResults(HtmlPageCrawler $crawler) | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 163 | 12 |  |         $crawler->filter('.search-results')->remove(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 12 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 12 |  |     protected function removeUnwantedCSS(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 12 |  |         $crawler->filter('link[href*="search.css"]')->remove(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 12 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 12 |  |     protected function removeUnwantedJavaScript(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 12 |  |         $crawler->filter('script[src*="search.js"]')->remove(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 12 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 12 |  |     protected function insertDashTableOfContents(HtmlPageCrawler $crawler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 12 |  |         $crawler->filter('.page-inner') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 12 |  |             ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         $crawler->filter('h2, h3')->each(static function (HtmlPageCrawler $node) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 12 |  |             $node->before( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 12 |  |                 '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 12 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 12 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 187 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 188 |  |  |  |