about summary refs log tree commit diff stats
path: root/wiki/inc/parser/code.php
blob: f91f1d2289161ad0453970e4471bda0c6b2fba89 (plain) (bl
module framagit.org/andinus/cetus

go 1.13

require framagit.org/andinus/indus v0.1.0
Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
<?php
/**
 * A simple renderer that allows downloading of code and file snippets
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */
if(!defined('DOKU_INC')) die('meh.');

class Doku_Renderer_code extends Doku_Renderer {
    var $_codeblock = 0;

    /**
     * Send the wanted code block to the browser
     *
     * When the correct block was found it exits the script.
     *
     * @param string $text
     * @param string $language
     * @param string $filename
     */
    function code($text, $language = null, $filename = '') {
        global $INPUT;
        if(!$language) $language = 'txt';
        $language = preg_replace(PREG_PATTERN_VALID_LANGUAGE, '', $language);
        if(!$filename) $filename = 'snippet.'.$language;
        $filename = utf8_basename($filename);
        $filename = utf8_stripspecials($filename, '_');

        // send CRLF to Windows clients
        if(strpos($INPUT->server->str('HTTP_USER_AGENT'), 'Windows') !== false) {
            $text = str_replace("\n", "\r\n", $text);
        }

        if($this->_codeblock == $INPUT->str('codeblock')) {
            header("Content-Type: text/plain; charset=utf-8");
            header("Content-Disposition: attachment; filename=$filename");
            header("X-Robots-Tag: noindex");
            echo trim($text, "\r\n");
            exit;
        }

        $this->_codeblock++;
    }

    /**
     * Wraps around code()
     *
     * @param string $text
     * @param string $language
     * @param string $filename
     */
    function file($text, $language = null, $filename = '') {
        $this->code($text, $language, $filename);
    }

    /**
     * This should never be reached, if it is send a 404
     */
    function document_end() {
        http_status(404);
        echo '404 - Not found';
        exit;
    }

    /**
     * Return the format of the renderer
     *
     * @returns string 'code'
     */
    function getFormat() {
        return 'code';
    }
}