[education/rkward/devel/workspace_output] rkward/rbackend/rpackages/rkward/R: Add function for conversion of legacy output file (fixing some long-standing quirks along the way)

Thomas Friedrichsmeier null at kde.org
Sat Mar 5 21:44:26 GMT 2022


Git commit 567d7a6f5302b90d3f165d2c8b3f99858f42da56 by Thomas Friedrichsmeier.
Committed on 05/03/2022 at 21:43.
Pushed by tfry into branch 'devel/workspace_output'.

Add function for conversion of legacy output file (fixing some long-standing quirks along the way)

M  +4    -5    rkward/rbackend/rpackages/rkward/R/rk.filename-functions.R
M  +30   -0    rkward/rbackend/rpackages/rkward/R/rk.output.R

https://invent.kde.org/education/rkward/commit/567d7a6f5302b90d3f165d2c8b3f99858f42da56

diff --git a/rkward/rbackend/rpackages/rkward/R/rk.filename-functions.R b/rkward/rbackend/rpackages/rkward/R/rk.filename-functions.R
index 8c72f880..f09ff7fa 100644
--- a/rkward/rbackend/rpackages/rkward/R/rk.filename-functions.R
+++ b/rkward/rbackend/rpackages/rkward/R/rk.filename-functions.R
@@ -225,15 +225,14 @@
 	for (line in lines) {
 		slines <- strsplit (line, "<")[[1]]
 		for (sline in slines) {
-			sline <- toupper (sline)
-			if (substring (sline, 0, 3) == "IMG") {
-				parts <- strsplit (sline, "SRC")[[1]]
+			if (substring (toupper(sline), 0, 3) == "IMG") {
+				parts <- strsplit (sline, "[Ss][Rr][Cc]")[[1]]
 				if (length (parts) < 2) next
 				parts <- strsplit (parts[2], "\"")[[1]]
 				if (length (parts) < 2) next
 				files <- c (files, parts[2])
-			} else if (substring (sline, 0, 6) == "OBJECT") {
-				parts <- strsplit (sline, "DATA")[[1]]
+			} else if (substring (toupper(sline), 0, 6) == "OBJECT") {
+				parts <- strsplit (sline, "[Dd][Aa][Tt][Aa]")[[1]]
 				if (length (parts) < 2) next
 				parts <- strsplit (parts[2], "\"")[[1]]
 				if (length (parts) < 2) next
diff --git a/rkward/rbackend/rpackages/rkward/R/rk.output.R b/rkward/rbackend/rpackages/rkward/R/rk.output.R
index 445d5d6c..8a419aa1 100644
--- a/rkward/rbackend/rpackages/rkward/R/rk.output.R
+++ b/rkward/rbackend/rpackages/rkward/R/rk.output.R
@@ -21,6 +21,8 @@
 #'              \code{rk.output} can be used to create or load output files, as well as to obtain a reference to an already loaded output file. After that, use the class methods
 #'              to operate on the reference obtained.
 #'
+#'              \code{rk.import.legacy.output} can be used to import an legacy (pre 0.7.3) output file to .rko-format. This function is going to be removed after some time.
+#'
 #' @param filename The location where an existing output directory is loaded from or saved/exported to. Note that this is usually not the same location where functions such as
 #'                 \link{rk.print} will write to (these operate on a temporary "working copy", but rather the target directory, where changes should eventually be saved
 #'                 back, to.
@@ -122,3 +124,31 @@ Do not write anything to the target filename, directly! This is purely for infor
 	if (all) ret
 	else ret[[1]]
 }
+
+#' @export
+#' @rdname RK.Output
+"rk.import.legacy.output" <- function(filename=file.path(rk.home(), "rk_out.html"), import=TRUE, delete=FALSE) {
+	f <- filename
+	stopifnot(file.exists(f))
+	files <- rkward:::.rk.get.images.in.html.file(f)
+	css <- file.path(rk.home(), "rk_out.css")
+	if (file.exists(css)) files <- c(files, css)
+
+	if(import) {
+		out <- rk.output(create=TRUE)
+		out$activate()
+		wd = out$.workingDir()   # Don't do this at home, please. For internal use, only, and might change
+
+		stopifnot(file.copy(f, file.path(wd, "index.html"), overwrite=TRUE))
+		stopifnot(all(file.copy(files, file.path(wd, basename(files)), overwrite=TRUE)))
+		wd$save(save_filename)
+		out$view()
+
+		rk.show.message("The legacy output file has been imported. If satisfied with the result, you now save it in the new format. To remove the old output file, run rk.import.legacy.output(import=FALSE,delete=TRUE)")
+	}
+
+	if (delete) {
+		unlink(f)
+		unlink(files)
+	}
+}


More information about the rkward-tracker mailing list