R/gdoc2qmd.R

Defines functions gdoc2qmd

Documented in gdoc2qmd

#' Google docs to Rmarkdown
#'
#' Use Articul8 Add-ons from Google docs to build Rticles
#'
#' @param file Zip file path from Articul8 exported in md format [path]
#' @param export Path to export the files [path: NA (file directory)]
#' @param format Output format [string: "qmd" "rmd"]
#' @param type output file type [strig: "asis" "list", "listfull", "full"]
#'
#' @return path
#' 
#' @details 
#' 
#' Document rendering until certain point: "#| end"
#' Include for next page: "#| newpage"
#' You can include the cover page params using "#|" in a Google docs table
#' 
#' @export
#' 

gdoc2qmd <- function(file
                     , export = NA
                     , format = "qmd"
                     , type = "asis"
                     ){
  
  # file <- choose.files() ; format = "qmd"; export = NA
  # type <- "listfull"

  export <- if(is.na(export)) {
    file %>% gsub(".zip", "", .) %>% file.path()
  } else {export}
  
  zip <- file %>% 
    utils::unzip(overwrite = T, exdir = export)
  
# -------------------------------------------------------------------------
  
  text <-  zip %>% 
    .[grep('.md', .)] %>%
    readLines() %>% 
    tibble::enframe() %>%
    dplyr::filter(!grepl("^#+ $", .data$value)) %>%  
    dplyr::mutate(value = gsub("```Unknown element type at this position: UNSUPPORTED```"
                               , "\n\n", .data$value)) %>% 
    {
      if (any(grepl(pattern = '#\\| end', x = .$value))) {
        dplyr::slice(.data = ., 1:(which(x = .$value == '#| end')-1))
      } else { . }
    } %>% 
    mutate(across(.data$value, ~ gsub("#\\| newpage", "\\\\newpage", .))) %>% 
    dplyr::rowwise() 
  
  params <- text %>% 
    dplyr::filter(str_detect(.data$value, "\\| \\#\\|")) %>% 
    dplyr::mutate(across(everything(), ~gsub("\\| \\#\\|", "", .))) %>% 
    dplyr::mutate(across(.data$value, ~gsub("\\|$", "", .))) %>% 
    tidyr::separate(.data$value, c("name", "value"), sep = "\\|") %>% 
    dplyr::mutate(across(everything(), ~trimws(.))) %>% 
    tibble::deframe() %>% 
    as.list() 
  
  txt <- text %>% 
      {
        if(length(params) >0) {
          .[-(1:(max(which(grepl("\\| \\#\\|", .$value))))), ]
        } else {.}
      }

  txtonly <- txt %>% 
    dplyr::filter(!grepl("\\|", .data$value)) %>% 
    dplyr::filter(!grepl("#tbl", .data$value)) %>% 
    dplyr::filter(!grepl("#fig", .data$value))
  
  tt <- txtonly %>% 
    dplyr::filter(if_all(everything(), ~ . != "")) %>% 
    utils::head(1) %>% 
    tibble::deframe() %>% 
    as.vector()
  
  fig <- txt %>% 
    dplyr::filter(grepl("#fig", .data$value)) %>% 
    tibble::as_tibble() %>% 
    dplyr::group_split(.data$value) %>% 
    rev() %>% 
    purrr::map_dfr(~ add_row(.x, .before = grepl("#fig", .x))) %>% 
    {
      if(length(.) != 0) {
        dplyr::mutate(.data = ., across(.data$value, ~ ifelse(is.na(.), "\\newpage", .)))
      }  else {.}
    }
    
  
  figx <- fig %>% 
    dplyr::rowwise() %>% 
    {
      if(length(.) != 0) {
        dplyr::mutate(.data = ., across(.data$value, ~gsub("\\{#fig:(.*)\\}", paste0("{#fig:", .data$name ,"}"), .)))
      }  else {.}
    }
    
  figlist <- fig %>% 
    {
      if(length(.) != 0) {
        
        dplyr::mutate(.data = ., value = case_when(
          dplyr::row_number() == 1 ~ .data$value
          , grepl("#fig", .data$value) ~ .data$value
          , TRUE  ~ "\n\n"
        )) %>% 
          dplyr::mutate(.data = ., across(.data$value, ~gsub("\\]\\((.*)\\)\\{", "](){", .))) 
        
      }  else {.}
    }
  
  tab <- txt %>% 
    dplyr::filter(grepl("^\\|", .data$value) | grepl("#tbl", .data$value)) %>% 
    {
      if(nrow(. > 1)) { 
        
        dplyr::mutate(.data = ., group = case_when(
          grepl(pattern = "^:", x = .data$value) ~ as.character(.data$name)
          , TRUE ~ NA
        )) %>% 
          tibble::as_tibble(x = .) %>% 
          tidyr::fill(data = ., "group", .direction = "up", ) %>% 
          tidyr::drop_na(data = ., .data$group) %>% 
          dplyr::group_by(.data = ., .data$group) %>% 
          dplyr::slice(.data = ., n(), 1:(n() - 1)) %>% 
          dplyr::ungroup() %>% 
          dplyr::group_split(.tbl = ., .data$group) %>%
          purrr::map_dfr(~ add_row(.x, .after = grepl("#tbl", .x))) %>% 
          dplyr::mutate(.data = ., across(.data$value, ~ ifelse(is.na(.), "\\newpage", .))) %>% 
          dplyr::select(.data = ., !.data$group) %>% 
          dplyr::mutate(.data = ., across(.data$value, ~gsub("}", "}", .))) 
        
        } else { . }
    }  
    
  
  tabx <- tab %>% 
    dplyr::rowwise() %>% 
    dplyr::mutate(across(.data$value, ~gsub("\\{#tbl:(.*)\\}", paste0("{#tbl:", .data$name ,"}"), .)))
  
  tablist <- tab %>% 
    dplyr::filter(!grepl("\\|", .data$value)) %>% 
    dplyr::mutate(value = case_when(
      dplyr::row_number() == 1 ~ .data$value
      , grepl("#tbl", .data$value) ~ .data$value
      , TRUE  ~ "\n\n"
    )) 

# -------------------------------------------------------------------------

  manuscript <- if(type == "full") {
    
    dplyr::bind_rows(txtonly, tablist, figlist, tabx, figx)
    
  } else if (type == "listfull") {
    
    dplyr::bind_rows(txtonly, tab, fig)
    
  } else if (type == "list") {
    
    dplyr::bind_rows(txtonly, tablist, figlist)
    
  } else if (type == "asis") { txt } 
   
    
  doc <- manuscript %>% 
    {
      if(format == "qmd") {
        
        dplyr::mutate(.data = ., value = inti::figure2qmd(text = .data$value, path = export)) %>% 
        dplyr::mutate(.data = ., value = inti::table2qmd(text = .data$value, type))
        
      } else if (format == "rmd") {
        
        dplyr::mutate(.data = ., value = inti::figure2rmd(text = .data$value, path = export)) %>% 
        dplyr::mutate(.data = ., value = inti::table2rmd(text = .data$value)) 
        
      }
    } %>% 
    tibble::add_row(value = "\\newpage", .before = which(grepl("# abstract", .$value, ignore.case = TRUE))) %>% 
    tibble::add_row(value = "\\newpage", .before = which(grepl("# reference", .$value, ignore.case = TRUE))) %>% 
    tibble::add_row(value = "\\newpage", .before = which(grepl("# result", .$value, ignore.case = TRUE)))  %>% 
    tibble::add_row(value = "\\newpage", .before = which(grepl("# discussion", .$value, ignore.case = TRUE))) %>% 
    {
      if (any(grepl(pattern = '# abstract', x = .$value, ignore.case = TRUE))) {
        tibble::add_row(.data = ., value = paste(tt, sep = "\n\n"),
                        .before = which(x = grepl(pattern = "# abstract", x = .$value, ignore.case = TRUE)))
      } else {.}
    } %>%
    dplyr::select(.data$value) %>% 
    # tibble::add_row(value = "\n```{r}\nknitr::knit_exit() \n```", ) %>%
    tibble::deframe() %>% 
    writeLines(con = file.path(export, "_doc.Rmd") %>% gsub("\\\\", "\\/", .))


  # -------------------------------------------------------------------------

  doc <- list.files(path = export, pattern = "_doc", full.names = T)
  
  
  return(list(path = doc
              , params = params))
  
}

Try the inti package in your browser

Any scripts or data that you put into this service are public.

inti documentation built on Oct. 27, 2023, 9:06 a.m.