R/list.R

Defines functions list_in_order checkbox checkbox1 checked unchecked get_quote get_url get_url_title

Documented in checkbox get_quote get_url get_url_title list_in_order

#' Contents in an ordered list
#' @param input NULL
#' @return Character.
#' @author Jiaxiang Li
#'
#' @importFrom stringr str_trim
#' @importFrom glue glue
#'
list_in_order <- function(input = NULL) {
    input <- get_input(input, collapse = FALSE)
    text <- stringr::str_trim(input)
    text <- glue_lines(text, glue_obj = glue::glue("1. {text}"))
    insert_lines(text)
    write_bottom_line(text)
}

#' Contents in a todo list
#'
#' @param input NULL
#' @return Character.
#' @author Jiaxiang Li
#' @importFrom rstudioapi getSourceEditorContext
checkbox <- function(input = NULL) {
    if (rstudioapi::getSourceEditorContext()$selection[[1]]$text %>%
        str_detect(unchecked_pattern)) {
        checked()
    } else if (rstudioapi::getSourceEditorContext()$selection[[1]]$text %>%
               str_detect(checked_pattern)) {
        unchecked()
    } else {
        checkbox1()
    }
}
#' @importFrom stringr str_trim
#' @importFrom glue glue
checkbox1 <- function(input = NULL) {
    input <- get_input(input, collapse = FALSE)
    text <- stringr::str_trim(input)
    text <- glue_lines(text, glue_obj = glue::glue("- [ ] {text}"))
    insert_lines(text)
    write_bottom_line(text)
}
checked <- function() {
    text <- rstudioapi::getSourceEditorContext()
    new_text <-
        text$selection[[1]]$text %>% str_replace_all(unchecked_pattern, checked_pattern)
    rstudioapi::insertText(location = text$selection[[1]]$range,
                           text = new_text)
}
checked_pattern <- "- \\[x\\]"
unchecked_pattern <- "- \\[ \\]"
unchecked <- function() {
    text <- rstudioapi::getSourceEditorContext()
    new_text <-
        text$selection[[1]]$text %>% str_replace_all(checked_pattern, unchecked_pattern)
    rstudioapi::insertText(location = text$selection[[1]]$range,
                           text = new_text)
}

#' Contents in a quote
#'
#' @param input character.
#' @return Character.
#' @author Jiaxiang Li
#'
#' @importFrom stringr str_trim
#' @importFrom glue glue
#'
get_quote <- function(input = NULL) {
    input <- get_input(input, collapse = FALSE)
    text <- stringr::str_trim(input)
    text <- glue_lines(text, glue_obj = glue::glue("> {text}"))
    insert_lines(text)
    write_bottom_line(text)
}

#' Contents in a URL
#' @param input Character.
#' @param name Character.
#' @return Character.
#' @author Jiaxiang Li
#'
#' @importFrom stringr str_trim
#' @importFrom glue glue
#'
get_url <- function(input = NULL, name = NULL) {
    input <- get_input(input, collapse = FALSE)
    text <- stringr::str_trim(input)
    name <- purrr::map_chr(text, get_url_title)

    text <-
        glue_lines(text, glue_obj = glue::glue("[{name}]({text})"))
    insert_lines(text)
    write_bottom_line(text)
}

#' Get contents in customized name
#' @param url character.
#' @importFrom stringr str_detect str_extract
get_url_title <- function(url) {
    name <-
        if (stringr::str_detect(url, 'github.io')) {
            'Github Pages'
        } else if (stringr::str_detect(url, 'github') &&
                   stringr::str_detect(url, 'issues')) {
            'Github Issue'
        } else if (stringr::str_detect(url, 'github')) {
            'GitHub'
        } else if (stringr::str_detect(url, 'community') &&
                   str_detect(url, 'rstudio')) {
            'RStudio Community'
        } else if (stringr::str_detect(url, 'stackoverflow')) {
            'Stack Overflow'
        } else if (stringr::str_detect(url, 'datacamp')) {
            'DataCamp'
        } else if (stringr::str_detect(url, 'weixin')) {
            'WeChat Article'
            # non-ASCII characters use unicode
            # https://cran.r-project.org/doc/manuals/R-exts.html#Encoding-issues
        } else if (stringr::str_detect(url, web_link)) {
            stringr::str_extract(url, web_link)
        } else if (stringr::str_detect(url, 'netlify')) {
            'Netlify Blog'
        } else if (stringr::str_detect(url, '\\.csv$')) {
            'Dataset'
        } else if (stringr::str_detect(url, 'yinxiang|evernote')) {
            'Evernote'
        } else if (stringr::str_detect(url, 'wikipedia')) {
            'Wikipedia'
        } else if (stringr::str_detect(url, 'youku')) {
            'Youku Video'
        } else {
            'notes'
        }
    return(name)
}
JiaxiangBU/add2md documentation built on Jan. 31, 2020, 7:46 p.m.