Created
June 9, 2015 19:20
-
-
Save barryvdh/a6b00c9d5a921f6cfad8 to your computer and use it in GitHub Desktop.
Laravel docs scraper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// composer require fabpot/goutte | |
Route::get('docs', function(){ | |
$host = 'http://laravel.com'; | |
$base = '/docs/5.1/'; | |
$client = new Goutte\Client(); | |
$crawler = $client->request('GET', $host.$base); | |
$head = $crawler->filter('head')->first()->html(); | |
$head = str_replace('/build/assets/', $host.'/build/assets/', $head); | |
$nav = $crawler->filter('.slide-docs-nav')->first(); | |
$body = '<article>'.$nav->html().'</article>'; | |
foreach ($nav->filter('a')->links() as $link) { | |
$article = $client->click($link)->filter('article')->first(); | |
$id = basename($link->getUri()); | |
$body .= '<article id='.$id.'>'.$article->html().'</article>'; | |
} | |
$body = str_replace('href="'.$base, 'href="#', $body); | |
$body = str_replace('#collections#', '#', $body); | |
$output = <<<EOT | |
<html> | |
<head> | |
$head | |
<style> | |
@media print { | |
article { | |
page-break-after: always; | |
} | |
p, pre { | |
page-break-inside: avoid; | |
} | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
$body | |
</div> | |
</body> | |
</html> | |
EOT; | |
return $output; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice :) .