|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|
<html xmlns="http://www.w3.org/1999/xhtml"> |
|
<head> |
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
|
<meta http-equiv="Content-Style-Type" content="text/css" /> |
|
<meta name="generator" content="pandoc" /> |
|
<title>Prism Syntax Highlighter for R</title> |
|
<style type="text/css">code{white-space: pre;}</style> |
|
<link rel='stylesheet' href='page.css' /> |
|
<!-- style for prism.js (you may wish to combine the two) --> |
|
<link id="theme-stylesheet" rel='stylesheet' href='prism.okaidia.css' /> |
|
<link id="theme-stylesheet" rel='stylesheet' href='prism.r.css' /> |
|
</head> |
|
<body> |
|
<header> |
|
<h1 id="prism-syntax-highlighting-in-r">Prism syntax highlighting in R</h1> |
|
</header> |
|
<div id="tabs"> |
|
<ul> |
|
<li class="selected"><a id="okaidia" href="javascript://" onclick="setTheme(this)">Okaidia</a></li> |
|
<li><a id="tomorrownight80s" href="javascript://" onclick="setTheme(this)">Tomorrow Night 80s</a></li> |
|
<li><a id="zenburn" href="javascript://" onclick="setTheme(this)">Zenburn</a></li> |
|
</ul> |
|
</div> |
|
|
|
<div class="content"> |
|
<p>This is a test of <a href="http://prismjs.com/">Prism syntax highlighting</a> for <a href="http://www.r-project.org/">R</a>.</p> |
|
<p>You need to include <a href="prism.r.js">prism.r.js</a> into your site, and also define CSS classes <code>.token.function</code>, <code>.token.variable</code> and (optionally) <code>.token.namespace</code>.</p> |
|
<p>This is in addition to your chosen prism.min.js and stylesheet you download from the <a href="http://prismjs.com/download">PrismJS website</a>. (You may wish to minify prism.r.js and combine it into prism.min.js).</p> |
|
<pre class="language-markup"><code><script src="prism.min.js" type="text/javascript"></script> |
|
<script src="prism.r.js" type="text/javascript"></script></code></pre> |
|
<pre class="language-css"><code>.token.function { ... } |
|
.token.variable { ... } |
|
.token.namespace { ... }</code></pre> |
|
<p>The <a href="prism.tomorrownight80s.css">CSS sheet used for this page</a> is based off Chris Kempson's <a href="https://github.com/chriskempson/tomorrow-theme">Tomorrow Night 80s palette</a>, matching the one of the same name used by RStudio.</p> |
|
<h1 id="example">Example</h1> |
|
<p>If you add the "language-r" class to inline code it will also be marked up: <code class="language-r">x <- 1 + 2</code>. |
|
The following is some block R code.</p> |
|
<pre class="language-r"><code># This is a comment. |
|
|
|
like <- function (times=1, like=T, ...) { |
|
str <- 'I do not like green eggs and ham!' |
|
if (like) str <- gsub('do not ', '', str) |
|
for (i in 1:10) { |
|
print(str) |
|
} |
|
return(paste(...)) |
|
} |
|
|
|
x <- "Here is a |
|
quite long |
|
multiline \t \" |
|
string with ' embedded. |
|
" |
|
|
|
x <- c( |
|
1e2 + 2i * 10, |
|
NA, |
|
NULL, |
|
Inf, |
|
NA_character_, |
|
TRUE, |
|
FALSE |
|
) |
|
|
|
y@slot |
|
list(a=1)$a[1] |
|
|
|
?table |
|
|
|
knitr::knit</code></pre> |
|
<h1 id="themes">Themes</h1> |
|
<p> |
|
Use the tab bar up top to switch between themes. |
|
The themes shown here are: |
|
</p> |
|
<ul> |
|
<li><a href="prism.okaidia.css">Okaidia</a>, the same as the one <a href="http://prismjs.com/index.html?theme=prism-okaidia#examples">provided with Prism</a> but with the added CSS classes <code>.token.{variable, namespace, function}</code>; |
|
<li><a href="prism.zenburn.css">Zenburn</a>, based off <a href="http://slinky.imukuppi.org/zenburnpage/">Jani Nurminen's vim theme</a> of the same name; and</li> |
|
<li><a href="prism.tomorrownight80s.css">Tomorrow Night 80s</a>, based off Chris Kempson's <a href="https://github.com/chriskempson/tomorrow-theme">Tomorrow Night 80s palette</a>, matching the one of the same name used by RStudio.</li> |
|
</ul> |
|
|
|
<h1 id="known-issue">Known Issue</h1> |
|
<p>Hash characters (i.e. the symbol that you use to comment) within strings screw up highlighting. This is a typical problem of any regex-based syntax highlighter and is <a href="http://prismjs.com/examples.html#failures">known by Prism</a>.</p> |
|
|
|
<pre class="language-r"><code> |
|
x <- "Here is a string with a # inside." |
|
</code></pre> |
|
|
|
<p>There are a few workarounds you will try, though as with all regex-based solutions there will be cases where it doesn't work. Pick what suits you best.</p> |
|
|
|
<p>If you like, you can swap the order of the 'comment' and 'string' lines in <a href="prism.r.js">prism.r.js</a> which will cause strings to take priority over comments. |
|
However, this means that if you have a comment with a string in it, it will fail.</p> |
|
|
|
<!-- Note: if you're viewing the source, ignore these next few pre/code with hard-coded syntax highlighting, that's to demonstrate the bug. --> |
|
<pre class="language-"><code class="language-"> |
|
<span class="token variable">x</span> <span class="token operator"><-</span> <span class="token string">"Here is a string with a # inside."</span> <span class="token comment" spellcheck="true"># and here is a comment with </span><span class="token string">"quotes"</span> <span class="token keyword">in</span> <span class="token variable">it</span> |
|
</code></pre> |
|
|
|
<p>Alternatively, you can change the 'comment' regex to this:</p> |
|
<!-- Note: hard-coded syntax highlighting here because of the similar bug I'm trying to explain. Strings withing regexes. --> |
|
<pre class=" language-"><code class=" language-"> |
|
Prism<span class="token punctuation">.</span>languages<span class="token punctuation">.</span>r <span class="token operator">=</span> <span class="token punctuation">{</span> |
|
</span> <span class="token string">'comment'</span><span class="token punctuation">:</span> <span class="token regex">/#(?=(?:[^"\\\r\n]*(\\.|"(?:[^"\\\r\n]*\\.)*[^"\\\r\n]*"))*[^"\r\n]*$).*$/gm</span><span class="token punctuation">,</span> |
|
<span class="token comment" spellcheck="true"> // ... the rest |
|
</span></code></pre> |
|
|
|
<p>This means that a hash tag within double quotes will not be highlighted. Also, if a comment has <b>balanced</b> double quotes in it it will be highlighted properly as a comment. |
|
However, if a comment has <b>unbalanced</b> double quotes in it (i.e. an odd number), that comment will <b>not</b> be highlighted properly. |
|
Also, if your string is surrounded by single quotes (not double) and has a hash tag in it, you will suffer from the original problem. (If you modify the regex above to allow hash tags within single quotes, you will have to be careful never to have unbalanced single quotes (i.e. apostrophes!) in your comments or else the comments won't be highlighted. Words with apostrophes are so common that this is a Bad Idea).</p> |
|
<p>Again, regex highlighters always have these problems. If you think you never have an unbalanced double quote in a comment this could be a feasible solution, but pick according to your usage.</p> |
|
|
|
<pre class=" language-"><code class=" language-"> |
|
<span class="token variable">x</span> <span class="token operator"><-</span> <span class="token string">"Here is a string with a # inside."</span> <span class="token comment" spellcheck="true"># and here is a comment with "quotes" in it</span> |
|
<span class="token string">"multiline string with |
|
a hash tag # inside it"</span> |
|
# <span class="token variable">comment</span> <span class="token variable">with</span> <span class="token variable">unbalanced</span> <span class="token variable">quote</span> <span class="token variable">mark</span><span class="token punctuation">:</span> " <span class="token variable">inside</span><span class="token punctuation">.</span> <span class="token variable">Won</span>'<span class="token variable">t</span> <span class="token variable">highlight</span><span class="token punctuation">.</span> |
|
</code></pre> |
|
|
|
<h1 id="bugs-suggestions-and-improvements">Bugs, Suggestions and Improvements</h1> |
|
<p><script type="text/javascript"> |
|
<!-- |
|
h='gmail.com';a='@';n='mathematical.coffee';e=n+a+h; |
|
document.write('<a h'+'ref'+'="ma'+'ilto'+':'+e+'">'+'Let me know about them!'+'<\/'+'a'+'>'); |
|
// --> |
|
</script><noscript>Let me know about them! (mathematical.coffee at gmail dot com)</noscript></p> |
|
</div> |
|
<!-- including the prism JS files (you may wish to combine prism.min.js & prism.r.js and minify both) --> |
|
<script src="prism.min.js"></script> |
|
<script src="prism.r.js"></script> |
|
<script type="text/javascript"> |
|
var last = document.getElementsByClassName('selected')[0]; |
|
function setTheme(node) { |
|
document.getElementById('theme-stylesheet').href = 'prism.' + node.id + '.css'; |
|
last.className = last.className.replace(/ ?\bselected\b/, ''); |
|
last = node.parentNode; |
|
last.className = last.className + ' selected'; |
|
} |
|
setTheme(last.firstChild); |
|
</script> |
|
</body> |
|
</html> |
Hello, it's lovely work you've done here.
I want to use this but I'm not familiar with CSS and javascript. Would you be able to provide any instructions for how to incorporate this language-r into the Lector theme I'm using for my Ghost blog, which already makes use of prism.js ?
https://creativemarket.com/temacotta/16399-Lector
Thanks!