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
Comments
Comment by ChristianBarthelemy
2005-02-17 18:23:01
Cool :-))
Comment by JeroenJansen
2005-02-18 10:24:47
I'm with Christian on this, good job DartTar!
Comment by DarTar
2005-02-18 10:34:19
Glad to hear that :-) Next step will be to add support for filenames (so that the downloader automatically adds the right name/extension), add support for an optional caption, and... (this I think would be terrific) add a button to allow editing just the code block without opening the whole page. This is more or less what happens in section editing, but I think it would be even more interesting in the case of code blocks...
Comment by MikePalmer
2005-02-20 00:59:09
Looks cool.

One thing that might be neat is expanding this to another option in the code formatter area that is used for geshi and do something like (php;1;afile.php) and have content disposition add a file name to the header for the grabcode handler.

Just a thought
Comment by JavaWoman
2005-02-20 10:32:28
MikePalmer,
That is *exactly* what we already had in mind as a next step! ;-)
(The GeSHi wrapper function will have to be adapted for that, too - as well as the handling of built-in code formatters.)
Comment by MikePalmer
2005-02-21 08:13:39
Doh! Obviously I didn't read through the comments to see DarTar's post.
Comment by IanHayhurst
2005-10-12 13:12:43
I spent all morning to get this to work with IE, investigating different headers caching and content... seems it's down to caching (ok so this is well known) it took me all morning to discover the line that matters is in wikka.php
(not apache, php.ini or grabcode.php)
I commented out the line:-
header("Cache-Control: no-cache");
(1197 in the standard wikka.php)

I dont know what the implications of that will be yet...
Comment by DarTar
2005-10-12 18:43:12
Ian, what is *exactly*your problem with IE? I've just tested the grab button on this site with IE6/WinXP and it works just fine, or are you referring to sth else?
Comment by IanHayhurst
2005-10-12 22:00:31
IE prompts you with save or open dialog, then when you click either option an error box pops up saying
"Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later.",
personaly I only use it to check paces render ok... users were complaining though fr reference ir was IE6 on W2K (display of the headers showed that despite code to the contrary in grabhandler.php the page was not cacheable... that changed when commented in wikka.php)
Comment by IanHayhurst
2005-10-13 08:45:07
For reference the grabcodehandler (or the cache header in wikka.php) on this site doesnt work with whatever (possibly mangled corporate) configuration of IE we have either (ruling out hopefully my apache and php configs)
Comment by DarTar
2005-10-13 08:55:47
That's really weird (I've tested it with a couple of IE versions on different OS and it always worked for me). Anyway, thanks for pointing this out: we'll have to check whether the no-cache line was introduced for some specific reasons before we remove it.
Comment by IanHayhurst
2005-11-07 12:26:42
mmm taking out the header("Cache-Control: no-cache"); had some nasty downstream effects on IE,(though they took a while to appear) so It looks like you pick which IE bug you want to live with, I've opted for leaving it in and having the grab handler not work for IE users
Comment by GiorgosKontopoulos
2006-02-16 01:41:42
Just a suggestion:
Is it too much to ask for a button that displayed the name of the file you are downloading, optionally specified through the edit box (i.e %%php ... ,"wikka.php" ...) and that would result in "Grab wikka.php" and the download dialog box would have the "wikka.php" already instead of the default "codesnippet.txt"

Or the grabcode handler could get it from inside the code from a comment (i.e. @filename=wikka.php)
Comment by DarTar
2006-02-16 02:03:32
Giorgos, this is one of the ideas we were discussing for the official release of the code :)
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki