Snippet Macro

Last modified by Andreea Popescu on 2021/03/17 21:50

cogExtracts a portion of code delimited by a token.
TypeDoc (Velocity Macro)
Category
Developed by

XWiki Development Team

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Description

Extracts a portion of code delimited by a token. This macro mimicks the behavior of the Confluence Snippet macro.

Usage

#snippet(lang, id, url)

Parameters definition

NameOptionalAllowed valuesDefault valueDescription
langnoStringnoneLanguage to be used when wrapping the content in the {code} macro
idnoStringnoneThe token id. Tokens are of the form // START SNIPPET: / // END SNIPPET:
urlnoStringnoneThe URL from where to get the content to display

Example

#snippet("java", "boy", "http://svn.codehaus.org/picocontainer/java/1.x/picocontainer/trunk/container/src/test/org/picocontainer/doc/tutorial/simple/Boy.java")

Result

result.png

Installation

This a Velocity macro. Since it's not bundled by default you'll have to copy/paste the following code to use it:
#macro(snippet $lang $id $url)
#set($codestart = "{CODE:$lang}")
$codestart.toLowerCase()
#set ($text = $xwiki.getURLContent($url))
#set ($delimiter = "// START SNIPPET: $id")
#set ($pos1 = $xwiki.add($text.indexOf($delimiter), $delimiter.length()))
#set ($pos2 = $text.indexOf("// END SNIPPET: $id"))
$text.substring($pos1, $pos2)
#set($codeend = "{CODE}")
$codeend.toLowerCase()
#end
Tags:
    

Get Connected