xref: /dokuwiki/inc/parser/code.php (revision 929bbe7f98cd7525cfea40c806d567ee2f1f7366)
1<?php
2/**
3 * A simple renderer that allows downloading of code and file snippets
4 *
5 * @author Andreas Gohr <andi@splitbrain.org>
6 */
7if(!defined('DOKU_INC')) die('meh.');
8require_once DOKU_INC . 'inc/parser/renderer.php';
9
10class Doku_Renderer_code extends Doku_Renderer {
11    var $_codeblock=0;
12
13    /**
14     * Send the wanted code block to the browser
15     *
16     * When the correct block was found it exits the script.
17     */
18    function code($text, $language = NULL, $filename='' ) {
19        if(!$language) $language = 'txt';
20        if(!$filename) $filename = 'snippet.'.$language;
21        $filename = basename($filename);
22
23        if($this->_codeblock == $_REQUEST['codeblock']){
24            header("Content-Type: text/plain; charset=utf-8");
25            header("Content-Disposition: attachment; filename=$filename");
26            header("X-Robots-Tag: noindex");
27            echo trim($text,"\r\n");
28            exit;
29        }
30
31        $this->_codeblock++;
32    }
33
34    /**
35     * Wraps around code()
36     */
37    function file($text, $language = NULL, $filename='') {
38        $this->code($text, $language, $filename);
39    }
40
41    /**
42     * This should never be reached, if it is send a 404
43     */
44    function document_end() {
45        header("HTTP/1.0 404 Not Found");
46        echo '404 - Not found';
47        exit;
48    }
49
50    /**
51     * Return the format of the renderer
52     *
53     * @returns string 'code'
54     */
55    function getFormat(){
56        return 'code';
57    }
58}
59