diff --git a/VERSION b/VERSION index 3cf475d..6d50c67 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2011-12-21 \ No newline at end of file +2012-01-18 \ No newline at end of file diff --git a/plugin.info.txt b/plugin.info.txt index 55fcd97..2714b64 100644 --- a/plugin.info.txt +++ b/plugin.info.txt @@ -3,7 +3,8 @@ base xssnipper author Taggic email taggic@t-online.de -date 2011-12-21 +date 2012-01-18 name xssnipper desc provides syntax plugin to retrieve code snippeds from files url http://www.dokuwiki.org/plugin:mysnipper + diff --git a/style.css b/style.css new file mode 100644 index 0000000..86f9e8e --- /dev/null +++ b/style.css @@ -0,0 +1,29 @@ +/** * Design Elements - additional CSS for the xsSnipper plugin * + ** @author Taggic */ +/* ----- General Elements ---------- */ + + div.xssnipper { + border: 1px dashed #a0a0a0; + background: #f0f0f0; + } + + div.xssnipper ol { + background: #f0f0f0; + margin:0px; + padding:10px 0 5px 0; + font: italic 1em Calibri, Times, serif; + color: #a0a0a0 !important; + } + + div.xssnipper li { + background: #ffffff; + margin-left:0px; + padding-left:20px; + } + + div.xssnipper p { + font: normal .8em Arial, Helvetica, sans-serif; + color: #000000; + padding:5px 0; + margin:0px; + } \ No newline at end of file diff --git a/syntax.php b/syntax.php index e3c1785..1483909 100644 --- a/syntax.php +++ b/syntax.php @@ -12,13 +12,17 @@ if(!defined('DOKU_DATA')) define('DOKU_DATA',DOKU_INC.'data/pages/'); require_once(DOKU_PLUGIN.'syntax.php'); require_once(DOKU_INC.'inc/parser/xhtml.php'); +require_once(DOKU_INC.'inc/parser/renderer.php'); + + +include_once(DOKU_INC.'inc/geshi.php'); /** * All DokuWiki plugins to extend the parser/rendering mechanism * need to inherit from this class */ class syntax_plugin_xssnipper extends DokuWiki_Syntax_Plugin { - + /******************************************************************************/ /* return some info */ @@ -34,8 +38,8 @@ function getSort(){ return 999;} /* Connect pattern to lexer */ function connectTo($mode){ - $this->Lexer->addSpecialPattern('\{\(xssnipper>[^}]*\)\}',$mode,'plugin_xssnipper'); - } + $this->Lexer->addSpecialPattern('\{\(xssnipper\>.*?\)\}',$mode,'plugin_xssnipper'); + } /******************************************************************************/ /* handle the match @@ -49,10 +53,11 @@ function handle($match, $state, $pos, &$handler) { /******************************************************************************/ /* parameters can be: {(xssnipper>[file path],[from line],[to line],[type])} - [file path] ... path to the file (either it is in DokuWiki media directory or windows fileshare, etc.) - [from line] ... the first line, which should be displayed - [to line] ... the last line, which should be displayed - [type] ... the type of content to tell the renderer how to interprete and set colors + [file path] ... path to the file (either it is in DokuWiki media directory or windows fileshare, etc.) + [from line] ... the first line, which should be displayed + [to line] ... the last line, which should be displayed + [type] [file] ... the type of content to tell the syntax higlighter how to interprete and set colors + and pass a file for download the code block /******************************************************************************/ $params = explode(",",$match); // if you will have more parameters and choose ',' to delim them @@ -67,7 +72,9 @@ function handle($match, $state, $pos, &$handler) { $xssnipper['filepath'] = $params[0]; $xssnipper['from'] = $params[1]; $xssnipper['until'] = $params[2]; - $xssnipper['type'] = $params[3]; + $alpha = explode(' ',$params[3]); + $xssnipper['type'] = $alpha[0]; + $xssnipper['file'] = $alpha[1]; return $xssnipper; } } @@ -76,29 +83,60 @@ function handle($match, $state, $pos, &$handler) { * @author Taggic */ function render($mode, &$renderer, $xssnipper) { + global $ID; + + if(!$xssnipper['type']) $xssnipper['type']='txt'; + if(!$xssnipper['file']) $xssnipper['file']= basename($xssnipper['filepath']); + if(!$xssnipper['file']) $xssnipper['file']='snippet.'.$xssnipper['type']; + if(!$simplesnipper['until']) $simplesnipper['until'] = count($records); + if($this->_codeblock<1) $this->_codeblock=1; + // 1. check if $xssnipper['filepath'] exist, else error message - // a) the file is in media directory and path is a relative one - if(!file_exists($xssnipper['filepath'])) { - msg('file not found',-1); - echo $xssnipper['filepath'].'
'; - } - // b) file is stored somwhere else and full qualified path is necessary + if(!file_exists($xssnipper['filepath'])) { + msg('file '.$xssnipper['filepath'].' not found',-1); + return false; + } // 2. open the file in read mode - - $records = file($xssnipper['filepath']); - + $records = file($xssnipper['filepath']); + // 3. load the file content from line = $xssnipper['from'] , to line = $xssnipper['until'] into $code_lines + if(!$xssnipper['until']) $xssnipper['until']=count($records); foreach ($records as $line_num => $line) { - if(($line_num>=$xssnipper['from']) && ($line_num<=$xssnipper['until'])) - $code_lines .= $line; + if(($line_num>=$xssnipper['from']-1) && ($line_num<=$xssnipper['until']-2)) + $code_lines .= $line; + if ($line_num>$xssnipper['until']) break; } + + $geshi = new GeSHi($code_lines, $xssnipper['type']); + $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); + $geshi->set_overall_class('xssnipper'); + $geshi->set_header_type(GESHI_HEADER_DIV); + $geshi->start_line_numbers_at($xssnipper['from']); + + $xs_path = '?do=export_code&id='.$ID; + $text = $geshi->parse_code(); + + $code_block .= NL.NL.' +
+
+ '.$xssnipper['file'].' +
+
'.$text.'
+
'.NL.NL; - // 4. output - $info = array(); - $code_block = '' . $code_lines . ''; - $code_block = p_render('xhtml',p_get_instructions($code_block),$info); $renderer->doc .= $code_block; + + if($this->_codeblock == $_REQUEST['codeblock']){ + header("Content-Type: text/plain; charset=utf-8"); + header("Content-Disposition: attachment; filename=".trim($xssnipper['file'])); + header("X-Robots-Tag: noindex"); + header("Pragma: public"); + echo trim($code_lines,"\r\n"); + exit; + } + $this->_codeblock++; + } } ?> \ No newline at end of file