R/cv_printing_functions.r

Defines functions create_CV_object

Documented in create_CV_object

#################################
####    CV Printing Utils    ####
#################################

# This file contains all the code needed to parse and print various sections of your CV
# from data. Feel free to tweak it as you desire!


#----    create_CV_object    ----

#' Create a CV_Printer object.
#'
#' @param data_location Path of the spreadsheets holding all your data. This can be
#'   either a URL to a google sheet with multiple sheets containing the four
#'   data types or a path to a folder containing four `.csv`s with the neccesary
#'   data.
#' @param source_location Where is the code to build your CV hosted?
#' @param pdf_mode Is the output being rendered into a pdf? Aka do links need
#'   to be stripped?
#' @param sheet_is_publicly_readable If you're using google sheets for data,
#'   is the sheet publicly available? (Makes authorization easier.)
#' @return A new `CV_Printer` object.
create_CV_object <-  function(data_location,
                              blocks = c("education", "academic", "working",
                                         "educational", "publications",
                                         "products"),
                              pdf_mode = FALSE,
                              sheet_is_publicly_readable = TRUE) {

  cv <- list(
    pdf_mode = pdf_mode,
    links = c()
  )

  is_google_sheets_location <- stringr::str_detect(data_location, "docs\\.google\\.com")

  if(is_google_sheets_location){
    if(sheet_is_publicly_readable){
      # This tells google sheets to not try and authenticate. Note that this will only
      # work if your sheet has sharing set to "anyone with link can view"
      googlesheets4::sheets_deauth()
    } else {
      # My info is in a public sheet so there's no need to do authentication but if you want
      # to use a private sheet, then this is the way you need to do it.
      # designate project-specific cache so we can render Rmd without problems
      options(gargle_oauth_cache = "../.secrets")
    }

    meta_data <- googlesheets4::gs4_get(data_location)

    cv <- meta_data$sheets$name %>%
      set_names() %>%
      map( ~googlesheets4::read_sheet(data_location, sheet = .,
                                      skip = 1, col_types = "c")) %>%
      c(cv, .)

  } else {
    stop("a google sheet is required")
  }

  # Clean up entries dataframe to format we need it for printing
  cv$entries <- cv[blocks] %>%
    map(.,~ get_description(.) %>%
          arrange_timeline(.)%>%
          dplyr::mutate_all(~ ifelse(is.na(.), 'N/A', .))) %>%
          bind_rows()

  cv
}

#----     print_section    ----

#' @description Take a position data frame and the section id desired and prints the section to markdown.
#' @param section_id ID of the entries section to be printed as encoded by the `section` column of the `entries` table
print_section <- function(cv, section_id, glue_template = "default"){

  if(glue_template == "default"){
    glue_template <- "
### {title}

{institution}

{loc}

{timeline}

{description_bullets}
\n\n\n"
  } else if (glue_template == "compact"){
    glue_template <- '
### <span style="font-size:.75rem;font-weight:normal;font-family:sans-serif;color: #444;">{title}</span>

<span style="font-size:.65rem;">{institution}</span>

{loc}

{timeline}
\n\n\n'
  }

  section_data <- dplyr::filter(cv$entries, section == section_id)

  # Take entire entries data frame and removes the links in descending order
  # so links for the same position are right next to each other in number.
  for(i in 1:nrow(section_data)){
    for(col in c('title', 'description_bullets')){
      strip_res <- sanitize_links(cv, section_data[i, col])
      section_data[i, col] <- strip_res$text
      cv <- strip_res$cv
    }
  }

  # apply corrige to timeline according to section
  if(section_id == "education"){
    section_data %<>%
      timeline_education()
  } else if (section_id %in% c("publications", "software", "presentation_poster")) {
    section_data %<>%
      timeline_publications() %>%
      get_bold_name()
  } else {
    section_data %<>%
      timeline_events()
  }

  print(glue::glue_data(section_data, glue_template))

  invisible(strip_res$cv)
}

#----    print_text_block    ----

#' @description Prints out text block identified by a given label.
#' @param label ID of the text block to print as encoded in `label` column of `text_blocks` table.
print_text_block <- function(cv, label){
  text_block <- dplyr::filter(cv$text_blocks, loc == label) %>%
    dplyr::pull(text)

  strip_res <- sanitize_links(cv, text_block)

  cat(strip_res$text)

  invisible(strip_res$cv)
}

#----    print_skill_bars    ----

#' @description Construct a bar chart of skills
#' @param out_of The relative maximum for skills. Used to set what a fully filled in skill bar is.
print_skill_bars <- function(cv, out_of = 5, bar_color = "#969696", bar_background = "#d9d9d9", glue_template = "default"){

  if(glue_template == "default"){
    glue_template <- "
<div
  class = 'skill-bar'
  style = \"background:linear-gradient(to right,
                                      {bar_color} {width_percent}%,
                                      {bar_background} {width_percent}% 100%)\"
>{skill}</div>"
  }
  cv$skills %>%
    dplyr::mutate(width_percent = round(100*as.numeric(level)/out_of)) %>%
    glue::glue_data(glue_template) %>%
    print()

  invisible(cv)
}

#----    print_links    ----

#' @description List of all links in document labeled by their superscript integer.
print_links <- function(cv) {
  n_links <- length(cv$links)
  if (n_links > 0) {
    cat("
Links {data-icon=link}
--------------------------------------------------------------------------------

<br>


")

    purrr::walk2(cv$links, 1:n_links, function(link, index) {
      print(glue::glue('{index}. {link}'))
    })
  }

  invisible(cv)
}

#----     print_contact_info    ----

#' @description Contact information section with icons
print_contact_info <- function(cv){
  glue::glue_data(
    cv$contact_info,
    "- <i class='fa fa-{icon}'></i> [{contact}]({link})"
  ) %>% print()

  invisible(cv)
}


#----
ClaudioZandonella/Resume_template documentation built on Dec. 17, 2021, 2:05 p.m.