[rkward] packages/rkwarddev: fixed the indentation issue and added better control over indentation in js()

m.eik michalke meik.michalke at uni-duesseldorf.de
Mon Nov 9 20:11:56 UTC 2015


Git commit f2ff07f1077223cb8c756ce96193dd30b3f27ec7 by m.eik michalke.
Committed on 09/11/2015 at 20:08.
Pushed by meikm into branch 'master'.

fixed the indentation issue and added better control over indentation in js()

  - you can now define the indentation character that is used inside js()
  - added the ability to also print empty "else" statements
  - cleaned up rk.paste.JS.graph()
  - the generated code looks pretty good now :-)

M  +3    -1    packages/rkwarddev/ChangeLog
M  +9    -6    packages/rkwarddev/R/js.R
M  +13   -13   packages/rkwarddev/R/rk.paste.JS.graph.R
M  +9    -1    packages/rkwarddev/man/js.Rd

http://commits.kde.org/rkward/f2ff07f1077223cb8c756ce96193dd30b3f27ec7

diff --git a/packages/rkwarddev/ChangeLog b/packages/rkwarddev/ChangeLog
index 7f84404..27b6d06 100644
--- a/packages/rkwarddev/ChangeLog
+++ b/packages/rkwarddev/ChangeLog
@@ -1,6 +1,6 @@
 ChangeLog for package rkwarddev
 
-changes in version 0.07-4 (2015-11-08)
+changes in version 0.07-4 (2015-11-09)
 unreleased:
   - this version is under development
 fixed:
@@ -12,6 +12,8 @@ fixed:
   - rk.XML.dependencies() didn't know about min/max versions of pluginmaps
     (fixed in rk.XML.about() as well)
   - some fixes to the skeleton GUI and demo script
+  - prevent internal function indent() from losing it over negative values
+  - indentation of rk.paste.JS.graph() was incorrect
 added:
   - new methods plugin2script() to try to translate raw plugin XML into
     working rkwarddev script calls
diff --git a/packages/rkwarddev/R/js.R b/packages/rkwarddev/R/js.R
index 334230c..fe127d5 100644
--- a/packages/rkwarddev/R/js.R
+++ b/packages/rkwarddev/R/js.R
@@ -35,7 +35,10 @@
 #'   and/or objects of classes \code{rk.JS.arr} or \code{rk.JS.opt}, simply separated by comma.
 #'   JavaScript operators and \code{if} conditions will be kept as-is.
 #' @param level Integer value, first indetation level.
+#' @param indent.by A character string defining the indentation string to use.
 #' @param linebreaks Logical, should there be line breaks between the elements in this call?
+#' @param empty.e For \code{if} conditions only: Logical, if \code{TRUE} will force to add empty \code{else \{\}} brackets when
+#'    there is no \code{else} statement defined, which is considered to enhance code readability by some.
 #' @return A character string.
 #' @export
 #' @seealso \code{\link[rkwarddev:rk.JS.vars]{rk.JS.vars}},
@@ -55,11 +58,11 @@
 #'   }
 #' )))
 
-js <- function(..., level=2, linebreaks=FALSE){
+js <- function(..., level=2, indent.by="\t", linebreaks=FALSE, empty.e=FALSE){
   full.content <- eval(substitute(alist(...)))
 
   if(isTRUE(linebreaks)){
-    collapse <- paste0("\n", paste0(rep("\t", max(0, level-1)), collapse=""))
+    collapse <- paste0("\n", indent(level=level, by=indent.by))
   } else {
     collapse <- ""
   }
@@ -76,19 +79,19 @@ js <- function(..., level=2, linebreaks=FALSE){
       if(is.call(this.part)){
         # recursively check for if conditions
         if(inherits(this.part, "if")){
-          this.part <- replaceJSIf(this.part, level=level)
+          this.part <- replaceJSIf(this.part, level=level, indent.by=indent.by, empty.e=empty.e)
         } else {}
         if(inherits(this.part, "for")){
-          this.part <- replaceJSFor(this.part, level=level)
+          this.part <- replaceJSFor(this.part, level=level, indent.by=indent.by)
         } else {}
         if(identical(this.part[[1]], "rk.comment")){
-          return(rk.paste.JS(eval(this.part), level=level))
+          return(rk.paste.JS(eval(this.part), level=level, indent.by=indent.by, empty.e=empty.e))
         } else {}
         # replace JS operators
         return(do.call("replaceJSOperators", args=list(this.part)))
       } else if(is.XiMpLe.node(this.part)){
         if(XMLName(this.part) == "!--"){
-          return(rk.paste.JS(this.part, level=level))
+          return(rk.paste.JS(this.part, level=level, indent.by=indent.by, empty.e=empty.e))
         } else {
           return(this.part)
         }
diff --git a/packages/rkwarddev/R/rk.paste.JS.graph.R b/packages/rkwarddev/R/rk.paste.JS.graph.R
index e2dc3b7..1956773 100644
--- a/packages/rkwarddev/R/rk.paste.JS.graph.R
+++ b/packages/rkwarddev/R/rk.paste.JS.graph.R
@@ -72,7 +72,7 @@ rk.paste.JS.graph <- function(..., plotOpts=NULL, printoutObj=NULL, level=2, ind
         js.po.preprocess <- rk.JS.vars(plotOpts, modifiers="code.preprocess", check.modifiers=FALSE)
         js.po.calculate <- rk.JS.vars(plotOpts, modifiers="code.calculate", check.modifiers=FALSE)
       }
-      rk.paste.JS(
+      trim(rk.paste.JS(
         rk.comment("in case there are generic plot options defined:"),
         js.po.preprocess,
         if(!is.null(printoutObj)){
@@ -81,17 +81,17 @@ rk.paste.JS.graph <- function(..., plotOpts=NULL, printoutObj=NULL, level=2, ind
           warning("rk.paste.JS.graph: you're using plot options, but 'printoutObj' is empty, is that intended?")
         },
         js.po.calculate,
-        level=level+1
-      )
+        level=level
+      ))
     } else {},
-    level=max(0, level-1), indent.by=indent.by, empty.e=empty.e
+    level=level, indent.by=indent.by, empty.e=empty.e
   )
 
   # graph.on() & begin try()
   js.prnt <- paste(js.prnt, rk.paste.JS(
     ite("full", echo("rk.graph.on()\n")),
     echo("\ttry({\n"),
-    level=level+1, indent.by=indent.by, empty.e=empty.e
+    level=level, indent.by=indent.by, empty.e=empty.e
   ), sep="\n\n")
 
   # plot options: preprocess
@@ -100,20 +100,20 @@ rk.paste.JS.graph <- function(..., plotOpts=NULL, printoutObj=NULL, level=2, ind
       rk.paste.JS(
         rk.comment("insert any option-setting code that should be run before the actual plotting commands:"),
         id(paste0(
-          paste0(rep("\t", level+1),collapse=""),
+          indent(level=level, by=indent.by),
           "printIndentedUnlessEmpty(\""), plotOptsIndent, "\", ", js.po.preprocess, ", \"\\n\", \"\");"
         ),
-        level=level+1
+        level=level
       )
     } else {},
-    level=level+1, indent.by=indent.by, empty.e=empty.e
+    level=level, indent.by=indent.by, empty.e=empty.e
   ), sep="\n\n")
 
   # here comes the plot
   js.prnt <- paste(js.prnt, rk.paste.JS(
     rk.comment("the actual plot:"),
     rk.paste.JS(..., level=level, indent.by=indent.by, empty.e=empty.e),
-    level=level+1, indent.by=indent.by, empty.e=empty.e
+    level=level, indent.by=indent.by, empty.e=empty.e
   ), sep="\n\n")
 
   # plot options: postprocess
@@ -122,20 +122,20 @@ rk.paste.JS.graph <- function(..., plotOpts=NULL, printoutObj=NULL, level=2, ind
       rk.paste.JS(
         rk.comment("insert any option-setting code that should be run after the actual plot:"),
         id(paste0(
-          paste0(rep("\t", level+1),collapse=""),
+          indent(level=level, by=indent.by), 
           "printIndentedUnlessEmpty(\""), plotOptsIndent, "\", ", js.po.calculate, ", \"\\n\", \"\");"
         ),
-        level=level+1
+        level=level
       )
     } else {},
-    level=level+1, indent.by=indent.by, empty.e=empty.e
+    level=level, indent.by=indent.by, empty.e=empty.e
   ), sep="\n\n")
 
   # end try() & graph.off()
   js.prnt <- paste(js.prnt, rk.paste.JS(
     echo("\n\t})\n"),
     ite("full", echo("rk.graph.off()\n")),
-    level=level+1, indent.by=indent.by, empty.e=empty.e
+    level=level, indent.by=indent.by, empty.e=empty.e
   ), sep="\n\n")
 
   return(js.prnt)
diff --git a/packages/rkwarddev/man/js.Rd b/packages/rkwarddev/man/js.Rd
index 0ae37be..5cf76cd 100644
--- a/packages/rkwarddev/man/js.Rd
+++ b/packages/rkwarddev/man/js.Rd
@@ -4,7 +4,8 @@
 \alias{js}
 \title{R to JavaScript translation}
 \usage{
-js(..., level = 2, linebreaks = FALSE)
+js(..., level = 2, indent.by = "\\t", linebreaks = FALSE,
+  empty.e = FALSE)
 }
 \arguments{
 \item{...}{One or several character strings and/or \code{XiMpLe.node} objects with plugin nodes,
@@ -13,7 +14,14 @@ JavaScript operators and \code{if} conditions will be kept as-is.}
 
 \item{level}{Integer value, first indetation level.}
 
+\item{indent.by}{A character string defining the indentation string to use.}
+
 \item{linebreaks}{Logical, should there be line breaks between the elements in this call?}
+
+\item{empty.e}{For \code{if} conditions only: Logical,
+      if \code{TRUE} will force to add empty \code{else \{\}} brackets when
+there is no \code{else} statement defined,
+      which is considered to enhance code readability by some.}
 }
 \value{
 A character string.



More information about the rkward-tracker mailing list