summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--inc/lang/en/lang.php1
-rw-r--r--inc/parser/code.php58
-rw-r--r--inc/parser/handler.php46
-rw-r--r--inc/parser/parser.php2
-rw-r--r--inc/parser/renderer.php6
-rw-r--r--inc/parser/xhtml.php47
-rw-r--r--lib/tpl/default/design.css41
7 files changed, 159 insertions, 42 deletions
diff --git a/inc/lang/en/lang.php b/inc/lang/en/lang.php
index d043350d8..524e976e5 100644
--- a/inc/lang/en/lang.php
+++ b/inc/lang/en/lang.php
@@ -150,6 +150,7 @@ $lang['restored'] = 'old revision restored';
$lang['external_edit'] = 'external edit';
$lang['summary'] = 'Edit summary';
$lang['noflash'] = 'The <a href="http://www.adobe.com/products/flashplayer/">Adobe Flash Plugin</a> is needed to display this content.';
+$lang['download'] = 'Download Snippet';
$lang['mail_newpage'] = 'page added:';
$lang['mail_changed'] = 'page changed:';
diff --git a/inc/parser/code.php b/inc/parser/code.php
new file mode 100644
index 000000000..d2bcf7d42
--- /dev/null
+++ b/inc/parser/code.php
@@ -0,0 +1,58 @@
+<?php
+/**
+ * A simple renderer that allows downloading of code and file snippets
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+if(!defined('DOKU_INC')) die('meh.');
+require_once DOKU_INC . 'inc/parser/renderer.php';
+
+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.
+ */
+ function code($text, $language = NULL, $filename='' ) {
+ if(!$language) $language = 'txt';
+ if(!$filename) $filename = 'snippet.'.$language;
+ $filename = basename($filename);
+
+ if($this->_codeblock == $_REQUEST['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()
+ */
+ function file($text) {
+ $this->code($text);
+ }
+
+ /**
+ * This should never be reached, if it is send a 404
+ */
+ function document_end() {
+ header("HTTP/1.0 404 Not Found");
+ echo '404 - Not found';
+ exit;
+ }
+
+ /**
+ * Return the format of the renderer
+ *
+ * @returns string 'code'
+ */
+ function getFormat(){
+ return 'code';
+ }
+}
diff --git a/inc/parser/handler.php b/inc/parser/handler.php
index 83f837b70..58b68bc42 100644
--- a/inc/parser/handler.php
+++ b/inc/parser/handler.php
@@ -323,13 +323,6 @@ class Doku_Handler {
return true;
}
- function file($match, $state, $pos) {
- if ( $state == DOKU_LEXER_UNMATCHED ) {
- $this->_addCall('file',array($match), $pos);
- }
- return true;
- }
-
function quote($match, $state, $pos) {
switch ( $state ) {
@@ -360,23 +353,28 @@ class Doku_Handler {
return true;
}
- function code($match, $state, $pos) {
- switch ( $state ) {
- case DOKU_LEXER_UNMATCHED:
- $matches = explode('>',$match,2);
- $matches[0] = trim($matches[0]);
- if ( trim($matches[0]) == '' ) {
- $matches[0] = NULL;
- }
- # $matches[0] contains name of programming language
- # if available, We shortcut html here.
- if($matches[0] == 'html') $matches[0] = 'html4strict';
- $this->_addCall(
- 'code',
- array($matches[1],$matches[0]),
- $pos
- );
- break;
+ function file($match, $state, $pos) {
+ return $this->code($match, $state, $pos, 'file');
+ }
+
+ function code($match, $state, $pos, $type='code') {
+ if ( $state == DOKU_LEXER_UNMATCHED ) {
+ $matches = explode('>',$match,2);
+ $matches[0] = trim($matches[0]);
+
+ list($language,$filename) = explode(' ',$matches[0],2);
+ $language = trim($language);
+ $filename = trim($filename);
+ if ( $language == '' ) $language = null;
+ if ( $language == '-' ) $language = null;
+ if ( $filename == '' ) $filename = null;
+ # We shortcut html here.
+ if($language == 'html') $language = 'html4strict';
+ $this->_addCall(
+ $type,
+ array($matches[1],$language,$filename),
+ $pos
+ );
}
return true;
}
diff --git a/inc/parser/parser.php b/inc/parser/parser.php
index 491206b81..773d9d4d5 100644
--- a/inc/parser/parser.php
+++ b/inc/parser/parser.php
@@ -561,7 +561,7 @@ class Doku_Parser_Mode_code extends Doku_Parser_Mode {
class Doku_Parser_Mode_file extends Doku_Parser_Mode {
function connectTo($mode) {
- $this->Lexer->addEntryPattern('<file>(?=.*</file>)',$mode,'file');
+ $this->Lexer->addEntryPattern('<file(?=.*</file>)',$mode,'file');
}
function postConnect() {
diff --git a/inc/parser/renderer.php b/inc/parser/renderer.php
index bd2b5043a..94aa5c67b 100644
--- a/inc/parser/renderer.php
+++ b/inc/parser/renderer.php
@@ -161,13 +161,13 @@ class Doku_Renderer extends DokuWiki_Plugin {
function preformatted($text) {}
- function file($text) {}
-
function quote_open() {}
function quote_close() {}
- function code($text, $lang = NULL) {}
+ function file($text, $lang = null, $file = null ) {}
+
+ function code($text, $lang = null, $file = null ) {}
function acronym($acronym) {}
diff --git a/inc/parser/xhtml.php b/inc/parser/xhtml.php
index 5a1c1893b..3137281e7 100644
--- a/inc/parser/xhtml.php
+++ b/inc/parser/xhtml.php
@@ -36,7 +36,8 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
var $node = array(0,0,0,0,0);
var $store = '';
- var $_counter = array(); // used as global counter, introduced for table classes
+ var $_counter = array(); // used as global counter, introduced for table classes
+ var $_codeblock = 0; // counts the code and file blocks, used to provide download links
function getFormat(){
return 'xhtml';
@@ -358,11 +359,11 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
}
function preformatted($text) {
- $this->doc .= '<pre class="code">' . trim($this->_xmlEntities($text),"\n\r") . '</pre>'. DOKU_LF;
+ $this->doc .= '<pre class="code">' . trim($this->_xmlEntities($text)) . '</pre>'. DOKU_LF;
}
function file($text) {
- $this->doc .= '<pre class="file">' . trim($this->_xmlEntities($text),"\n\r"). '</pre>'. DOKU_LF;
+ $this->doc .= '<pre class="file">' . trim($this->_xmlEntities($text)). '</pre>'. DOKU_LF;
}
function quote_open() {
@@ -373,21 +374,49 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
$this->doc .= '</div></blockquote>'.DOKU_LF;
}
+ function preformatted($text) {
+ $this->doc .= '<pre class="'.$class.'">' . trim($this->_xmlEntities($text)) . '</pre>'. DOKU_LF;
+ }
+
+ function file($text, $language=null, $filename=null) {
+ $this->_highlight('file',$text,$language,$filename);
+ }
+
+ function code($text, $language=null, $filename=null) {
+ $this->_highlight('code',$text,$language,$filename);
+ }
+
/**
- * Callback for code text
- *
- * Uses GeSHi to highlight language syntax
+ * Use GeSHi to highlight language syntax in code and file blocks
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
- function code($text, $language = NULL) {
+ function _highlight($type, $text, $language=null, $filename=null) {
global $conf;
+ global $ID;
+ global $lang;
+
+ if($filename){
+ $this->doc .= '<dl class="'.$type.'">'.DOKU_LF;
+ $this->doc .= '<dt><a href="'.exportlink($ID,'code',array('codeblock'=>$this->_codeblock)).'" title="'.$lang['download'].'">';
+ $this->doc .= hsc($filename);
+ $this->doc .= '</a></dt>'.DOKU_LF.'<dd>';
+ }
if ( is_null($language) ) {
- $this->preformatted($text);
+ $this->doc .= '<pre class="'.$type.'">'.$this->_xmlEntities($text).'</pre>'.DOKU_LF;
} else {
- $this->doc .= p_xhtml_cached_geshi($text, $language);
+ $class = 'code'; //we always need the code class to make the syntax highlighting apply
+ if($type != 'code') $class .= ' '.$type;
+
+ $this->doc .= "<pre class=\"$class $language\">".p_xhtml_cached_geshi($text, $language, '').'</pre>'.DOKU_LF;
+ }
+
+ if($filename){
+ $this->doc .= '</dd></dl>'.DOKU_LF;
}
+
+ $this->_codeblock++;
}
function acronym($acronym) {
diff --git a/lib/tpl/default/design.css b/lib/tpl/default/design.css
index 63c86d36b..c3591fc67 100644
--- a/lib/tpl/default/design.css
+++ b/lib/tpl/default/design.css
@@ -481,16 +481,47 @@ div.dokuwiki pre.code {
background-color: __background_other__;
}
-/* inline code words */
-div.dokuwiki code {
- font-size: 120%;
-}
-
/* code blocks by file tag */
div.dokuwiki pre.file {
background-color: __background_alt__;
}
+/* filenames for file and code blocks */
+div.dokuwiki dl.file,
+div.dokuwiki dl.code {
+ margin-top: 2em;
+ margin-bottom: 2.5em;
+}
+
+div.dokuwiki dl.file dt,
+div.dokuwiki dl.code dt {
+ border: 1px dashed __border__;
+ display: inline;
+ padding: 0.1em 1em;
+ margin-left: 2em;
+}
+
+div.dokuwiki dl.code dt a,
+div.dokuwiki dl.file dt a {
+ color: __text__;
+}
+
+div.dokuwiki dl.code dt {
+ background-color: __background_other__;
+ border-bottom: 1px solid __background_other__;
+}
+
+div.dokuwiki dl.file dt {
+ background-color: __background_alt__;
+ border-bottom: 1px solid __background_alt__;
+}
+
+
+/* inline code words */
+div.dokuwiki code {
+ font-size: 120%;
+}
+
/* inline tables */
div.dokuwiki table.inline {
background-color: __background__;