Revision [6083]

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

 

Code Snippet Handler

Last edited by DarTar:
Cloned from CodeSnippetHandler
Thu, 17 Feb 2005 14:03 UTC [diff]


See also:
Documentation: CodeSnippetHandlerInfo.
This is the development page for the Code Snippet 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


<?php
echo "Hello world!";
?>



The code


1. Modify the formatter

Make the following modifications in formatters/wakka.php

original
                        return $output;
                }


modified
          //build form
                        $form = $wakka->FormOpen("codesnippet");
                        $form .= '<input type="submit" name="save" value="Save to file" />';
                        $form .= '<input type="hidden" name="code" value="'.urlencode($code).'" />';
                        $form .= $wakka->FormClose();
               
                        // output
                        return $output.'<br />'.$form;
                }


2. Create a codesnippet handler

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

<?php
$code = urldecode($_POST["code"]);
$filename = ($_POST["name"])? $_POST["name"] : "snippet.php"; # forthcoming
//header('Content-type: text/plain');
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 . '"');
//header('Connection: close');
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));  
                }
        // code snippet handler
                elseif ($this->method == "codesnippet")
                {
                        print($this->Method($this->method));
                }


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


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



Issues




References


PHP:header


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