Snippet Macro Library V1

What is it?

A Confluence macro to retrieve and display a snippet of text from a remote URL (e.g., ViewCVS).
The content will be trimmed down to the content found between some special tags. This is very handy for documentation that includes code. The code can be in CVS (whatever build system is used will make sure it builds properly) and the URL can be a ViewCVS URL. (In case of ViewCVS the URL should point to the download URL of a file). The content is cached for one hour in order to improve performance. The snippet is also unindented to remove excess space.

This can ensure that code examples in your documentation are kept up-to-date since they are sourced from the original code, thus preserving ideas put forth in LiterateProgramming.

The original version was spawned at codehaus (written by those two legendary maestros of code - Jon Tirsen and Aslak Hellesoy) and the code for it still lives there. This is a page that makes heavy use of snippet.

This version is simply updated to work with the macro management present in Confluence 1.1+. A bug was also fixed where snippets with blank lines had messed up indentation.

Download

macros-snippet-1.0.jar - for Confluence 1.4.x
macros-snippet-0.4.jar - for Confluence 1.3.x
macros-snippet-0.3.jar

Source

macros-snippet-1.0-src.zip
macros-snippet-0.3-src.tar.gz

Usage


{snippet:id=<snippetId>|url=<sourceURL>|lang=<language>|linenumbers=<true|false>}



Parameters

  • id (required) - the id of the snippet in the URL content
  • url (required) - the location of the content
  • lang (optional) - if present, will wrap the content in {code:lang}
  • linenumbers (optional) - if present and true, will add line numbers

Example

URL/snippet content


this
// START SNIPPET: something
macro
// START SNIPPET: somethingElse
is
// END SNIPPET: somethingElse
very
// END SNIPPET: something
cool



Macro usage


{snippet:lang=java|id=something|linenumbers=true|url=http://some/url/to/viewcvs}



This will give the following content:

{code:java}
1. macro
2. is
3. very
{code}



Labels

confluence14 confluence14 Delete
plugin plugin Delete
macro macro Delete
confluence13 confluence13 Delete
repository_pending repository_pending Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Sep 04, 2004

    Anonymous says:

    If you're pointing to a Fisheye repository you can now specify revision to HEAD ...

    If you're pointing to a Fisheye repository you can now specify revision to HEAD Example:

    {snippet:lang=java|id=class|url=http://cvs.picocontainer.codehaus.org/viewrep/~raw,r=HEAD/picocontainer/java/picocontainer/src/test/org/picocontainer/doc/introduction/Peelable.java}

    I discovered this here:
    http://www.cenqua.com/forums/thread.jspa?forumID=12&threadID=782&messageID=5451#5451

    --Aslak

  2. Nov 15, 2005

    Bob Swift says:

    {snippet:id=something|linenumbers=true|url=http://confluence.atlassian.com/displ...

    {snippet:id=something|linenumbers=true|url=http://confluence.atlassian.com/display/CONFEXT/Snippet+Macro+Library }
    gives the following:

    1. macro
    2. is
    

    instead of the expected

    1. macro
    2. is 
    3. very 
    
  3. Nov 16, 2005

    Guy Fraser says:

    Simple user macro to put snippet tags in to your confluence pages: start-snippe...

    Simple user macro to put snippet tags in to your confluence pages:

    start-snippet user macro:

    // START SNIPPET: ${param0}

    end-snippet user macro:

    // END SNIPPET: ${param0}

    Benefit of having 2 seperate macros rather than one macro with body: nestable

    Obviously, you'll end up with paragraphs getting inserted, but it should work. If someone has time on their hands, it would be worth adding a couple of new macros to the snippet plugin - think of it as allowing a very flexible {excerpt-include} or {include} type of functionality

  4. Mar 04, 2006

    Vincent Massol says:

    I've been spending several hours trying to figure out why it wasn't working for ...

    I've been spending several hours trying to figure out why it wasn't working for me... After long hours, I've found the problem: the snippet id must NOT contain the word "start" (and probably not "end" too).

    The id I was trying to use was "quickstart-xxx"...