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