SCM

SCM Repository

[inlinedocs] Diff of /pkg/inlinedocs/R/parsers.R
ViewVC logotype

Diff of /pkg/inlinedocs/R/parsers.R

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 87, Sat May 1 12:21:27 2010 UTC revision 95, Mon May 3 14:22:42 2010 UTC
# Line 12  Line 12 
12    function(objs,docs,...){    function(objs,docs,...){
13      objs <- objs[sapply(objs,subfun)]      objs <- objs[sapply(objs,subfun)]
14      L <- list()      L <- list()
15      for(N in names(objs)){      for(N in names(docs)){
16        o <- objs[[N]]        o <- objs[[N]]
17        L[[N]] <- FUN(src=attr(o,"source"),name=N,objs=objs,o=o,docs=docs,doc=docs[[N]],...)        L[[N]] <- FUN(src=attr(o,"source"),
18                        name=N,objs=objs,o=o,docs=docs,doc=docs[[N]],...)
19      }      }
20      L      L
21    }    }
# Line 24  Line 25 
25  ### For each function in the package, do something.  ### For each function in the package, do something.
26  forfun <- function(FUN)forall(FUN,is.function)  forfun <- function(FUN)forall(FUN,is.function)
27    
28  ### Get last line (test)  ### Default parsers to use with package.skeleton.dx
29  parsefuns <- forfun(function(src,name,...){  forall.parsers <-
30      list(## Extract lots of info from normal functions.
31           parsefun=list(forfun,function(src,name,...){
32    extract.docs.fun(src,name)    extract.docs.fun(src,name)
33  })         }),
34           ## Fill in author from DESCRIPTION and titles.
35  ### Fill in author from DESCRIPTION and titles.         author.from.description=list(forall,function(desc,...){
 author.from.description <- forall(function(desc,...){  
36    list(author=desc[,"Maintainer"])    list(author=desc[,"Maintainer"])
37  })         }),
38           ## The format section sometimes causes problems, so erase it.
39  ### The format section sometimes causes problems, so erase it.         erase.format=list(forall,function(...){
 erase.format <- forall(function(...){  
40    list(format="")    list(format="")
41  })         }),
42           ## Convert the function name to a title.
43  ### Convert the function name to a title.         title.from.name=list(forall,function(name,doc,...){
44  title.from.name <- forall(function(name,...){           if("title"%in%names(doc))list() else
45    list(title=gsub("[._]"," ",name))    list(title=gsub("[._]"," ",name))
46  })         }),
47           ## Get examples for FUN from the file test/FUN.R
48  ### Get examples for FUN from the file test/FUN.R         examples.from.testfile=list(forfun,function(name,...){
 examples.from.testfile <- forfun(function(name,...){  
49    tfile <- file.path("..","tests",paste(name,".R",sep=""))    tfile <- file.path("..","tests",paste(name,".R",sep=""))
50    if(file.exists(tfile))    if(file.exists(tfile))
51      list(examples=paste(readLines(tfile),collapse="\n"))      list(examples=paste(readLines(tfile),collapse="\n"))
52    else list()    else list()
53  })         }),
54           ## Get examples from inline definitions after return()
55  ### Get examples from inline definitions after return()         examples.after.return=list(forfun,function(src,...){
56  examples.after.return <- forfun(function(src,...){           rline <- grep("^\\W*return[(]",src)
   rline <- grep("return([^)]*)",src)  
57    if(length(rline)==0)return(list())    if(length(rline)==0)return(list())
58    rline <- rline[length(rline)]    rline <- rline[length(rline)]
59    comment.line.nums <- grep(prefix,src)    comment.line.nums <- grep(prefix,src)
# Line 62  Line 61 
61    while(comment.end%in%comment.line.nums)comment.end <- comment.end+1    while(comment.end%in%comment.line.nums)comment.end <- comment.end+1
62    excode <- src[comment.end:(length(src)-1)]    excode <- src[comment.end:(length(src)-1)]
63    list(examples=paste(gsub("^\\W*","",excode),collapse="\n"))    list(examples=paste(gsub("^\\W*","",excode),collapse="\n"))
64  })         }))
65    
66    ### List of parser functions that operate on single objects. This list
67    ### is useful for testing these functions, ie
68    ### lonely$parsefuns(attr(extract.docs.file,"src"),"extract.docs.file")
69    lonely <- sapply(forall.parsers,function(L)L[[2]])
70    
71  extract.docs.file <- function # Extract documentation from a file  extract.docs.file <- function # Extract documentation from a file
72  ### Parse R code to extract inline documentation from comments around  ### Parse R code to extract inline documentation from comments around
# Line 175  Line 179 
179  ### named list of lists, one for each object to document.  ### named list of lists, one for each object to document.
180  }  }
181    
182  ### Default parsers to use with package.skeleton.dx  ### List of parsers to use by default with package.skeleton.dx.
183  default.parsers <- list("parsefuns","extract.docs.file",  default.parsers <- c(extract.docs.file=extract.docs.file,
184                          "examples.after.return","examples.from.testfile",                       sapply(forall.parsers,function(L)L[[1]](L[[2]])))
                         "author.from.description","erase.format","title.from.name")  
185    
186  extract.docs.fun <- function # Extract documentation from a function  extract.docs.fun <- function # Extract documentation from a function
187  ### Given source code of a function, return a list describing inline  ### Given source code of a function, return a list describing inline
# Line 187  Line 190 
190  ### The function to examine.  ### The function to examine.
191   name.fun   name.fun
192  ### The name of the function/chunk to use in warning messages.  ### The name of the function/chunk to use in warning messages.
193   )   ){
 {  
194    res <- list()    res <- list()
195    clines <- grep(prefix,code)    clines <- grep(prefix,code)
196    if(length(grep("#",code[1]))){    if(length(grep("#",code[1]))){
# Line 574  Line 576 
576  ### Using the same conventions as for functions, definitions of S4 classes  ### Using the same conventions as for functions, definitions of S4 classes
577  ### in the form \code{setClass("classname",\dots)} are also located and  ### in the form \code{setClass("classname",\dots)} are also located and
578  ### scanned for inline comments.  ### scanned for inline comments.
579  (doc.link)  (doc.link
580  ### DocLink object as created by \code{\link{extract.file.parse}}.  ### DocLink object as created by \code{\link{extract.file.parse}}.
581  ### Note that \code{source} statements are \emph{ignored} when scanning for  ### Note that \code{source} statements are \emph{ignored} when scanning for
582  ### class definitions.  ### class definitions.
583  {   ){
584    chunk.source <- doc.link@code    chunk.source <- doc.link@code
585    ##details<<    ##details<<
586    ## Extraction of S4 class documentation is currently limited to expressions    ## Extraction of S4 class documentation is currently limited to expressions

Legend:
Removed from v.87  
changed lines
  Added in v.95

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge