From 1504accd874c48d8ad719a8be9347325604954b5 Mon Sep 17 00:00:00 2001 From: Peng Zhao Date: Tue, 9 Oct 2018 13:39:24 +0200 Subject: [PATCH] users can specify the root of a mind map by markup() --- R/foo.R | 48 +++++++++++++++++++++++++++++++++++------------- man/markmap.Rd | 7 +++++-- man/md2mm.Rd | 6 ++++-- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/R/foo.R b/R/foo.R index 7ec8b3e..e03a1b4 100644 --- a/R/foo.R +++ b/R/foo.R @@ -6,6 +6,7 @@ #' @param pattern an optional regular expression for filtering the input files. See `help(dir)`. #' @param savefilename character. Valid when savefile == TRUE. #' @param backup logical. Whether the existing target file, if any, should be saved as backup. +#' @param bookdown_style logical. whether the markdown files are in bookdown style, i.e. index.Rmd at the beginning, `# (PART)`, `# (APPENDIX)` and `# References` as an upper level of normal `#` title #' #' @return a mindmap file, which can be viewed by common mindmap software, such as 'FreeMind' () and 'XMind' (). #' @export @@ -13,14 +14,23 @@ #' folder <- system.file('examples/md', package = 'mindr') #' md2mm(folder = folder) #' md2mm(folder = folder, remove_curly_bracket = TRUE) -md2mm <- function(title = 'my title', +md2mm <- function(title = NA, folder = 'md', pattern = '*.[R]*md', remove_curly_bracket = FALSE, - savefilename = 'mindr', - backup = TRUE) { + savefilename = NA, + backup = TRUE, + bookdown_style = TRUE) { if (dir.exists(folder)) { - header <- outline(folder, pattern, remove_curly_bracket, savefile = FALSE) + header <- outline(folder = folder, + pattern = pattern, + remove_curly_bracket = remove_curly_bracket, + savefile = FALSE, + bookdown_style = bookdown_style) + foldername <- strsplit(folder, '[/\\]')[[1]] + foldername <- foldername[length(foldername)] + if(is.na(title)) title <- foldername + if(is.na(savefilename)) savefilename <- foldername mm <- mdtxt2mmtxt(title = title, mmtxt = header) # savefilename <- paste0(savefilename, ifelse(backup & file.exists(paste0(savefilename, '.mm')), paste0('-', format(Sys.time(), '%Y-%m-%d-%H-%M-%S')), ''), '.mm') if (backup & file.exists(paste0(savefilename, '.mm'))){ @@ -105,7 +115,6 @@ outline <- function(folder = 'md', backup = TRUE, bookdown_style = TRUE) { if (dir.exists(folder)) { - # an internal function to remove the code blocks from md rmvcode <- function(index, loc) { sum(index > loc[seq(1, length(loc), by = 2)] & @@ -139,11 +148,13 @@ outline <- function(folder = 'md', # lower the levels after `# (PART)` and `# (APPENDIX)` if(bookdown_style){ part_loc <- c(grep('^# \\(PART\\)', header), grep('^# \\(APPENDIX\\)', header), grep('^# References', header)) - header[part_loc] <- gsub(' \\(PART\\)', '', header[part_loc]) - header[part_loc] <- gsub(' \\(APPENDIX\\)', '', header[part_loc]) - lower_loc <- (part_loc[1] + 1) : length(header) - lower_loc <- lower_loc[!lower_loc %in% part_loc] - header[lower_loc] <- paste0('#', header[lower_loc]) + if(length(part_loc) > 0) { + header[part_loc] <- gsub(' \\(PART\\)', '', header[part_loc]) + header[part_loc] <- gsub(' \\(APPENDIX\\)', '', header[part_loc]) + lower_loc <- (part_loc[1] + 1) : length(header) + lower_loc <- lower_loc[!lower_loc %in% part_loc] + header[lower_loc] <- paste0('#', header[lower_loc]) + } } # save file @@ -167,6 +178,7 @@ outline <- function(folder = 'md', #' @param elementId character. #' @param options the markmap options #' @param input character, The format of theinput files +#' @param root character. a string displayed as the root of the mind map #' #' @import htmlwidgets #' @return A HTML widget object rendered from a given document. @@ -175,14 +187,24 @@ outline <- function(folder = 'md', #' folder <- system.file('examples/md', package = 'mindr') #' markmap(folder = folder) #' markmap(folder = folder, remove_curly_bracket = TRUE) -markmap <- function(input = c('.md', '.mm'), +markmap <- function(root = NA, + input = c('.md', '.mm'), folder = NA, remove_curly_bracket = FALSE, - width = NULL, height = NULL, elementId = NULL, options = markmapOption()) { + width = NULL, + height = NULL, + elementId = NULL, + options = markmapOption(), + bookdown_style = TRUE) { input <- match.arg(input) if(!is.na(folder) & dir.exists(folder)) { if(input == '.md'){ - header <- outline(folder, remove_curly_bracket = remove_curly_bracket, savefile = FALSE) + header <- outline(folder = folder, + remove_curly_bracket = remove_curly_bracket, + savefile = FALSE, + bookdown_style = bookdown_style) + header <- paste0('#', header) + header <- c(paste('#', ifelse(is.na(root), folder, root)), header) } else if(input == '.mm'){ header <- mm2md(folder = folder, savefile = FALSE) } else { diff --git a/man/markmap.Rd b/man/markmap.Rd index 5b352c1..29afc41 100644 --- a/man/markmap.Rd +++ b/man/markmap.Rd @@ -4,10 +4,13 @@ \alias{markmap} \title{Create a markmap widget} \usage{ -markmap(input = c(".md", ".mm"), folder = NA, remove_curly_bracket = FALSE, - width = NULL, height = NULL, elementId = NULL, options = markmapOption()) +markmap(root = NA, input = c(".md", ".mm"), folder = NA, remove_curly_bracket = FALSE, + width = NULL, height = NULL, elementId = NULL, options = markmapOption(), + bookdown_style = TRUE) } \arguments{ +\item{root}{character. a string displayed as the root of the mind map} + \item{input}{character, The format of theinput files} \item{folder}{character. The folder which contains the input file(s).} diff --git a/man/md2mm.Rd b/man/md2mm.Rd index 9081a94..7193dd4 100644 --- a/man/md2mm.Rd +++ b/man/md2mm.Rd @@ -4,8 +4,8 @@ \alias{md2mm} \title{Convert markdown or rmarkdown files to mindmap files.} \usage{ -md2mm(title = "my title", folder = "md", pattern = "*.[R]*md", - remove_curly_bracket = FALSE, savefilename = "mindr", backup = TRUE) +md2mm(title = NA, folder = "md", pattern = "*.[R]*md", remove_curly_bracket = FALSE, + savefilename = NA, backup = TRUE, bookdown_style = TRUE) } \arguments{ \item{title}{character. The title of the output file.} @@ -19,6 +19,8 @@ md2mm(title = "my title", folder = "md", pattern = "*.[R]*md", \item{savefilename}{character. Valid when savefile == TRUE.} \item{backup}{logical. Whether the existing target file, if any, should be saved as backup.} + +\item{bookdown_style}{logical. whether the markdown files are in bookdown style, i.e. index.Rmd at the beginning, \code{# (PART)}, \code{# (APPENDIX)} and \code{# References} as an upper level of normal \code{#} title} } \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}).