Code2 Plugin

Informazioni generali

Questo plugin rimpiazza quello già presente nella dokuwiki consentendo di visualizzare i numeri di riga e la possibilita di aggiungere una riga di intestazione ed una di chiusura. Include anche l'evidenziazione per la sintassi Diff.

Sito: http://wiki.splitbrain.org/plugin:code2

Sintassi

<code lang 123 |[fh] text |[sh]>
{code to highlight}
</code>
  • lang” (if present) specifies the given code's (programming-) language;
  • 123” (if present) specifies the first number to use when numbering the given code's lines;
  • the first “|” (if present) delimits the first argument(s) from the remaining ones which are used to produce a header or footer above/below the high­ligh­ted code;
  • a “h” (for “header”)1) following immediately the pipe cha­rac­ter cau­ses the remaining “text” to get placed above the code, a “f” (for “footer”)2) places the “text” below the code (note that this flag is optional; if omitted it de­faults to “f”);
  • the “text” is used as-is with the additional benefit of get­ting wrapped by an anchor tag which allows for addressing the code from other pages.
  • the second “|” (if present) delimits the header/footer text;
  • a “s3) or “h4) character (if present) controls the code block's initial folding state; for more details see the Behaviour section below.
  • the “{code to highlight}” is either the plain text to process or a pointer to an external source.

Examples

Please note that you will not see any highlighting here: This section just shows some use cases with­out actually triggering the activation of the plugin – even if it was installed …

<code>
some text
and more
</code>

This just renders the given preformatted text without any spe­cial high­ligh­ting. However,

<code 1>
some text
and more
</code>

will add line numbers in front of each line starting (in this case) with 1 (one).

<code JavaScript 12|Listing 2>
var de = function() {
	return (typeof(window.de) == 'object') ? window.de : {};
}();
</code>

This markup will turn on both the JavaScript syntax highlighting and the line num­be­ring star­ting in this case with num­ber 12 and place the text “Listing 2” below the code block – pro­du­cing HTML like:

<div class="code">
  <pre class="code javascript">
  <span class="lno">12:</span>  <span class="kw2">var</span> de = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span class="lno">13:</span>      <span class="kw1">return</span> <span class="br0">&#40;</span><span class="kw1">typeof</span><span class="br0">&#40;</span>window.<span class="me1">de</span><span class="br0">&#41;</span> == <span class="st0">'object'</span><span class="br0">&#41;</span> ? window.<span class="me1">de</span> : <span class="br0">&#123;</span><span class="br0">&#125;</span>;
  <span class="lno">14:</span>  <span class="br0">&#125;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
  </pre>
  <p class="codefoot"><a name="listing_2">Listing 2</a></p>
</div>

If you'd rather the text above the code you'd just insert a “h” right after the pipe character i.e. “<code javascript 12|h Listing 2>”.

Of course, instead of javascript you may use any other highlighting mode sup­por­ted by GeSHi like “html” for exam­ple or “php”. In case a lan­gua­ge is gi­ven which is not sup­por­ted by GeSHi there won't be any syn­tax high­ligh­ting, of course, but the line num­bers (if re­que­sted) and hea­der/foo­ter li­nes (if re­que­sted) will ap­pear ne­ver­the­less. As men­tio­ned abo­ve be­si­des the line num­be­ring fea­tu­re this plugin pro­vi­des an im­pro­ved high­ligh­ting mode for “diff” files (aka “pat­ches”).

For “diff” the second argument can be either u (for “unified”), c (for “con­text”), n (or r for RCS) or s (for “simple”) format5). Although ap­pre­ciated this ar­gu­ment is optional as well. Omitting it will cause the plugin to per­form some ad­di­tio­nal tests to figure out the “diff” for­mat ac­tu­ally used. A little sample:

<code diff>
        --- syntax_plugin_code.php.orig	2007-02-23 11:33:35.000000000 +0100
        +++ syntax_plugin_code.php	2007-02-23 11:53:17.000000000 +0100
        @@ -595,14 +595,14 @@
         				$n = (int)$hits[5];
         			} // if
         			if (isset($hits[7]) && ($hits[7])) {
        -				$hits[6] = (isset($hits[6])) ? strtolower($hits[6]) . 'f' : 'f';
        +				$hits[6] = (isset($hits[6])) ? strtolower($hits[6]) . 'h' : 'h';
         				switch ($hits[6]{0}) {
        -					case 'h':
        -					case 't':
        -						$ht = trim($hits[7]);
        +					case 'b':
        +					case 'f':
        +						$ft = trim($hits[7]);
         						break;
         					default:
        -						$ft = trim($hits[7]);
        +						$ht = trim($hits[7]);
         						break;
         				} // switch
         				if (isset($hits[8])) {
</code>

To summarize:

  • Without the new arguments nothing changes from what you're used to know (and get) – but see the Notes section below.
  • Giving a numeric argument only results in preformatted text without high­ligh­ting but line numbering turned on.
  • Giving just a language argument turns on syntax highlighting w/o line num­bers.
  • Giving a language and a numeric argument turns on syntax highlighting and line numbering starting with the given number.
  • Adding a “|”, optionally followed by “h” or “f” and some text will result in a header or footer line attached to the code block (see Behaviour below).
1)
or “t” for “top”
2)
or “b” for “bottom”
3)
for “show”, the default
4)
for “hide”
5)
Meaning, that line numbering is not available for “diff”. It wouldn't make any sense any­way. Just think about it.
dokuwiki/plugins/code2.txt · Ultima modifica: 2015/06/08 20:15 da 127.0.0.1
Torna su
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0