Revision [6089]

This is an old revision of GrabCodeHandler made by DarTar on 2005-02-17 14:10:33.

 

Grab Code Handler

Last edited by DarTar:
First draft of GrabCode handler
Thu, 17 Feb 2005 14:10 UTC [diff]


See also:
Documentation: GrabCodeHandlerInfo.
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.
In the future, admin-configurable options will be added to allow:
  1. switching this option on/off;
  1. display a download button only for code blocks longer than n lines.


Sample output


  1. <?php
  2. echo "Hello world!";
  3. ?>



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" style="float:right; margin-right:20px;" name="save" value="Save to file" title="Grab code"/>';
                        $form .= '<input type="hidden" name="code" value="'.urlencode($code).'" />';
                        $form .= $wakka->FormClose();
               
                        // output
                        return $output.'<br />'.$form;
                }


2. Create a grabcode handler

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

<?php
$code = urldecode($_POST["code"]);
//$filename = ($_POST["name"])? $_POST["name"] : "snippet.php"; # forthcoming
$filename = "codesnippet.php"; # forthcoming
header('Content-type: application/force-download');
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


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