New Code Macro

Name New Code Macro
Vendor Atlassian Software Systems (Website)
Authors Agnes Ro, Mike Cannon-Brookes
Homepage http://confluence.atlassian.com/display/CONFEXT/New+Code+Macro
Issue Management http://developer.atlassian.com/jira/browse/NCODE
Categories Content Macros
Version 1.0.3
Availability Confluence v2.2 to v2.9-m5
State Beta
Support Unsupported Plugins
License Freeware / Open Source (BSD)
Price Free
Release Docs http://confluence.atlassian.com/display/CONFEXT/New+Code+Macro
Java API Docs n/a
Download Source n/a
Download JAR newcode-macro-plugin-1.0.3.jar

Outstanding Issues

Before installing the New Code Macro, please review the outstanding issues. We do not recommend the use of this new code macro if you are affected by any of the outstanding issues as shown above.

Description/Features

A new code macro that highlights more languages as well as nicer line numbering, printing and scrolling.
For live examples of the macro in action, see New Code Macro Examples!

Installation

  1. Disable the old "Code Macro" using the admin interface
  2. Install the plugin jar using the admin interface
  3. You're done!

Tested Environments (confluence vs plugin version)

v macro \ confluence > v2.1.x v2.2.x
v1.0

Usage

This macro is almost a complete drop in replacement for the existing code block macro bundled with Confluence.

Parameter Required Description
language no (default param) the language to highlight, see below for support
collapse no "true" will collapse the code fragment by default
linenumbers no "true" will show line numbers in the left gutter (default: false)
firstline no if showing line numbers, where to start numbering from (default 1)
controls no "true" will show controls at the top of the code fragment to copy and print (default: false)
macro body yes the code to format

The macros also has accompanying documentation in the Wiki Notation Guide.

Supported Languages

Currently these are the supported languages and the aliases for them:

  • C Sharp - c#, csharp, c-sharp
  • Delphi - delphi
  • HTML - html, xhtml
  • Java - java
  • Javascript - js, jscript, javascript
  • Pascal - pascal
  • PHP - php
  • Python - python, py
  • Shell scripts - shell
  • SQL - sql
  • VB - vb, vb.net
  • XML - xml

Adding New Languages

Adding new languages is very easy. If you open the plugin or look in Subversion you will see a series of Javascript files (ie shBrushCSharp.js) which have the settings for each language.

To add a new language:

  1. Create a new .js file for your language, including the aliases it should have
  2. Add your .js file to the atlassian-plugin.xml as a downloadable resource
  3. Add your .js file to the newcodemacro-jscss.vm file (which lists all the Javascript and CSS files to be sent the first time a code macro is used on a page)
  4. Tell us about it so we can include it here!

Version History

Version Comments Author
1.0
  • Initial release
Atlassian.com

Future Enhancements

  • Highlight attachments - it would be nice to be able to include attachments, highlighted, within a page (ie XML or Java snippets)
  • Show fragment of fragment - right now you can collapse the whole code, or show it all. It would be nice to be able to show the 'first 10 lines' by default or something

Labels

