SyntaxHighlighter Brush for the R Language
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
shBrushR.js (1Kb)
/** * 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'];
Hopefully Tal can persuade the WordPress.com manager to add support for R syntax highlighting, so these users do not need to worry much about the installation and configuration. For WordPress users, there are a couple of choices to make use of SyntaxHighlighter, e.g. the SyntaxHighlighter Evolved plugin, but I find this plugin somehow out-dated because it is still using an old version of SyntaxHighlighter, and it looks not easy to add support for new languages. Therefore I decided not to use any WordPress plugins, but to manually add the necessary HTML code in my header and footer; this approach works for any HTML pages.
You need to upload the latest version (3.0.83) of SyntaxHighlighter somewhere first (of course, with the R brush shBrushR.js
added in the scripts
directory), change the following paths accordingly and insert them before the <body>
tag in your HTML page:
<link rel="stylesheet" href="path/to/your/syntaxhighlighter/styles/shCore.css" type="text/css" /> <link rel="stylesheet" href="path/to/your/syntaxhighlighter/styles/shThemeDefault.css" type="text/css" /> <script type='text/javascript' src='path/to/your/syntaxhighlighter/scripts/shCore.js'></script> <script type='text/javascript' src='path/to/your/syntaxhighlighter/scripts/shAutoloader.js'></script>
Then add these lines in the footer area (right before the </body>
tag):
<script type="text/javascript"> SyntaxHighlighter.autoloader( "r path/to/your/syntaxhighlighter/scripts/shBrushR.js", "plain path/to/your/syntaxhighlighter/scripts/shBrushPlain.js", "sql path/to/your/syntaxhighlighter/scripts/shBrushSql.js", "js path/to/your/syntaxhighlighter/scripts/shBrushJScript.js", "html xml path/to/your/syntaxhighlighter/scripts/shBrushXml.js" ); SyntaxHighlighter.defaults["toolbar"] = false; SyntaxHighlighter.all(); </script>
Above is only my configuration; you may refer to the manual of SyntaxHighlighter for more information. To add those HTML code in your pages, you may modify the theme files (typically the header.php
and footer.php
). To make use of highlighting, you need to assign a special CSS class to your <pre></code> tag, e.g.</p>
<pre><pre class="brush: r">
test.function = function(r) {
return(pi * r^2)
}
test.function(1)
</pre>
</pre>
<p>The result:</p>
<pre>test.function = function(r) {
return(pi * r^2)
}
test.function(1)
</pre>
<p>You may double-click on the code to select all of them, and copy them with normally (e.g. with Ctrl + C). The old version of SyntaxHighlighter uses a Flash file to copy the code to the clipboard and you have to select the line numbers as well when you only want to select the code; I do not like these features.</p>
<p>Finally, the JS brush file can be improved in a few aspects but in fact I’m not quite good at JS RegExp, so I leave these possible improvements to the readers:</p>
<ol>
<li>highlight the function arguments — loosely speaking, the pattern is: they are in parentheses “()” followed by “=”; the first argument follows “(” and others follow “,”.</li>
<li>highlight the variables — those strings followed by “<-” or “=” when “=” is not in paretheses.</li>
</ol>
<p>P.S. today I converted all my <code><pre></code> tags in this whole site to <code><pre class="brush: r"></code> using a SQL command:</p>
<pre>UPDATE `wp_posts`
SET `post_content` = REPLACE(`post_content`, '<pre>', '<pre>')
WHERE `post_content` LIKE '%<pre>%';
</pre>
<p>You may also consider this batch processing instead of manually adding <code>class="brush: r"
to your
tags.Related Posts
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.