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 186, Fri Jan 14 18:43:36 2011 UTC revision 196, Fri Feb 18 12:52:48 2011 UTC
# Line 9  Line 9 
9  combine.list <- function(x,y){  combine.list <- function(x,y){
10    toadd <- !names(y)%in%names(x)    toadd <- !names(y)%in%names(x)
11    toup <- names(y)[!toadd]    toup <- names(y)[!toadd]
12    if("doc"%in%names(x))return(x$doc)    ##if("doc"%in%names(x))return(x$doc)
13    if("doc"%in%names(y))return(y$doc)    ##if("doc"%in%names(y))return(y$doc)
14    x[names(y)[toadd]] <- y[toadd]    x[names(y)[toadd]] <- y[toadd]
15    for(up in toup)x[[up]] <- combine(x[[up]],y[[up]])    for(up in toup)x[[up]] <- combine(x[[up]],y[[up]])
16    return(x)    return(x)
# Line 184  Line 184 
184    res    res
185  },ex=function(){  },ex=function(){
186  test <- function  test <- function
187  ### the desc  ### the description
188  (x,  (x,
189  ### the first argument  ### the first argument
190   y ##<< another argument   y ##<< another argument
# Line 282  Line 282 
282          chunk.sep <- "\n\n"          chunk.sep <- "\n\n"
283        }        }
284        chunk.res <- NULL        chunk.res <- NULL
285        if ( 0 == length(grep("^\\s*$",payload,perl=TRUE)) )        if ( !grepl("^\\s*$",payload,perl=TRUE) )
286          chunk.res <-          chunk.res <-
287            if ( is.null(res[[field]]) ) payload            if ( is.null(res[[field]]) ) payload
288            else paste(res[[field]], payload, sep=chunk.sep)            else paste(res[[field]], payload, sep=chunk.sep)
# Line 290  Line 290 
290      }      }
291    while ( k <= length(src) ){    while ( k <= length(src) ){
292      line <- src[k]      line <- src[k]
293      if ( 0 < length(grep(extra.regexp,line,perl=TRUE) ) ){      if ( grepl(extra.regexp,line,perl=TRUE) ){
294        ## we have a new extra chunk - first get field name and any payload        ## we have a new extra chunk - first get field name and any payload
295        new.field <- gsub(extra.regexp,"\\1",line,perl=TRUE)        new.field <- gsub(extra.regexp,"\\1",line,perl=TRUE)
296        new.contents <- gsub(extra.regexp,"\\2",line,perl=TRUE)        new.contents <- gsub(extra.regexp,"\\2",line,perl=TRUE)
# Line 365  Line 365 
365            first.describe <- TRUE;            first.describe <- TRUE;
366          }          }
367        }        }
368      } else if ( in.chunk && 0<length(grep(cont.re,line,perl=TRUE)) ){      } else if ( in.chunk && grepl(cont.re,line,perl=TRUE) ){
369        ## append this line to current chunk        ## append this line to current chunk
370        if ( 0 == length(grep(prefix,line,perl=TRUE)) ){        if ( !grepl(prefix,line,perl=TRUE) ){
371          ##describe<< Any lines with "\code{### }" at the left hand          ##describe<< Any lines with "\code{### }" at the left hand
372          ## margin within the included chunks are handled separately,          ## margin within the included chunks are handled separately,
373          ## so if they appear in the documentation they will appear          ## so if they appear in the documentation they will appear
# Line 380  Line 380 
380            payload <- stripped            payload <- stripped
381          }          }
382        }        }
383      } else if ( 0 < length(grep(arg.pat,line,perl=TRUE)) ){      } else if ( grepl(arg.pat,line,perl=TRUE) ){
384        not.describe <- (0==in.describe && !first.describe)        not.describe <- (0==in.describe && !first.describe)
385        if ( in.chunk && not.describe){        if ( in.chunk && not.describe){
386          res[[cur.field]] <- end.chunk(cur.field,payload)          res[[cur.field]] <- end.chunk(cur.field,payload)
# Line 450  Line 450 
450         examples.after.return=examples.after.return,         examples.after.return=examples.after.return,
451         extract.xxx.chunks=extract.xxx.chunks,         extract.xxx.chunks=extract.xxx.chunks,
452         ## title from first line of function def         ## title from first line of function def
453         title.from.firstline=function(src,name,code,...){         title.from.firstline=function(src,...){
454           if(length(grep("#",src[1]))){           first <- src[1]
455             list(title=gsub("[^#]*#\\s*(.*)","\\1",src[1],perl=TRUE))           if(!is.character(first))return(list())
456           } else list()           if(!grepl("#",first))return(list())
457             list(title=gsub("[^#]*#\\s*(.*)","\\1",first,perl=TRUE))
458         },         },
459         ## PhG: it is tests/FUN.R!!! I would like more flexibility here         ## PhG: it is tests/FUN.R!!! I would like more flexibility here
460         ## please, let me choose which dir to use for examples!         ## please, let me choose which dir to use for examples!
# Line 511  Line 512 
512             }             }
513             list(examples = ex)             list(examples = ex)
514           } else list()           } else list()
515         },         }
516         collapse.docs=function(doc,...){         )
          list(doc=lapply(doc,paste,collapse="\n"))  
        })  
