From 0109d784422ea125b730371e689c90b26657b6a2 Mon Sep 17 00:00:00 2001 From: Peng Zhao Date: Fri, 28 Sep 2018 23:10:11 +0200 Subject: [PATCH] compitable with both the old and new versions of mindmaps. new function dir2() --- NAMESPACE | 1 + R/foo.R | 71 ++++++++++++++++++++++++++++++- inst/examples/md/bookdownplus1.md | 2 +- inst/examples/mm/bookdownplus.mm | 2 +- man/dir2.Rd | 27 ++++++++++++ readme.md | 2 +- 6 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 man/dir2.Rd diff --git a/NAMESPACE b/NAMESPACE index 5416c08..617dadd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(dir2) export(markmap) export(markmapOption) export(markmapOutput) diff --git a/R/foo.R b/R/foo.R index 138d3ac..46b526e 100644 --- a/R/foo.R +++ b/R/foo.R @@ -49,6 +49,13 @@ mm2md <- function(folder = 'mm', if (dir.exists(folder)) { mm <- NULL for (filename in dir(folder, full.names = TRUE)) mm <- c(mm, readLines(filename, encoding = 'UTF-8')) + # compitable for both versions: node ends with '/>' or '' + mm <- gsub(pattern = '/>', '', mm) + # keep links + loc_link <- grep('LINK="([^\"]*)"', mm) + links <- gsub('LINK="([^\"]*)"', '\\1', regmatches(mm, gregexpr('LINK="[^\"]*"', mm))) + for(i in loc_link) mm[i] <- gsub('TEXT="([^"]*)"', paste0('TEXT=\"[\\1](', links[i], ')\"'), mm[i]) + mm <- paste0(mm, collapse = '') node_begin <- unlist(gregexpr('', '\\1', regmatches(mm, gregexpr('TEXT="[^>]*">', mm))[[1]]) + headers <- gsub('TEXT="([^"]*)"', '\\1', regmatches(mm, gregexpr('TEXT="[^"]*"', mm))[[1]]) md <- paste(sapply(node_level - 1, function(x) paste0(rep('#', x), collapse = '')), headers) md[1] <- paste('Title:', md[1]) if (savefile) { @@ -281,6 +288,11 @@ renderMarkmap <- function(expr, env = parent.frame(), quoted = FALSE) { mdtxt2mmtxt <- function(title = 'my title', mmtxt = '') { ncc <- sapply(mmtxt, function(x) nchar(strsplit(x, split = ' ')[[1]][1])) mmtext <- substr(mmtxt, ncc + 2, nchar(mmtxt)) + + # get the hyperlinks + which_link <- grep(pattern = '\\[.*](.*)', mmtext) + mmtext[which_link] <- gsub('\\((.*)\\)', '" LINK="\\1', mmtext[which_link]) + mmtext[which_link] <- gsub(pattern = '[][]*', replacement = '', mmtext[which_link]) mm <- '' mm[2] <- paste0('', paste0(rep('', ncc[1] - 1), collapse = '')) diffncc <- diff(ncc) @@ -335,3 +347,60 @@ tree2mm <- function(tree, savefilename <- paste0(savefilename, ifelse(backup & file.exists(paste0(savefilename, '.mm')), paste0('-', format(Sys.time(), '%Y-%m-%d-%H-%M-%S')), ''), '.mm') writeLines(text = mm, savefilename, useBytes = TRUE) } + + +#' Convert a folder structure into a mindmap. +#' +#' @param savefilename character. Valid when savefile == TRUE. +#' @param backup logical. Whether the existing target file, if any, should be saved as backup. +#' @param path character. the path of the folder. +#' @param output a file with the folder structure. +#' +#' @return a mindmap file, which can be viewed by common mindmap software, such as 'FreeMind' () and 'XMind' (). +#' @export +#' @examples +#' dir2(NA) +dir2 <- function(path = getwd(), + savefilename = 'mindr', + output = c('mm', 'txt', 'md'), + backup = TRUE) { + output <- match.arg(output) + if(is.na(path)) return(message('The path cannot be N!.')) + if (dir.exists(path)) { + if_files = FALSE + tree <- paste0('tree "', path, '" /A', ifelse(if_files, ' /f', '')) + mytree <- system(tree, intern = T) + if('txt' %in% output) { + if (backup & file.exists(paste0(savefilename, '.txt'))){ + message(paste0(savefilename, '.txt already exits.')) + savefilename <- paste0(savefilename, '-', format(Sys.time(), '%Y-%m-%d-%H-%M-%S')) + } + writeLines(mytree, paste0(savefilename, '.txt'), useBytes = TRUE) + message(paste(savefilename), '.txt is generated!') + } + md <- mytree[-(1:3)] + # headers <- sapply(mytree, function(x) strsplit(x, ifelse(grepl('+---', x), '+---', '\\---'))[[1]][2]) + # attributes(headers) <- NULL + md <- gsub(pattern = '\\+---', '# ', md) + md <- gsub(pattern = '\\\\---', '# ', md) + md <- gsub(pattern = '\\| ', '#', md) + md <- gsub(pattern = ' ', '#', md) + mm <- mdtxt2mmtxt(title = path, mmtxt = md) + if('md' %in% output){ + if (backup & file.exists(paste0(savefilename, '.md'))){ + message(paste0(savefilename, '.md already exits.')) + savefilename <- paste0(savefilename, '-', format(Sys.time(), '%Y-%m-%d-%H-%M-%S')) + } + writeLines(text = md, paste0(savefilename, '.md'), useBytes = TRUE) + message(paste(savefilename), '.md is generated!') + } + if('mm' %in% output){ + if (backup & file.exists(paste0(savefilename, '.mm'))){ + message(paste0(savefilename, '.mm already exits.')) + savefilename <- paste0(savefilename, '-', format(Sys.time(), '%Y-%m-%d-%H-%M-%S')) + } + writeLines(text = mm, paste0(savefilename, '.mm'), useBytes = TRUE) + message(paste(savefilename), '.mm is generated!') + } + } else {message(paste('The directory', path, 'does not exist!'))} +} diff --git a/inst/examples/md/bookdownplus1.md b/inst/examples/md/bookdownplus1.md index 949103f..82b7dc3 100644 --- a/inst/examples/md/bookdownplus1.md +++ b/inst/examples/md/bookdownplus1.md @@ -1,6 +1,6 @@ # Introduction {#introduction} ## What is 'bookdown' {#what_is__bookdown_} -## What is 'bookdownplus' {#what_is__bookdownplus_} +## What is ['bookdownplus'](https://github.com/pzhaonet/bookdownplus) {#what_is__bookdownplus_} ## Why 'bookdownplus' {#why__bookdownplus_} ## Giants' Shoulders {#giants__shoulders} # Quick Start {#quick_start} diff --git a/inst/examples/mm/bookdownplus.mm b/inst/examples/mm/bookdownplus.mm index 08d79b5..f233b20 100644 --- a/inst/examples/mm/bookdownplus.mm +++ b/inst/examples/mm/bookdownplus.mm @@ -2,7 +2,7 @@ - + diff --git a/man/dir2.Rd b/man/dir2.Rd new file mode 100644 index 0000000..6b22ea3 --- /dev/null +++ b/man/dir2.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/foo.R +\name{dir2} +\alias{dir2} +\title{Convert a folder structure into a mindmap.} +\usage{ +dir2(path = getwd(), savefilename = "mindr", output = c("mm", "txt", "md"), + backup = TRUE) +} +\arguments{ +\item{path}{character. the path of the folder.} + +\item{savefilename}{character. Valid when savefile == TRUE.} + +\item{output}{a file with the folder structure.} + +\item{backup}{logical. Whether the existing target file, if any, should be saved as backup.} +} +\value{ +a mindmap file, which can be viewed by common mindmap software, such as 'FreeMind' (\url{http://freemind.sourceforge.net/wiki/index.php/Main_Page}) and 'XMind' (\url{http://www.xmind.net}). +} +\description{ +Convert a folder structure into a mindmap. +} +\examples{ +dir2(NA) +} diff --git a/readme.md b/readme.md index 10157da..e3a8972 100644 --- a/readme.md +++ b/readme.md @@ -125,7 +125,7 @@ Still being developed. Feel free to give your feedback to me! ## Updates -- 2018-09-28. **v1.1.2**. Display .mm mind maps. +- 2018-09-28. **v1.1.2**. Display .mm mind maps. Keep hyperlinks. `dir2()` for creating mindmaps from folder structure. - 2018-04-17. **v1.1.1**. Support tribble dataframes. - 2017-07-19. **v1.1.0**. On CRAN. See [CHANGES IN mindr VERSION 1.1](https://github.com/pzhaonet/mindr/releases/tag/v1.1). - 2017-07-05. **v1.0.6**. [Rmarkmap](https://github.com/seifer08ms/Rmarkmap) added. Run `example(markmap)`.