Skip to content

Commit

Permalink
New Features: code line numbering, css supported box style, optional …
Browse files Browse the repository at this point in the history
…parameters

Signed-off-by: Taggic <taggic@t-online.de>
  • Loading branch information
Taggic committed Jan 18, 2012
1 parent e4a113f commit 2d14fd6
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 25 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2011-12-21
2012-01-18
3 changes: 2 additions & 1 deletion plugin.info.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

29 changes: 29 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/** * Design Elements - additional CSS for the xsSnipper plugin *
** @author Taggic <taggic@t-online.de> */
/* ----- 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;
}
84 changes: 61 additions & 23 deletions syntax.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
}
}
Expand All @@ -76,29 +83,60 @@ function handle($match, $state, $pos, &$handler) {
* @author Taggic <taggic@t-online.de>
*/
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'].'<br />';
}
// 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.'
<dl class="code">
<dt>
<a href="'.$xs_path.'&codeblock='.$this->_codeblock.'" title="Download Snippet" class="mediafile mf_'.$xssnipper['type'].'">'.$xssnipper['file'].'</a>
</dt>
<dd>'.$text.'</dd>
</dl>'.NL.NL;

// 4. output
$info = array();
$code_block = '<code ' . $xssnipper['type'] . '>' . $code_lines . '</code>';
$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++;

}
}
?>

0 comments on commit 2d14fd6

Please sign in to comment.