Search code examples
rjekyllsyntax-highlightingrouge

Is rouge syntax highlighting meant to highlight non-base functions in R?


I'm trying to understand the rouge syntax highlighter and in particular use it to highlight R code. Ultimately it's for a website built with jekyll but I've been able to isolate my problems to just rouge (eg I use rougify on sample code to produce inspectable HTML).

My problem is that most of my code is given class = "n", which I think stands for "name" and is not distinguishable from arbitrary variables. Most CSS for syntax highlighters I think leaves code of class "n" untouched. Here's an example of what's generated from library(ggseas):

<span class="n">library</span><span class="p">(</span><span class="n">ggseas</span><span class="p">)</span><span class="w">

From what I understand of the discussion on this pull request, rouge highlighting of R only worked at all from early June. Looking at what I'm pretty sure is the key bit of code in the source for rouge, I think that only functions in the variable PRIMITIVE_FUNCTIONS are going to be highlighted. In other words, by leaving all non primitive functions unhighlighted rouge is working as it should, it just has got a very limited sense of R syntax.

My question is, have I understood it right?

I need things like library() and ggplot() to be highlighted even though they aren't primitive functions in the base package of R. If I have understood things correctly, I will either have to hack the source of rouge to include more functions or try to move to something else.


Solution

  • I have been using rouge for R syntax highlighting for a little while, so I can confirm that it has worked in rouge for longer than just this month and also that it does a pretty decent job (personal opinion).

    I have a GitHub pages site generated using jekyll and I went through the same process of switching over to rouge as the highlighter a little while ago. But it is working well now for R code. Below is an example screenshot of my site (with some extra code that I added to benefit this post).

    R code on a GitHub pages site using rouge

    The code on the page for library(ggseas) and library("ggseas") (the way I usually write it) is as you suggested:

    <span class="n">library</span><span class="p">(</span><span class="n">ggseas</span><span class="p">)</span> 
    <span class="n">library</span><span class="p">(</span><span class="s2">"ggseas"</span><span class="p">)</span>
    

    As you can see, the special highlighting of PRIMITIVE_FUNCTIONS refers to the highlighting like we see in the for-loop for the words for and in.

    However, this is the same way that highlighting works in the popular IDE Studio with regards to ggplot() and ggsea. Only library() (and also some other functions like require()) are not included in the list of PRIMITIVE_FUNCTIONS, and so do not get highlighted as they should:

    R code in the RStudio IDE

    So, I'm curious what your code looked like before that you can't get with rouge. If you only need to fix highlighting for library() and require() (and probably a few others), then you should be able to just add them to the list of PRIMITIVE_FUNCTIONS as you surmised. But if you want special highlighting for all non-base functions, then it will get more complicated.

    In case it helps, I have posted the last part of my _config.yml file for my jekyl generated site.

    # Build settings
    destination: _site
    paginate: 10
    permalink: /:year/:title/
    markdown: kramdown #redcarpet
    highlighter: rouge
    
    #redcarpet:
    #  extensions: ["no_intra_emphasis", "fenced_code_blocks", "autolink", "tables", "strikethrough", "superscript", "with_toc_data"]
    #redcarpet:
    #  extensions: ["no_intra_emphasis", "fenced_code_blocks", "autolink", "tables", "with_toc_data"]
    
    kramdown:
      # use Github Flavored Markdown
      input: GFM
      auto_ids: true
      syntax_highlighter: rouge
      # do not replace newlines by <br>s
      hard_wrap: false
    
    gems: ['jekyll-paginate']
    exclude: ['README.md', 'Gemfile', 'Gemfile.lock', 'screenshot.png']
    

    I also just noticed that it doesn't color code the parenthesis and braces, which is desirable.