R/dom.R

# no need of this? we can get html specifying a parameter

#' Convert genius ??? document object model to xml
#'
#' @param x
#'
#' @return
#' @export
#'
#' @examples
#' \dontrun{
#'
#' }
#'
genius_dom_to_text <- function(x){
    if (is.null(x)) return("")
    else if (is.character(x)) return(paste(x, collapse = ""))
    else if ("tag" %in% names(x)){
        attrs <- x[!(names(x) %in% c("tag", "children"))] %>% unname %>% unlist
        if (length(attrs) == 0) attrs_txt <- ""
        else{
            attrs_txt <- paste(names(attrs), paste0("'", attrs, "'"), sep = "=") %>%
                paste(collapse = " ")
            attrs_txt <- paste0(" ", attrs_txt)
        }
        return(paste0(
            "<", x$tag, attrs_txt, ">",
            genius_dom_to_text(x$children),
            "</", x$tag, ">"))
    }
    else return(paste(map(x, genius_dom_to_text), collapse = ""))
}

#' Title
#'
#' @param x
#'
#' @return
#' @export
#'
#' @examples
genius_dom_to_xml <- function(x){
    txt <- genius_dom_to_text(x)
    xml2::read_html(txt)
}
idmn/genius documentation built on May 27, 2019, 7:26 a.m.