13d491f75SAndreas Gohr<?php 23d491f75SAndreas Gohr/** 33d491f75SAndreas Gohr * A simple renderer that allows downloading of code and file snippets 43d491f75SAndreas Gohr * 53d491f75SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 63d491f75SAndreas Gohr */ 73d491f75SAndreas Gohrif(!defined('DOKU_INC')) die('meh.'); 83d491f75SAndreas Gohrrequire_once DOKU_INC . 'inc/parser/renderer.php'; 93d491f75SAndreas Gohr 103d491f75SAndreas Gohrclass Doku_Renderer_code extends Doku_Renderer { 113d491f75SAndreas Gohr var $_codeblock=0; 123d491f75SAndreas Gohr 133d491f75SAndreas Gohr /** 143d491f75SAndreas Gohr * Send the wanted code block to the browser 153d491f75SAndreas Gohr * 163d491f75SAndreas Gohr * When the correct block was found it exits the script. 173d491f75SAndreas Gohr */ 183d491f75SAndreas Gohr function code($text, $language = NULL, $filename='' ) { 193d491f75SAndreas Gohr if(!$language) $language = 'txt'; 203d491f75SAndreas Gohr if(!$filename) $filename = 'snippet.'.$language; 213d491f75SAndreas Gohr $filename = basename($filename); 223d491f75SAndreas Gohr 233d491f75SAndreas Gohr if($this->_codeblock == $_REQUEST['codeblock']){ 243d491f75SAndreas Gohr header("Content-Type: text/plain; charset=utf-8"); 253d491f75SAndreas Gohr header("Content-Disposition: attachment; filename=$filename"); 263d491f75SAndreas Gohr header("X-Robots-Tag: noindex"); 273d491f75SAndreas Gohr echo trim($text,"\r\n"); 283d491f75SAndreas Gohr exit; 293d491f75SAndreas Gohr } 303d491f75SAndreas Gohr 313d491f75SAndreas Gohr $this->_codeblock++; 323d491f75SAndreas Gohr } 333d491f75SAndreas Gohr 343d491f75SAndreas Gohr /** 353d491f75SAndreas Gohr * Wraps around code() 363d491f75SAndreas Gohr */ 37*f9d4952bSAndreas Gohr function file($text, $language = NULL, $filename='') { 38*f9d4952bSAndreas Gohr $this->code($text, $language, $filename); 393d491f75SAndreas Gohr } 403d491f75SAndreas Gohr 413d491f75SAndreas Gohr /** 423d491f75SAndreas Gohr * This should never be reached, if it is send a 404 433d491f75SAndreas Gohr */ 443d491f75SAndreas Gohr function document_end() { 453d491f75SAndreas Gohr header("HTTP/1.0 404 Not Found"); 463d491f75SAndreas Gohr echo '404 - Not found'; 473d491f75SAndreas Gohr exit; 483d491f75SAndreas Gohr } 493d491f75SAndreas Gohr 503d491f75SAndreas Gohr /** 513d491f75SAndreas Gohr * Return the format of the renderer 523d491f75SAndreas Gohr * 533d491f75SAndreas Gohr * @returns string 'code' 543d491f75SAndreas Gohr */ 553d491f75SAndreas Gohr function getFormat(){ 563d491f75SAndreas Gohr return 'code'; 573d491f75SAndreas Gohr } 583d491f75SAndreas Gohr} 59