R/m1.R

Defines functions m1

Documented in m1

#' Build a url in markdown
#'
#' This function helps users to paste url in markdown
#'
#' @param size the figure size, by default, 100 x 100
#' @return Character.
#' @author Jiaxiang Li
#'
#' @import rstudioapi
#' @importFrom stringr str_split str_match str_detect
#' @importFrom glue glue
#' @importFrom rebus %R% or DOT one_or_more LOWER optional capture END

m1 <- function(size=100){

    line <-
        selected() %>%
        stringr::str_split(n=2,pattern=' ') %>%
        .[[1]]

    url <- line[1]
    name <- line[2]
    name <-
        if (!is.na(name)) {
            name
        } else if (str_detect(url,'github.io')) {
            'Github Pages'
        } else if (str_detect(url,'github') &&  str_detect(url,'issues')) {
            str_c('Github Issue',str_extract(url,'\\d+$'),sep  = ' ')
        } else if (str_detect(url,'community') &&  str_detect(url,'rstudio')) {
            'RStudio Community'
        } else if (str_detect(url,'stackoverflow')) {
            'Stack Overflow'
        } else if (str_detect(url,'datacamp')) {
            'DataCamp'
        } else if (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 (str_detect(url,web_link)) {
            str_extract(url,web_link)
        } else if (str_detect(url,'netlify')) {
            'Blog'
        } else if (str_detect(url,'\\.csv$')) {
            'dataset'
        } else if (str_detect(url,'yinxiang|evernote')) {
            'Evernote'
        } else if (str_detect(url,'wikipedia')) {
            'Wikipedia'

        # Add new here

        } else if (str_detect(url,'youku')) {
            'Youku Video'

        } else if (is.na(name)) {
            stringr::str_match(
                url
                ,rebus::or('www'
                           ,'https://'
                           ) %R%
                    rebus::optional(rebus::DOT) %R%
                    rebus::capture(rebus::one_or_more(rebus::LOWER))
            )[2]
        }
        # idea from https://github.com/JiaxiangBU/code-smells-and-feels
    # Please use :: or requireNamespace() instead.
    text <-
        if (
            stringr::str_detect(
                url
                ,rebus::DOT %R%
                    rebus::or('png','jpg','gif') %R%
                    rebus::END
                )
        ) {
            glue::glue('![{name}]({url}){{width={size}% height={size}%}}')
            # double curly bracket to solve it.
            # https://github.com/tidyverse/glue
        } else {
            glue::glue('[{name}]({url})')
        }
    text <- stringr::str_replace(text,'\\[NA\\]','[Reference]')
    # if na, give ''
    rstudioapi::insertText(text)
    # avoid paste

    text <-
        if (str_detect(selected(),name_and_url_result)) {
            str_extract(selected(),url_result)
        } else {
            text
        }
    # retrieve does not work
    cat(
        sep="\n"
        ,text
        ,tips()
    )
    usethis::ui_warn("m1() is depreciated, use m1_v2()")
}
JiaxiangBU/add2md documentation built on Jan. 31, 2020, 7:46 p.m.