68

Is there some mechanism by which I can transform the comments that roxygen sees, preferably before it does the roxygen->rd conversion?

For example, suppose I have:

#' My function. Does stuff with numbers.
#'
#' This takes an input `x` and does something with it.
#' @param x a number.
myFunction <- function (x) {
}

Now, suppose I want to do some conversion of the comment before roxygen parses it, for example replacing all instances of things in backticks with \code{}. Ie:

preprocess <- function (txt) {
    gsub('`([^ ]+)`', '\\\\code{\\1}', txt)
}
# cat(preprocess('Takes an input `x` and does something with it'.))
# Takes an input \code{x} and does something with it.

Can I feed preprocess into roxygen somehow so that it will run it on the doclets before (or after would work in this case) roxygen does its document generation?

I don't want to do a permanent find-replace in my .r files. As you might guess from my example, I'm aiming towards some rudimentary markdown support in my roxygen comments, and hence wish to keep my .r files as-is to preserve readability (and insert the \code{..} stuff programmatically).

Should I just write my own version of roxygenise that runs preprocess on all detected roxygen-style comments in my files, saves them temporarily somewhere, and then runs the actual roxygenise on those?

user227710
  • 3,164
  • 18
  • 35
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194
  • 1
    I haven't tried this before, but AFAIK you can write your own roclets and specify these in your roxygen call, i.e. you specify your new `roclet` in the argument `roxygenize(..., roclet=mc_roclet)` – Andrie Mar 21 '13 at 09:46
  • 3
    I'd recommend building on top of https://github.com/hadley/roxygen3, and looking at the internals of the markdown package to do this properly. I would be happy to accept a patch. – hadley Mar 21 '13 at 13:17
  • @hadley cheers, I've already checked out a copy roxygen3, just wanted to see if anyone had a hacked-together solution before I started digging around. If I manage to code up something that works I'll submit a patch – mathematical.coffee Mar 22 '13 at 03:36

1 Answers1

0

Revisiting this a couple of years later, it looks like Roxygen has a function register.preref.parsers that one can use to inject their own parsers into roxygen. One such use of this is the promising maxygen package (markdown + roxygen = maxygen), which a very neat implementation of markdown processing of roxygen comments (albeit only to the CommonMark spec), and you can see how it is used in that package's macument function. I eagerly await "pandoc + roxygen = pandoxygen"... :)

mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194