Skip to content

Commit

Permalink
compitable with both the old and new versions of mindmaps. new functi…
Browse files Browse the repository at this point in the history
…on dir2()
  • Loading branch information
pzhaonet committed Sep 28, 2018
1 parent b1e1f91 commit 0109d78
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 4 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(dir2)
export(markmap)
export(markmapOption)
export(markmapOutput)
Expand Down
71 changes: 70 additions & 1 deletion R/foo.R
Expand Up @@ -49,14 +49,21 @@ 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 '</node>'
mm <- gsub(pattern = '/>', '</node>', 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('<node', mm))
node_end <- unlist(gregexpr('</node', mm))
node_sign <- c(rep(1, length(node_begin)), rep(-1, length(node_end)))
node_loc <- c(node_begin, node_end)
node_sign <- node_sign[order(node_loc)]
node_level <- cumsum(node_sign)[node_sign == 1]
headers <- gsub('TEXT="([^>]*)">', '\\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) {
Expand Down Expand Up @@ -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 <- '<map version="1.0.1">'
mm[2] <- paste0('<node TEXT="', title, '">', paste0(rep('<node TEXT="">', ncc[1] - 1), collapse = ''))
diffncc <- diff(ncc)
Expand Down Expand Up @@ -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' (<http://freemind.sourceforge.net/wiki/index.php/Main_Page>) and 'XMind' (<http://www.xmind.net>).
#' @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!'))}
}
2 changes: 1 addition & 1 deletion 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}
Expand Down
2 changes: 1 addition & 1 deletion inst/examples/mm/bookdownplus.mm
Expand Up @@ -2,7 +2,7 @@
<node TEXT="my title">
<node TEXT="Introduction ">
<node TEXT="What is 'bookdown' "></node>
<node TEXT="What is 'bookdownplus' "></node>
<node TEXT="What is 'bookdownplus' " LINK="https://github.com/pzhaonet/bookdownplus" ></node>
<node TEXT="Why 'bookdownplus' "></node>
<node TEXT="Giants' Shoulders "></node></node>
<node TEXT="Quick Start ">
Expand Down
27 changes: 27 additions & 0 deletions man/dir2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -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)`.
Expand Down

0 comments on commit 0109d78

Please sign in to comment.