517    
518  ### List of parser functions that operate on single objects. This list  ### List of parser functions that operate on single objects. This list
519  ### is useful for testing these functions.  ### is useful for testing these functions.
520  lonely <- c(forall.parsers,forfun.parsers)  lonely <- structure(c(forall.parsers,forfun.parsers),ex=function(){
 attr(lonely,"ex") <- function(){  
521    f <- function # title    f <- function # title
522  ### description  ### description
523    (x, ##<< arg x    (x, ##<< arg x
# Line 535  Line 533 
533    src <- attr(f,"source")    src <- attr(f,"source")
534    lonely$extract.xxx.chunks(src)    lonely$extract.xxx.chunks(src)
535    lonely$prefixed.lines(src)    lonely$prefixed.lines(src)
536  }  })
537    
538  extra.code.docs <- function # Extract documentation from code chunks  extra.code.docs <- function # Extract documentation from code chunks
539  ### Parse R code to extract inline documentation from comments around  ### Parse R code to extract inline documentation from comments around
# Line 573  Line 571 
571                    pattern <- "^([^\\.]+)\\.(.*)$"                    pattern <- "^([^\\.]+)\\.(.*)$"
572            doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),            doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),
573                m2 <- gsub(pattern,"\\2",on,perl=TRUE))                m2 <- gsub(pattern,"\\2",on,perl=TRUE))
574            if ( 0 < length(grep("\\W",m1,perl=TRUE)) ){            if ( grepl("\\W",m1,perl=TRUE) ){
575                            m1 <- paste("`",m1,"`",sep="")                            m1 <- paste("`",m1,"`",sep="")
576            }            }
577            cat("S3method(",m1,",",m2,")\n",sep="")            cat("S3method(",m1,",",m2,")\n",sep="")
# Line 597  Line 595 
595        NULL        NULL
596      } else if(0 == length(res) && "function" %in% class(o)      } else if(0 == length(res) && "function" %in% class(o)
597                && 1 == length(osource <- attr(o,"source"))                && 1 == length(osource <- attr(o,"source"))
598                && 1 == length(grep(paste("UseMethod(",on,")",sep="\""),osource))                && grepl(paste("UseMethod(",on,")",sep="\""),osource)
599                ){                ){
600        ## phew - this should only pick up R.oo S3 generic definitions like:        ## phew - this should only pick up R.oo S3 generic definitions like:
601        ## attr(*, "source")= chr "function(...) UseMethod(\"select\")"        ## attr(*, "source")= chr "function(...) UseMethod(\"select\")"
# Line 658  Line 656 
656  default.parsers <-  default.parsers <-
657    c(extra.code.docs=extra.code.docs, ## TODO: cleanup!    c(extra.code.docs=extra.code.docs, ## TODO: cleanup!
658      sapply(forfun.parsers,forfun),      sapply(forfun.parsers,forfun),
659        sapply(forall.parsers,forall),
660      edit.package.file=function(desc,...){      edit.package.file=function(desc,...){
661        in.details <- setdiff(colnames(desc),"Description")        in.details <- setdiff(colnames(desc),"Description")
662        details <- sprintf("%s: \\tab %s\\cr",in.details,desc[,in.details])        details <- sprintf("%s: \\tab %s\\cr",in.details,desc[,in.details])
663        L <-        L <-
664          list(list(title=desc[,"Title"],          list(list(title=desc[,"Title"],
665                    description=desc[,"Description"],                    description=desc[,"Description"],
666                    `tabular{ll}`=details,                    `tabular{ll}`=details))
                   author=desc[,"Maintainer"]))  
667        names(L) <- paste(desc[,"Package"],"-package",sep="")        names(L) <- paste(desc[,"Package"],"-package",sep="")
668        L        L
669      },      }
     sapply(forall.parsers,forall)  
670      )      )
671    
672  setClass("DocLink", # Link documentation among related functions  setClass("DocLink", # Link documentation among related functions
# Line 710  Line 707 
707      ## "prefix" lines will be used instead.      ## "prefix" lines will be used instead.
708      default.description <- NULL      default.description <- NULL
709      while ( start > last.end+1      while ( start > last.end+1
710             && 1 == length(grep(prefix,code[start-1],perl=TRUE)) ){             && grepl(prefix,code[start-1],perl=TRUE) ){
711        start <- start-1        start <- start-1
712      }      }
713      if ( start < chunks[[k]][1] ){      if ( start < chunks[[k]][1] ){
# Line 737  Line 734 
734        ## If the function definition is not embedded within the call, then        ## If the function definition is not embedded within the call, then
735        ## the parent is that function. Test whether the the third value        ## the parent is that function. Test whether the the third value
736        ## looks like a name and add it to parents if so.        ## looks like a name and add it to parents if so.
737        if ( 1 == length(grep("^[\\._\\w]+$",chars[3],perl=TRUE)) ){        if ( grepl("^[\\._\\w]+$",chars[3],perl=TRUE) ){
738          parent <- chars[3]          parent <- chars[3]
739        }        }
740        res[[object.name]] <- new("DocLink",name=object.name,        res[[object.name]] <- new("DocLink",name=object.name,
# Line 769  Line 766 
766        ## If the function definition is not embedded within the call, then        ## If the function definition is not embedded within the call, then
767        ## the parent is that function. Test whether the the fourth value        ## the parent is that function. Test whether the the fourth value
768        ## looks like a name and add it to parents if so.        ## looks like a name and add it to parents if so.
769        if ( 1 == length(grep("^[\\._\\w]+$",chars[4],perl=TRUE)) ){        if ( grepl("^[\\._\\w]+$",chars[4],perl=TRUE) ){
770          parent <- c(chars[4],parent)          parent <- c(chars[4],parent)
771        }        }
772        res[[object.name]] <- new("DocLink",name=object.name,        res[[object.name]] <- new("DocLink",name=object.name,
# Line 895  Line 892 
892      L <- p(code=code,objs=objs,docs=docs,...)      L <- p(code=code,objs=objs,docs=docs,...)
893      docs <- combine(docs,L)      docs <- combine(docs,L)
894    }    }
895      ## post-process to collapse all character vectors
896      for(i in seq_along(docs)){
897        for(j in seq_along(docs[[i]])){
898          docs[[i]][[j]] <- paste(docs[[i]][[j]],collapse="\n")
899        }
900      }
901    if(verbose)cat("\n")    if(verbose)cat("\n")
902    docs    docs
903  ### A list of extracted documentation from code.  ### A list of extracted documentation from code.

Legend:
Removed from v.186  
changed lines
  Added in v.196

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