Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Syntax Highlighting for Confluence

Tags:

r

confluence

Is there a way to get R syntax highlighting in confluence? I've tried General configuration > configure code macro > add new language, but I have no clue how to upload a custom brush syntax for R..has anyone already done this or is there a way I can get it for R ?

like image 726
WeakLearner Avatar asked Apr 28 '16 06:04

WeakLearner


1 Answers

For adding a syntax-highlighting "brush" javascript file I found the following process to work nicely.

get yourself the latest Syntaxhighlighter from: http://alexgorbatchev.com/SyntaxHighlighter/

e.g. 3.0.83 as of 2016-05

unpack it and create an index.html file that uses it like:

<!DOCTYPE html>
<html lang="de">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<!-- Include required JS files -->
<script type="text/javascript" src="js/shCore.js"></script>

<!--
    At least one brush, here we choose JS. You need to include a brush for every 
    language you want to highlight
-->
<script type="text/javascript" src="css/shBrushRule.js"></script>

<!-- Include *at least* the core style and default theme -->
<link href="css/shCore.css" rel="stylesheet" type="text/css" />
<link href="css/shThemeDefault.css" rel="stylesheet" type="text/css" />
</head>
<body> 
<!-- You also need to add some content to highlight, but that is covered elsewhere. -->
<pre class="brush: R">
add your R code here
</pre>

<!-- Finally, to actually run the highlighter, you need to include this JS on your page -->
<script type="text/javascript">
     SyntaxHighlighter.all()
</script>
</body>
</html>

in the

<pre></pre>

you might want to add some R code. To create an R brush you might want to go from some of the brushes in the css folder:

shBrushAS3.js         shBrushDelphi.js      shBrushPerl.js        shBrushSass.js
shBrushAppleScript.js shBrushDiff.js        shBrushPhp.js         shBrushScala.js shBrushBash.js        shBrushErlang.js      shBrushPlain.js       shBrushSql.js
shBrushCSharp.js      shBrushGroovy.js      shBrushPowerShell.js  shBrushTcl.js
shBrushColdFusion.js  shBrushJScript.js     shBrushPython.js      shBrushVb.js
shBrushCpp.js         shBrushJava.js        shBrushRuby.js        shBrushXml.js
shBrushCss.js         shBrushJavaFX.js      shBrushRule.js

it just needs a few lines of code and regular expressions to setup the keywords and comment, variable and other rules. When you have created (or searched for it on the internet e.g https://gist.github.com/yihui/1804862) your shBrushR.js file and are happy with it you can upload it to confluence via General configuration > configure code macro > add new language (and hopefully add the resulting js file to this answer to make this a fully complete solution - sorry I don't know R myself so I can't help much with this part)

See the below R brush (taken from the link above) as an example:

 /**
 *  Author: Yihui Xie
 *  URL: http://yihui.name/en/2010/09/syntaxhighlighter-brush-for-the-r-language
 *  License: GPL-2 | GPL-3
 */
SyntaxHighlighter.brushes.R = function()
{
    var keywords = 'if else repeat while function for in next break TRUE FALSE NULL Inf NaN NA NA_integer_ NA_real_ NA_complex_ NA_character_';
    var constants = 'LETTERS letters month.abb month.name pi';
    this.regexList = [
    { regex: SyntaxHighlighter.regexLib.singleLinePerlComments, css: 'comments' },
    { regex: SyntaxHighlighter.regexLib.singleQuotedString,     css: 'string' },
    { regex: SyntaxHighlighter.regexLib.doubleQuotedString,     css: 'string' },
    { regex: new RegExp(this.getKeywords(keywords), 'gm'),      css: 'keyword' },
    { regex: new RegExp(this.getKeywords(constants), 'gm'),     css: 'constants' },
    { regex: /[\w._]+[ \t]*(?=\()/gm,               css: 'functions' },
    ];
};
SyntaxHighlighter.brushes.R.prototype   = new SyntaxHighlighter.Highlighter();
SyntaxHighlighter.brushes.R.aliases = ['r', 's', 'splus'];
like image 147
Wolfgang Fahl Avatar answered Sep 27 '22 23:09

Wolfgang Fahl