Pre- or post-process roxygen snippets
Asked Answered
E

1

67

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?

Empire answered 21/3, 2013 at 5:42 Comment(3)
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)Nonstop
I'd recommend building on top of 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.Hindustan
@Hindustan 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 patchEmpire
E
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"... :)

Empire answered 23/9, 2015 at 0:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.