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 97, Mon May 3 16:07:46 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(name,src,...){
56  examples.after.return <- forfun(function(src,...){           coll <- paste(src,collapse="\n")
57    rline <- grep("return([^)]*)",src)           thispre <- gsub("^[\\^]","",prefix)
58    if(length(rline)==0)return(list())           FIND <- paste("(return|UseMethod)[(][^\\n]*\\n",thispre,sep="")
59    rline <- rline[length(rline)]           m <- regexpr(FIND,coll)
60    comment.line.nums <- grep(prefix,src)           if(m[1]==-1)return(list())
61    if(!(comment.end <- rline+1)%in%comment.line.nums)return(list())           after <- substr(coll,m[1],nchar(coll))
62    while(comment.end%in%comment.line.nums)comment.end <- comment.end+1           FIND <-
63    excode <- src[comment.end:(length(src)-1)]             paste("[^\\n]*",# rest of the return line
64    list(examples=paste(gsub("^\\W*","",excode),collapse="\n"))                   "((?:\\n###[^\\n]*)+)",#comment value lines \\1
65  })                   "([\\w\\W]*)[}]",#examples \\2
66                     sep="")
67             SEP <- "----------"
68             REP <- paste("\\1",SEP,"\\2",sep="")
69             r <- strsplit(gsub(FIND,REP,after,perl=TRUE),split=SEP)[[1]]
70             l <- strsplit(r,split="\n")
71             ##if(name=="dl.combine")browser()
72             excode <- c(l[[2]],"")
73             prefixes <- gsub("(\\s*).*","\\1",excode,perl=TRUE)[grep("\\w",excode)]
74             FIND <- prefixes[which.min(nchar(prefixes))]
75             list(examples=paste(sub(FIND,"",excode),collapse="\n"),
76                  value=decomment(l[[1]][-1]))
77           }))
78    
79    ### List of parser functions that operate on single objects. This list
80    ### is useful for testing these functions, ie
81    ### lonely$parsefuns(attr(extract.docs.file,"src"),"extract.docs.file")
82    lonely <- sapply(forall.parsers,function(L)L[[2]])
83    
84  extract.docs.file <- function # Extract documentation from a file  extract.docs.file <- function # Extract documentation from a file
85  ### Parse R code to extract inline documentation from comments around  ### Parse R code to extract inline documentation from comments around
# Line 175  Line 192 
192  ### named list of lists, one for each object to document.  ### named list of lists, one for each object to document.
193  }  }
194    
195  ### Default parsers to use with package.skeleton.dx  ### List of parsers to use by default with package.skeleton.dx.
196  default.parsers <- list("parsefuns","extract.docs.file",  default.parsers <- c(extract.docs.file=extract.docs.file,
197                          "examples.after.return","examples.from.testfile",                       sapply(forall.parsers,function(L)L[[1]](L[[2]])))
                         "author.from.description","erase.format","title.from.name")  
198    
199  extract.docs.fun <- function # Extract documentation from a function  extract.docs.fun <- function # Extract documentation from a function
200  ### 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 203 
203  ### The function to examine.  ### The function to examine.
204   name.fun   name.fun
205  ### The name of the function/chunk to use in warning messages.  ### The name of the function/chunk to use in warning messages.
206   )   ){
 {  
207    res <- list()    res <- list()
208    clines <- grep(prefix,code)    clines <- grep(prefix,code)
209    if(length(grep("#",code[1]))){    if(length(grep("#",code[1]))){
# Line 574  Line 589 
589  ### Using the same conventions as for functions, definitions of S4 classes  ### Using the same conventions as for functions, definitions of S4 classes
590  ### in the form \code{setClass("classname",\dots)} are also located and  ### in the form \code{setClass("classname",\dots)} are also located and
591  ### scanned for inline comments.  ### scanned for inline comments.
592  (doc.link)  (doc.link
593  ### DocLink object as created by \code{\link{extract.file.parse}}.  ### DocLink object as created by \code{\link{extract.file.parse}}.
594  ### Note that \code{source} statements are \emph{ignored} when scanning for  ### Note that \code{source} statements are \emph{ignored} when scanning for
595  ### class definitions.  ### class definitions.
596  {   ){
597    chunk.source <- doc.link@code    chunk.source <- doc.link@code
598    ##details<<    ##details<<
599    ## 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.97

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