R/str_to_x.R

Defines functions change_to_upper naming_dir get_word_style center build_iframe

Documented in build_iframe center change_to_upper get_word_style naming_dir

#' Change text into uppercase
#'
#' This function helps users to change text into uppercase by the addins instead of the functions.
#'
#' @param input NULL
#' @return Character.
#' @author Jiaxiang Li
#'
#' @importFrom  rstudioapi insertText
#' @importFrom  stringr str_to_upper
change_to_upper <- function(input = NULL) {
    input <- get_input(input)
    text <-
        input %>%
        stringr::str_to_upper()
    insert_lines(text)
    write_bottom_line(text)
}
#' Change text into good directry name
#' @param input character.
#' @param sep a character string to separate the terms. Not NA_character_.
#' @param is_paste Logical. Whether or not to paste the text on the clipboard.
#' @importFrom stringr str_to_lower str_trim str_replace_all
#' @importFrom clipr write_clip
#' @return The directory name.
#' @export
#' @examples
#' naming_dir("FEATURE ENGINEERING FOR NLP IN PYTHON", is_paste = FALSE)
naming_dir <-
    function(input = "FEATURE ENGINEERING FOR NLP IN PYTHON",
             sep = "_", is_paste = TRUE) {
        text <- input %>%
            stringr::str_to_lower() %>%
            stringr::str_trim() %>%
            stringr::str_replace_all("[[:punct:][:blank:]]+", sep)
        if (is_paste == TRUE) {
            text %>%
                clipr::write_clip(allow_non_interactive = TRUE)
        } else {
            return(text)
        }
    }


#' Change text into HTML style
#' @param style Character.
#' @return Character.
#' @author Jiaxiang Li
#'
get_word_style <- function(style = 'kbd') {
    input <- get_input(input = input, collapse = FALSE)
    text <- get_html(input, style = style)
    insert_lines(text)
    write_bottom_line(text)
}
#' Change text into keyboard
#' @param ... Other params in get_word_style
kbd <- purrr::partial(get_word_style, style = 'kbd')
#' Change text into button
#' @param ... Other params in get_word_style
button <- purrr::partial(get_word_style, style = 'button')
#' Change text into mark
#' @param ... Other params in get_word_style
mark <- purrr::partial(get_word_style, style = 'mark')
#' Change text center
#' @param input default \code{NULL}.
#' @param size default 4.
#' @importFrom glue glue
center <- function(input = NULL, size = 4) {
    input <- get_input(input = input, collapse = FALSE)
    text <- glue::glue('<h{size} align="center">{input}</h{size}>')
    insert_lines(text)
    write_bottom_line(text)
}
#' Outout the iframe syntax for a URL.
#' @param input Character. URL.
#' @param is_nbviewer Logical. Whether the url is Jupyter notebook.
#' @param is_paste Logical. Whether or not to paste the text on the clipboard.
#' @importFrom glue glue
#' @importFrom clipr write_clip
#' @importFrom usethis proj_path
#' @export
#' @examples
#' \donttest{build_iframe("https://www.r-project.org/", is_paste = FALSE)}
build_iframe <- function(input = NULL,
                         is_nbviewer = FALSE, is_paste = TRUE) {
    input <- get_input(input = input, collapse = FALSE)
    if (is_nbviewer == TRUE) {
        input = file.path(
            "https://nbviewer.jupyter.org/urls",
            "jiaxiangbu.github.io",
            usethis::proj_path() %>% basename(),
            input
        )
    }
    text <-
        glue::glue('<iframe src="{input}" width="100%" height="800px"></iframe>')
    if (is_paste == TRUE) {
        clipr::write_clip(text, allow_non_interactive = TRUE)
    }
    return(text)
}
JiaxiangBU/add2md documentation built on Jan. 31, 2020, 7:46 p.m.