Skip to content

Commit

Permalink
initial version of xsSnipper
Browse files Browse the repository at this point in the history
Signed-off-by: Taggic <taggic@t-online.de>
  • Loading branch information
Taggic committed Jan 15, 2012
0 parents commit e4a113f
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2011-12-21
9 changes: 9 additions & 0 deletions plugin.info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# PLUGIN INFO - DO NOT EDIT!

base xssnipper
author Taggic
email taggic@t-online.de
date 2011-12-21
name xssnipper
desc provides syntax plugin to retrieve code snippeds from files
url http://www.dokuwiki.org/plugin:mysnipper
104 changes: 104 additions & 0 deletions syntax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/**
* Plugin xssnipper: provides rendered code snippeds from files to be displayed in a code block
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Taggic <taggic@t-online.de>
*/

if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
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');

/**
* 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
*/
function getInfo(){
return confToHash(dirname(__FILE__).'/plugin.info.txt');
}

function getType(){ return 'substition';}
function getPType(){ return 'block';}
function getSort(){ return 999;}

/******************************************************************************/
/* Connect pattern to lexer
*/
function connectTo($mode){
$this->Lexer->addSpecialPattern('\{\(xssnipper>[^}]*\)\}',$mode,'plugin_xssnipper');
}

/******************************************************************************/
/* handle the match
*/
function handle($match, $state, $pos, &$handler) {
global $ID;
$match = substr($match,strlen('{(xssnipper>'),-2); //strip markup from start and end

//handle params
$data = array();
/******************************************************************************/
/* 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
/******************************************************************************/

$params = explode(",",$match); // if you will have more parameters and choose ',' to delim them


if (!$params) {
msg('Syntax of xssnipper detected but an unknown parameter was attached.', -1);
}
else {
// Values
$xssnipper = array();
$xssnipper['filepath'] = $params[0];
$xssnipper['from'] = $params[1];
$xssnipper['until'] = $params[2];
$xssnipper['type'] = $params[3];
return $xssnipper;
}
}
/******************************************************************************/
/* render output
* @author Taggic <taggic@t-online.de>
*/
function render($mode, &$renderer, $xssnipper) {
// 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

// 2. open the file in read mode

$records = file($xssnipper['filepath']);

// 3. load the file content from line = $xssnipper['from'] , to line = $xssnipper['until'] into $code_lines
foreach ($records as $line_num => $line) {
if(($line_num>=$xssnipper['from']) && ($line_num<=$xssnipper['until']))
$code_lines .= $line;
}

// 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;
}
}
?>

0 comments on commit e4a113f

Please sign in to comment.