plugin plugin Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. May 31, 2006

    Mark Michaelis says:

    The examples for the new code macro really look nice, but: It seems as if unlik...

    The examples for the new code macro really look nice, but:

    It seems as if unlike the old code macro the rendered result is not put into divs. This will break the layout of many old pages. E. g. the following code:

    # Here my code:
    {code:none|title=somefile.txt}
    XYZ
    {code}
    

    will not be rendered anymore with the code-panel beeing on a second line but rather with a textfield just behind "code:" with the bottom line of the field aligned with the bottom line of the text.

    Please at least find a way to make legacy markup look nice. Otherwise enabling this plugin will surely break many carefully designed layouts.

    1. Jun 01, 2006

      Mike Cannon-Brookes says:

      Mark thanks for the feedback. We discovered this too, as well as a few bugs with...

      Mark - thanks for the feedback. We discovered this too, as well as a few bugs with the WYSIWYG editor and code macro combined. These are being patched (some live here already) and should be released today.

  2. Jun 03, 2006

    Ruediger Jungbeck says:

    The content of code:xml and code:c# are not included in pdf output

    The content of code:xml and code:c# are not included in pdf output

  3. Jun 14, 2006

    Glenn Stewart says:

    I completely agree with Mark above. I like many of the new features of the "New...

    I completely agree with Mark above.

    I like many of the new features of the "New Code Macro", but have reservations as to the lacking of features the original Code Macro included. For this reason I also wonder if the two can co-exist somehow.

    In my opinion, the feature that is lacking is the ability to include a title. This is very useful for documenting contents of files for example.

    The following code useable by the original, no longer works:

    # Here my code:
    {code:none|title=somefile.txt}
    XYZ
    {code}
    

    The result is as follows:

    somefile.txt
    XYZ
  4. Jun 23, 2006

    Andrew J Cosgriff says:

    Here's a very(!) basic Cisco config highlighter dp.sh.Brushes.Cisco = functio...

    Here's a very basic Cisco config highlighter

    Unable to find source-code formatter for language: js. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
    dp.sh.Brushes.Cisco = function()
    {
    	//
    	// 2006 by Andrew J Cosgriff, based on emacs font-lock keywords at http://www.emacswiki.org/cgi-bin/wiki/cisco-router-mode.el
    	//
    	var toplevel_keywords =	'access-list class-map controller crypto interface policy-map redundancy route-map router username';
    	var command_keywords = 'alias boot card diagnostic ^enable hostname logging service snmp-server version vtp';
    
    	this.regexList = [
    		{ regex: new RegExp('^!.*$', 'gm'),							css: 'comment' },			//  comments
    		{ regex: new RegExp('[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+', 'g'),				css: 'vars' },			// IP addresses
    		{ regex: new RegExp('no ', 'gm'),						css: 'preprocessor' },		// "no" something
    		{ regex: new RegExp(this.GetKeywords(toplevel_keywords), 'gm'),		css: 'keyword' },	// cisco toplevel keywords
    		{ regex: new RegExp(this.GetKeywords(command_keywords), 'gm'),		css: 'keyword' }		// cisco command keywords
    		];
    
    	this.CssClass = 'dp-c';
    }
    
    dp.sh.Brushes.Cisco.prototype	= new dp.sh.Highlighter();
    dp.sh.Brushes.Cisco.Aliases	= ['cisco'];
  5. Jul 14, 2006

    Agnes Ro says:

    (!) Note: this macro cannot be exported to PDF. This is a known bug, please use ...

    Note: this macro cannot be exported to PDF. This is a known bug, please use the original code macro if exports are required.

    Agnes.

    1. May 15, 2007

      Ulrich Trampe says:

      Here is a simple workaround for the PDF export bug, until it is fixed ;): Create...

      Here is a simple workaround for the PDF export bug, until it is fixed : Create a user macro newcode with the following content and use it instead of code.

      ##check the request URI (if available) for doexport or exportword
      #if(!($req.getRequestURI().length() > 0) || ($req.getRequestURI().indexOf("doexport") == -1 && $req.getRequestURI().indexOf("exportword")== -1))
          ##not exporting, render code macro
          #set($globalHelper=$action.getGlobalHelper())
          #set($renderer=$globalHelper.getWikiStyleRenderer())
          #set($context=$content.toPageContext())
          #if($param0)
              #set($newContent="{code:${param0}}${body}{code}")
          #else
              #set($newContent="{code}${body}{code}")
          #end
          #set($renderedContent=$renderer.convertWikiToXHtml($context, $newContent))
          ${renderedContent}
      #else
          ##exporting, render simple code panel
          ${body}
      #end
      
  6. Jul 18, 2006

    Thomas Wendel says:

    Nifty macro! The only drawback is: Long pieces of code do not work well with th...

    Nifty macro!

    The only drawback is: Long pieces of code do not work well with the macro. The browser pops up a warning:

    If you click continue, the browser might show the code in the macro. If the code is really long, it takes ages and many continue-clicks.

    Would be awesome if you could fix that problem!

    Cheers
    Thomas

  7. Jul 27, 2006

    David Ruddock says:

    We tries out v1.3 and it but the Rich Text editor lost all the carrage returns i...

    We tries out v1.3 and it but the Rich Text editor lost all the carrage returns in the code, making it hard to edit. (WikiMarkup was fine - but not if you looked at the Rich Text first and then clicked on wiki markup)

    1. Aug 08, 2006

      daren desjardins says:

      We are having the same issue. Any time you switch to the Rich editor, the macros...

      We are having the same issue. Any time you switch to the Rich editor, the macros looses all formatting and becomes a single line.

      1. Aug 09, 2006

        Ivan Benko says:

        Hi Daren and David, What version of Confluence are you using? Could you raise a...

        Hi Daren and David,

        What version of Confluence are you using? Could you raise a support issue please?

        Thanks,
        Ivan

        1. Sep 01, 2006

          David Ruddock says:

          We're on 2.2.5 There's an outstanding issue here:

          We're on 2.2.5

          There's an outstanding issue here: http://jira.atlassian.com/browse/CONF-6826

  8. Jul 28, 2006

    Tom Moore says:

    Thomas, that unresponsive script warning seems to be a bug with the way Firefox ...

    Thomas, that unresponsive script warning seems to be a bug with the way Firefox runs the javascript.  It seems to work fine in IE.  We've had to install IE Tab extension in firefox in order to use this, until the author can resolve this issue.

    1. Aug 02, 2006

      Thomas Wendel says:

      Tom, we also did experience this bug with IE or with the IE Tab extention for Fi...

      Tom, we also did experience this bug with IE or with the IE Tab extention for Firefox.

  9. Aug 02, 2006

    Tom Moore says:

    Oh ok.. Weird.. I stand corrected.  Seems to work fine for us in IE though.

    Oh ok.. Weird.. I stand corrected.  Seems to work fine for us in IE though.

  10. Aug 22, 2006

    David Aldrich says:

    I found that when I first inserted the new code macro and a code sample on a pag...

    I found that when I first inserted the new code macro and a code sample on a page and saved it, the code was displayed nicely. However, when I returned to edit the page, and viewed the Wiki markup and saved the page again, the code collapsed i.e. the line breaks were removed. Atlassian Support has recognised the problem and raised a bug:
    http://jira.atlassian.com/browse/CONF-6826

    Please vote for this issue to get it fixed.

  11. Aug 24, 2006

    Emily Stumpf says:

    Nobody's made a Perl one yet, have they? :)

    Nobody's made a Perl one yet, have they?

  12. Sep 09, 2006

    Andrew J Cosgriff says:

    I've done a bit more work on the Cisco syntax highlighting. a diff against 1.0....

    I've done a bit more work on the Cisco syntax highlighting.

    a diff against 1.0.3 and a jar file are available via http://polydistortion.net/monkey/archives/2006/09/10/003819.html

  13. Sep 25, 2006

    Guy Fraser says:

    It would be superuseful to have a "wiki" language file for the syntax highlighte...

    It would be super-useful to have a "wiki" language file for the syntax highlighter - there's loads of times when I've had to show chunks of wiki notation in user guides, etc., and having a wiki syntax would be a dream

  14. Oct 05, 2006

    Thomas Burkhart says:

    Hi, the plugin is great, but it would be even better if it would be possible to...

    Hi,

    the plugin is great, but it would be even better if it would be possible to display attached source files. Also a line range and not just a start would be great.

    If you expand a collapsed code fragment it should be possible to collapse them again.

    Best

    Thomas

  15. Oct 27, 2006

    Gary Pullis says:

    This would be a fantastic boon for my developers, but I'm concerned about using ...

    This would be a fantastic boon for my developers, but I'm concerned about using it until it's really stable. I think I'm going to go as far as to hold off until this moves from "New Code Macro" to "Standard Code Macro". When Atlassian trusts it enough to have it enabled, so will I.

  16. Nov 27, 2006

    Bob Swift says:

    Is it possible to get preview to show line numbers? The more preview mode looks ...

    Is it possible to get preview to show line numbers? The more preview mode looks like normal rendering the better.

  17. Dec 14, 2006

    John Price says:

    I just added a vote for fixing the bug that makes the RTF mode mess up code form...

    I just added a vote for fixing the bug that makes the RTF mode mess up code formatting. My team can't use this until that's fixed. Awesome macro though!

    1. Feb 23, 2007

      Pavlo Kasperskyi says:

      I am just wondering how this macro can have State = Stable ....

      I am just wondering how this macro can have State = Stable...

      1. Nov 07, 2007

        Matt Ryall says:

        You're right. There are a number of significant bugs with this macro, so I've ch...

        You're right. There are a number of significant bugs with this macro, so I've changed its state to 'beta'.

        The bugs will also be moved from Confluence's issue tracker to the new project for this plugin:

        http://developer.atlassian.com/jira/browse/NCODE

  18. Mar 05, 2007

    Joseph Kim says:

    I agree with Pavlo. I could only get broken  PDF format export files with t...

    I agree with Pavlo. I could only get broken  PDF format export files with this macro.

    I think this problem is about Unicode font support. 

    Plz change the state of this macro into "beta" something... and fix it first.

    otherwise many other confluence users could be in trouble like me...

  19. May 10, 2007

    Ric Parks says:

    Does anyone know if the issue with the rich test editor removing all the line br...

    Does anyone know if the issue with the rich test editor removing all the line breaks in code snippets has been resolved? Is there a work-around? This is an extremely, even fatal, problem for many of my existing pages.public void testThis {
    private static aTest;
    }
    Hm...looks like there's still a problem here, just a different one.

  20. May 15, 2007

    Ulrich Trampe says:

    Because of its unpredictable results and the loss of some features (title, borde...

    Because of its unpredictable results and the loss of some features (title, borderStyle, bgColor) the plugin should better not replace the original {code} macro. You can easily change the macros name by editing the atlassian-plugin.xml file within newcode-macro-plugin-1.0.3.jar.

    <macro name='newcode' class='com.atlassian.confluence.ext.code.NewCodeMacro' key='code'>

    After that, the macros name will be {newcode} and you can keep on using {code}.

    Hint for Windows Users: a jar is nothing else than a zip file, simply rename .jar to .zip and open it with windows explorer, rename it back to .jar before uploading it.

    1. May 15, 2007

      Ulrich Trampe says:

      In fact, I've changed the macros name to \ ... {{<macro name=' newcodeorg ' cl...

      In fact, I've changed the macros name to {newcodeorg}...

      <macro name='newcodeorg' class='com.atlassian.confluence.ext.code.NewCodeMacro' key='code'>

      ...and access the {newcodeorg} macro not directly, but only via another user macro, I called {newcode} (to have a workaround for the PDF export bug, as described above):

      ##check the request URI (if available) for doexport or exportword
      #set($globalHelper=$action.getGlobalHelper())
      #set($renderer=$globalHelper.getWikiStyleRenderer())
      #set($context=$content.toPageContext())
      #if(!($req.getRequestURI().length() > 0) || ($req.getRequestURI().indexOf("doexport") == -1 && $req.getRequestURI().indexOf("exportword")== -1))
          ##not exporting, render new code macro
          #if($param0)
              #set($newContent="{newcodeorg:${param0}}${body}{newcodeorg}")
          #else
              #set($newContent="{newcodeorg}${body}{newcodeorg}")
          #end
      #else
          ##exporting, render simple code panel
          #if($param0 && ($param0 == 'actionscript' || $param0 == 'html' || $param0 == 'java' || $param0 == 'javascript' || $param0 == 'none' || $param0 == 'sql' || $param0 == 'xhtml' || $param0 == 'xml'))
              #set($newContent="{code:${param0}}${body}{code}")
          #else
              #set($newContent="{code}${body}{code}")
          #end
      #end
      #set($renderedContent=$renderer.convertWikiToXHtml($context, $newContent))
      ${renderedContent}
      
  21. May 22, 2007

    Vinny Carpenter says:

    Hi there. I just checked out

    Hi there. I just checked out http://jira.atlassian.com/browse/CONF-6826 and it says it's closed but I am still having the same issue. I am using Confluence Wiki v2.4.5 (Build:#708 Apr 12, 2007) with v1.0.3 of the New Code Macro and every time I try and re-edit the page, all of the code becomes one line and scrolls off the screen. I've tried code, code:java and code:xml and all 3 do the same thing. Looks ok the first time but when I go back in to edit, all of the formatting is gone. All the Line breaks in new code macro are removed when edited inside WYSIWYG.
    Is there a fix as this bug makes the new code macro unusable. thanks

    --Vinny

    1. Jun 13, 2007

      Pavlo Kasperskyi says:

      This is because of:&nbsp; {}Resolution: Duplicate &nbsp;Original CONF6183

      This is because of: 

      Resolution: Duplicate

       Original CONF-6183is not fixed. Please vote.

  22. Jul 11

    Treg Anderson says:

    Hi, I am having a problem using the New Code Macro on IE 7.0.&nbsp; If I have on...

    Hi, I am having a problem using the New Code Macro on IE 7.0.  If I have one long line of text within the {code} tags the macro produces a scrollbar at the bottom of the code box but it covers up the single line of text. I am currently using IE 7.0 to enter this comment and when I shrink the width of the browser the scroll bar pops up over the text on the example below.

    This is some text that could cause a horizantal scroll bar with a narrow enough window

    With the window wide enough the scroll bar goes away and the text can be seen.

    Is this a bug that is known and is in the process of being fixed? I looked for the source code to take a look at it here but there doesn't seem to be any to download.

  23. yesterday at 17:49:15

    Crystal Johnson says:

    We have 2.8 installed and are trying to use C# with the code macro. \\ I notice ...

    We have 2.8 installed and are trying to use C# with the code macro.

    I notice it is one of the supported languages, but come up with an error.

    Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml

    Please help.

    Thanks,

    Crystal Johnson