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 104, Tue May 18 16:35:08 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)  ### Parsers for each object/function that are constructed
29  parsefuns <- forfun(function(src,name,...){  ### automatically. This is a named list, each element is a list of 2
30    ### elements: forfun/forall, then a parser function for an individual
31    ### object.
32    forall.parsers <-
33      list(## Extract lots of info from normal functions.
34           parsefun=list(forfun,function(src,name,...){
35    extract.docs.fun(src,name)    extract.docs.fun(src,name)
36  })         }),
37           ## Fill in author from DESCRIPTION and titles.
38  ### Fill in author from DESCRIPTION and titles.         author.from.description=list(forall,function(desc,...){
 author.from.description <- forall(function(desc,...){  
39    list(author=desc[,"Maintainer"])    list(author=desc[,"Maintainer"])
40  })         }),
41           ## The format section sometimes causes problems, so erase it.
42  ### The format section sometimes causes problems, so erase it.         erase.format=list(forall,function(...){
 erase.format <- forall(function(...){  
43    list(format="")    list(format="")
44  })         }),
45           ## Convert the function name to a title.
46  ### Convert the function name to a title.         title.from.name=list(forall,function(name,doc,...){
47  title.from.name <- forall(function(name,...){           if("title"%in%names(doc))list() else
48    list(title=gsub("[._]"," ",name))    list(title=gsub("[._]"," ",name))
49  })         }),
50           ## Get examples for FUN from the file test/FUN.R
51  ### Get examples for FUN from the file test/FUN.R         examples.from.testfile=list(forfun,function(name,...){
 examples.from.testfile <- forfun(function(name,...){  
52    tfile <- file.path("..","tests",paste(name,".R",sep=""))    tfile <- file.path("..","tests",paste(name,".R",sep=""))
53    if(file.exists(tfile))    if(file.exists(tfile))
54      list(examples=paste(readLines(tfile),collapse="\n"))      list(examples=paste(readLines(tfile),collapse="\n"))
55    else list()    else list()
56  })         }),
57           ## Get examples from inline definitions after return()
58  ### Get examples from inline definitions after return()         examples.after.return=list(forfun,function(name,src,...){
59  examples.after.return <- forfun(function(src,...){           coll <- paste(src,collapse="\n")
60    rline <- grep("return([^)]*)",src)           thispre <- gsub("^[\\^]","",prefix)
61    if(length(rline)==0)return(list())           FIND <- paste("(return|UseMethod)[(][^\\n]*\\n",thispre,sep="")
62    rline <- rline[length(rline)]           m <- regexpr(FIND,coll)
63    comment.line.nums <- grep(prefix,src)           if(m[1]==-1)return(list())
64    if(!(comment.end <- rline+1)%in%comment.line.nums)return(list())           after <- substr(coll,m[1],nchar(coll))
65    while(comment.end%in%comment.line.nums)comment.end <- comment.end+1           FIND <-
66    excode <- src[comment.end:(length(src)-1)]             paste("[^\\n]*",# rest of the return line
67    list(examples=paste(gsub("^\\W*","",excode),collapse="\n"))                   "((?:\\n###[^\\n]*)+)",#comment value lines \\1
68  })                   "([\\w\\W]*)[}]",#examples \\2
69                     sep="")
70             SEP <- "-/-/-/-/-=====-----"
71             REP <- paste("\\1",SEP,"\\2",sep="")
72             r <- strsplit(gsub(FIND,REP,after,perl=TRUE),split=SEP)[[1]]
73             l <- strsplit(r,split="\n")
74             excode <- c(l[[2]],"")
75             if(length(l)<2||paste(excode,collapse="")=="")return(list())
76             prefixes <- gsub("(\\s*).*","\\1",excode,perl=TRUE)[grep("\\w",excode)]
77             FIND <- prefixes[which.min(nchar(prefixes))]
78             list(examples=paste(sub(FIND,"",excode),collapse="\n"),
79                  value=decomment(l[[1]][-1]))
80           }))
81    
82    ### List of parser functions that operate on single objects. This list
83    ### is useful for testing these functions, ie
84    ### lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")
85    lonely <- sapply(forall.parsers,function(L)L[[2]])
86    
87  extract.docs.file <- function # Extract documentation from a file  extract.docs.file <- function # Extract documentation from a file
88  ### Parse R code to extract inline documentation from comments around  ### Parse R code to extract inline documentation from comments around
# Line 175  Line 195 
195  ### named list of lists, one for each object to document.  ### named list of lists, one for each object to document.
196  }  }
197    
198  ### Default parsers to use with package.skeleton.dx  ### List of parsers to use by default with package.skeleton.dx.
199  default.parsers <- list("parsefuns","extract.docs.file",  default.parsers <- c(extract.docs.file=extract.docs.file,
200                          "examples.after.return","examples.from.testfile",                       sapply(forall.parsers,function(L)L[[1]](L[[2]])))
                         "author.from.description","erase.format","title.from.name")  
201    
202  extract.docs.fun <- function # Extract documentation from a function  extract.docs.fun <- function # Extract documentation from a function
203  ### 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 206 
206  ### The function to examine.  ### The function to examine.
207   name.fun   name.fun
208  ### The name of the function/chunk to use in warning messages.  ### The name of the function/chunk to use in warning messages.
209   )   ){
 {  
210    res <- list()    res <- list()
211    clines <- grep(prefix,code)    clines <- grep(prefix,code)
212    if(length(grep("#",code[1]))){    if(length(grep("#",code[1]))){
# Line 574  Line 592 
592  ### Using the same conventions as for functions, definitions of S4 classes  ### Using the same conventions as for functions, definitions of S4 classes
593  ### in the form \code{setClass("classname",\dots)} are also located and  ### in the form \code{setClass("classname",\dots)} are also located and
594  ### scanned for inline comments.  ### scanned for inline comments.
595  (doc.link)  (doc.link
596  ### DocLink object as created by \code{\link{extract.file.parse}}.  ### DocLink object as created by \code{\link{extract.file.parse}}.
597  ### Note that \code{source} statements are \emph{ignored} when scanning for  ### Note that \code{source} statements are \emph{ignored} when scanning for
598  ### class definitions.  ### class definitions.
599  {   ){
600    chunk.source <- doc.link@code    chunk.source <- doc.link@code
601    ##details<<    ##details<<
602    ## 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.104

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