xref: /dokuwiki/inc/parser/code.php (revision a27c9d6f6c03a5b93871dd414021317c2bb9eac8)
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        global $INPUT;
20        if(!$language) $language = 'txt';
21        if(!$filename) $filename = 'snippet.'.$language;
22        $filename = utf8_basename($filename);
23        $filename = utf8_stripspecials($filename, '_');
24
25        if($this->_codeblock == $INPUT->str('codeblock')){
26            header("Content-Type: text/plain; charset=utf-8");
27            header("Content-Disposition: attachment; filename=$filename");
28            header("X-Robots-Tag: noindex");
29            echo trim($text,"\r\n");
30            exit;
31        }
32
33        $this->_codeblock++;
34    }
35
36    /**
37     * Wraps around code()
38     */
39    function file($text, $language = null, $filename='') {
40        $this->code($text, $language, $filename);
41    }
42
43    /**
44     * This should never be reached, if it is send a 404
45     */
46    function document_end() {
47        http_status(404);
48        echo '404 - Not found';
49        exit;
50    }
51
52    /**
53     * Return the format of the renderer
54     *
55     * @returns string 'code'
56     */
57    function getFormat(){
58        return 'code';
59    }
60}
61