Revision [6095]
This is an old revision of GrabCodeHandler made by DarTar on 2005-02-17 14:55:43.
Grab Code Handler
See also:
Documentation: GrabCodeHandlerInfo.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:
- switching this option on/off;
- display a download button only for code blocks longer than n lines.
[2004-02-17] - I've uploaded this handler on this server as a beta feature. Feedback is welcome. See the issues section at the bottom of this page for more details.
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;
}
$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('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;
?>
$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));
}
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));
}
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
B. modified
Issues
- There are some known issues related to the way in which browsers interpret the Content-Disposition header sent by PHP (especially with IE) and I will try to figure out how this can be fixed. Help/suggestions are welcome.
- Currently the handler is stored in the handlers/page/ folder, but it is not a page handler (it does not perform operations on the page as a whole) so we might try to find a better organization in the handlers file structure.
- It seems that Content-type: application/force-download doesn't work under M$Windows/IE so I changed it back to Content-type: text/plain which should just display a text version of the code snippet.
References
PHP:header
CategoryDevelopment