CakePhp is one of the most popular php-based web application frameworks, but it’s not without its frustrations. I’m not speaking of its steep learning curve, but its misnamed cookbook, which is really its manual.  Try as you may, you won’t get their cookbook to print properly, and there’s no version in book form that you can buy. Did they make it this way intentionally?
If you’re a little bit patient, I have a free solution for you, that will save you a bundle on ink cartridges. It’s a little script which you can use that will churn out printable versions of the cookbook pages you need, minus the stuff that causes the problems and the header and sidebar. I can’t host the script on my site since it might get me the wrath of the CakePhp people, and I can’t offer you a ready made printable manual for the same reason. Thus, the fair thing to do is to give you the code below and you use it on your own web site on a non-public page for your own use.
Usage: Just enter the full cookbook URLs into the form and it will fetch them, parse them, and then combine them into one html doc, which you can then save or print directly from your browser. It’s probably not a good idea to try to paste hundreds of URLs at a time. What I did was go section by section. Here’s the code…
<?php
/**
* By Tom Germain, cgiware.com
* Free for personal use only.
* CakePHP cookbook printable page generator
* Just install this on your web site and use it!
**/
if(isset($_REQUEST['url'])&&!empty($_REQUEST['url'])){
$URLS=explode("\n",$_REQUEST['url']);
if(count($URLS)==1) $URLS=explode("\r",$_REQUEST['url']);
$DOC="";
foreach($URLS as $url){
$url=trim($url);
if(!$url) continue;
$HTML=file_get_contents($url);
if(!$HTML){
echo "Could not fetch url: $url";
exit;
}
$x=explode('<div class="node-nav">',$HTML);
$HTML=array_shift($x);
$x=explode('<div id="content">',$HTML);
$HTML=array_pop($x);
$HTML=preg_replace('/<a href="http\:\/\/cakefest\.org".+?<\/a>\n/','',$HTML);
$x=explode('<ol class="code">',$HTML);
$HTML=array_shift($x);
foreach($x as $xx){
$y=explode("</ol>",$xx);
$HTML.=array_pop($y);
}
$HTML=preg_replace('/<div class="options">(.|\n)+?<\/div>/m','',$HTML);
$HTML=preg_replace('/<div class="comments".+?<\/div>/','',$HTML);
$HTML=str_replace('class="code"','style="color: blue; font-style:italic;"',$HTML);
if($DOC) $DOC.="<p><hr><p>";
$DOC.=$HTML;
sleep(1);
}
header("Content-Type: text/html; charset=utf-8");
print $DOC;
exit;
}
?>
<form method="POST">
Enter URLs and hit button: <p><textarea cols=80 rows=40 name="url"></textarea>
<p>
<input type=submit>
</form>
Related Articles:
Tags: cakephp, printable cookbook




Tom Germain
Today
Be the first to leave a comment!