Grab Code Handler

Last edited by DarTar:
Replaces old-style internal links with new pipe-split links.
Fri, 20 May 2016 07:38 UTC [diff]


See also:
Documentation: GrabCodeHandlerInfo
Other: ImprovedFormatter.
This is the development page for the Grab Code handler.
 



I'm working on a handler to select on the fly code blocks contained in Wikka pages and download them as files.
The idea is to add a Download button at the end of each code block in order to save the code snippet as a file with an appropriate name and extension.

Changelog




The code


1. Modify the formatter

Make the following modifications in formatters/wakka.php

original
                        return $output;
                }


modified
          //build form
                        $form = $wakka->FormOpen("grabcode");
                        $form .= '<input type="submit" class="grabcodebutton" style="float:right; margin-right:20px; margin-top:0px; font-size: 10px; color: #000;font-weight: normal; font-family: Verdana, Arial, sans-serif; background-color: #DDD; text-decoration: none; height:18px;" name="save" value="Grab" title="Download this code"/>';
                        $form .= '<input type="hidden" name="code" value="'.urlencode($code).'" />';
                        $form .= $wakka->FormClose();
               
                        // output
                        return "$output \n $form";
                }



2. Create a grabcode handler

Save the following code as handlers/page/grabcode.php

<?php
/**
 * Downloads a code snippet as a file.
 *
 * Usage: The handler is called from a form appended to any code block in Wikka pages.
 *
 * @package         Handlers
 * @name              grabcode
 * @author            {@link http://wikka.jsnx.com/DarTar Dario Taraborelli}
 * @version           0.1
 * @since             Wikka 1.1.X
 * @todo              - address header issues with different browsers.
 *                    - modify formatter to take care of filename.
 */


$code = urldecode($_POST["code"]);
//$filename = ($_POST["name"])? $_POST["name"] : "snippet.php"; # forthcoming
$filename = "codesnippet.php"; # forthcoming
//header('Content-type: application/force-download');
header('Content-type: text/plain');
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Content-Length: '.strlen($code));
header('Content-Description: $filename Download Data');
header('Pragma: no-cache');
header('Content-Disposition: attachment; filename="' . $filename . '"');
echo $code;
?>


3. Modify wikka.php

Make the two following modifications in ./wikka.php:

A. original
       // raw page handler
                elseif ($this->method == "raw")
                {
                        header("Content-type: text/plain");
                        print($this->Method($this->method));  
                }


A. modified
       // raw page handler
                elseif ($this->method == "raw")
                {
                        header("Content-type: text/plain");
                        print($this->Method($this->method));  
                }
        // grabcode handler
                elseif ($this->method == "grabcode")
                {
                        print($this->Method($this->method));
                }


B. original
if (!preg_match("/(xml|raw|mm)$/", $method))
{


B. modified
if (!preg_match("/(xml|raw|mm|grabcode)$/", $method))
{



Issues




References


PHP:header


CategoryDevelopmentHandlers
There are 14 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki