Skip to content

Commit

Permalink
Merge pull request #20 from jhudsl/cansavvy/add-check
Browse files Browse the repository at this point in the history
Add commas and gha check
  • Loading branch information
cansavvy authored Aug 28, 2021
2 parents 284634b + c5ef1d4 commit 1610e8b
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 21 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/render-leanpub.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

# Adapted for this jhudsl repository by Candace Savonen Aug 2021

name: Render and publish Leanpub

# Triggers the workflow on pull requests for the master branch OR can be manually triggered
on:
workflow_dispatch:
pull_request:
branches: [ master ]

jobs:
publish:
runs-on: ubuntu-latest
# I have it check out the image that is built from: https://github.com/jhudsl/DaSL_Course_Template_Bookdown/blob/main/docker/Dockerfile
container:
image: jhudsl/course_template

steps:
# Checks-out the _Leanpub repository to run the checks
- name: Checkout code from Leanpub test repo
uses: actions/checkout@v2
with:
repository: jhudsl/DaSL_Course_Template_Leanpub
token: ${{ secrets.GH_PAT }}

# Copy over the latest leanbuild and use that for the run
- name: Get latest leanbuild
run: |
sudo apt-get install -y --no-install-recommends subversion
# Copy over the latest leanbuild build
svn export https://github.com/${GITHUB_REPOSITORY}.git/branches/${GITHUB_HEAD_REF} leanbuild/
# Run leanpub rendering
- name: Run leanbuild::bookdown_to_leanpub
run: |
Rscript -e "devtools::load_all('leanbuild'); leanbuild::bookdown_to_leanpub(footer_text = 'check if footer works')"
63 changes: 63 additions & 0 deletions .github/workflows/update-leanpub.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

# Adapted for this jhudsl repository by Candace Savonen Aug 2021

name: Render and publish Leanpub

# Triggers the workflow AFTER changes to leanbuild have been merged
on:
workflow_dispatch:
push:
branches: [ master ]

jobs:
publish:
runs-on: ubuntu-latest
# I have it check out the image that is built from: https://github.com/jhudsl/DaSL_Course_Template_Bookdown/blob/main/docker/Dockerfile
container:
image: jhudsl/course_template

steps:
# Checks-out the _Leanpub repository to run the checks
- name: Checkout code from Leanpub test repo
uses: actions/checkout@v2
with:
repository: jhudsl/DaSL_Course_Template_Leanpub
token: ${{ secrets.GH_PAT }}

# Copy over the latest leanbuild and use that for the run
- name: Get latest leanbuild
run: |
sudo apt-get install -y --no-install-recommends subversion
# Copy over the latest leanbuild build
svn export https://github.com/${GITHUB_REPOSITORY}.git/branches/${GITHUB_HEAD_REF} leanbuild/
# Run leanpub rendering
- name: Run leanbuild::bookdown_to_leanpub
run: |
Rscript -e "devtools::load_all('leanbuild'); leanbuild::bookdown_to_leanpub(footer_text = 'check if footer works')"
- name: Create PR with newly rendered docs files
uses: peter-evans/create-pull-request@v3
id: cpr
with:
token: ${{ secrets.GH_PAT }}
commit-message: Show latest leanbuild changes
signoff: false
branch: auto_copy_rendered_files
delete-branch: true
title: 'GHA: Automated transfer of leanbuild-needed files from Bookdown repository'
body: |
### Description:
This PR has changes to this repository's files using the latest leanbuild package changes!
If they don't look right, file an issue on the leanbuild package: https://github.com/jhudsl/leanbuild/issuesand do not merge.
labels: |
automated
reviewers: $GITHUB_ACTOR

# Write out PR info
- name: Check outputs
run: |
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ Imports:
curl,
rprojroot,
magrittr,
yaml
yaml,
here
Suggests:
didactr,
knitr,
Expand Down
136 changes: 120 additions & 16 deletions R/bookdown_to_leanpub.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,66 @@

#' Load in Bookdown specifications from _bookdown.yml
#'
#' @param path Where to look for the _bookdown.yml file. Passes to the bookdown_file() function. By default looks in current directory
#'
#' @return The yaml contents using yaml::yaml.load_file()
#' @export

get_bookdown_spec <- function(path = ".") {
file <- bookdown_file(path = path)

# Get the file path to _bookdown.yaml
file_path <- bookdown_file(path = path)

# Read in yaml
suppressWarnings({
out <- yaml::yaml.load_file(file)
yaml_contents <- yaml::yaml.load_file(file_path)
})
out

return(yaml_contents)
}

#' Find main Bookdown directory
#'
#' @param path Where to look for the file. By default looks in current directory.
#'
#' @return Returns the directory where the _bookdown.yml is contained.
#' @export

bookdown_path <- function(path = ".") {

rprojroot::find_root(rprojroot::has_file("_bookdown.yml"), path = path)

path <- dirname(here::here("_bookdown.yml"))

return(path)
}

#' Find file path to _bookdown.yml
#'
#' @param path Where to look for the _bookdown.yml file. Passes to the bookdown_file() function. By default looks in current directory
#'
#' @return The file path to _bookdown.yml
#' @export
#'
bookdown_file <- function(path = ".") {

root_dir <- bookdown_path(path = path)
file.path(root_dir, "_bookdown.yml")
file_path <- file.path(root_dir, "_bookdown.yml")

return(file_path)
}

#' Get file paths all Rmds in the bookdown directory
#'
#' @param path Where to look for the _bookdown.yml file. Passes toget_bookdown_spec() function. By default looks in current directory
#'
#' @return The file paths to Rmds listed in the _bookdown.yml file.
#' @export
#'
bookdown_rmd_files <- function(path = ".") {

spec <- get_bookdown_spec(path)

files <- spec$rmd_files
if (is.null(files) || all(is.na(files)) || length(files) == 0) {
warning(
Expand All @@ -32,33 +76,74 @@ bookdown_rmd_files <- function(path = ".") {
return(files)
}

#' Declare file path to docs/ folder
#'
#' @param path Where to look for the _bookdown.yml file. Passes toget_bookdown_spec() function. By default looks in current directory
#'
#' @return The file paths to Rmds listed in the _bookdown.yml file.
#' @export
#'
bookdown_destination <- function(path = ".") {

# Find _bookdown.yml
root_dir <- bookdown_path(path = path)

# Get specs from _bookdown.yml
spec <- get_bookdown_spec(path = path)

# Find output directory declared in the bookdown.yml
output_dir <- spec$output_dir

# If none specified, assume its called docs/
if (is.null(output_dir)) {
output_dir <- "docs"
}
# Get the full file path
full_output_dir <- file.path(root_dir, output_dir)

# If the output dir doesn't exist, make it
dir.create(full_output_dir, showWarnings = FALSE, recursive = TRUE)

# Declare full paths
full_output_dir <- normalizePath(full_output_dir, winslash = "/")
full_output_dir

return(full_output_dir)
}

#' Copying directory contents
#'
#' @param from Where the files to copy are located.
#' @param to Where the files to copy are to be copied should go to.
#'
#' @return The file paths to Rmds listed in the _bookdown.yml file.
#' @export
#'
copy_directory_contents <- function(from, to) {
x <- list.files(

file_list <- list.files(
path = from, full.names = TRUE, all.files = TRUE,
recursive = TRUE
)
file.copy(x, to, recursive = TRUE, overwrite = TRUE)
file.copy(file_list, to, recursive = TRUE, overwrite = TRUE)
}

copy_resources <- function(path = ".", output_dir = "manuscript") {
copy_resources <- function(path = ".",
images_dir = "resources/images",
output_dir = "manuscript") {

# Get file path to bookdown.yml
path <- bookdown_path(path)
res_image_dir <- file.path(path, "resources/images")

# Assume image directory is `resources/images`
res_image_dir <- file.path(path, images_dir )

# Creat the directory if it doesn't exist
dir.create(res_image_dir, showWarnings = FALSE, recursive = TRUE)
manuscript_image_dir <- file.path(output_dir, "resources/images")

manuscript_image_dir <- file.path(output_dir, images_dir )

dir.create(manuscript_image_dir, showWarnings = FALSE, recursive = TRUE)

manuscript_image_dir <- normalizePath(manuscript_image_dir)
if (file.exists(res_image_dir)) {
copy_directory_contents(res_image_dir, manuscript_image_dir)
Expand Down Expand Up @@ -101,27 +186,46 @@ copy_bib <- function(path = ".", output_dir = "manuscript") {
bookdown_to_leanpub <- function(path = ".",
render = TRUE,
output_dir = "manuscript",
make_book_txt = TRUE,
make_book_txt = FALSE,
remove_resources_start = FALSE,
verbose = TRUE,
footer_text = NULL) {

# Declare regex for finding rmd files
rmd_regex <- "[.][R|r]md$"

# Get the path to the _bookdown.yml
path <- bookdown_path(path)


if (verbose) {
message(paste0("Looking for bookdown file in ", path))
}
rmd_files <- bookdown_rmd_files(path = path)

if (verbose) {
message(paste0(c("Processing these files: ", rmd_files), collapse = "\n"))
}

if (render) {
if (verbose) {
message("Rendering the Book")
}
input <- rmd_files[grepl("index", rmd_files, ignore.case = TRUE)][1]
if (length(input) == 0 || is.na(input)) {
input <- rmd_files[1]
# Get the index file path
index_file <- grep("index", rmd_files, ignore.case = TRUE, value = TRUE)

index_file <- normalizePath(index_file)

if (length(index_file) == 0 || is.na(index_file)) {
index_file <- rmd_files[1]
}
message(paste("index_file is", index_file))

output_format <- bookdown::gitbook(pandoc_args = "--citeproc")
# output_format$pandoc$to = output_format$pandoc$from
output_format$pandoc$args <- c(output_format$pandoc$args, "--citeproc")
bookdown::render_book(input = input, output_format = output_format)
bookdown::render_book(input = index_file,
output_format = output_format,
clean_envir = FALSE)
}

# may be irrelevant since copy_docs does everything
Expand Down
6 changes: 3 additions & 3 deletions R/replace_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ build_image <- function(src, ..., caption = NULL, embed = NULL,
'alt: "{alt}",',
'height: "{height}",',
'width: "{width}",',
'align: "{align}"',
'type: "{type}"',
'poster: "{poster}"',
'align: "{align}",',
'type: "{type}",',
'poster: "{poster}",',
'embed: "{embed}"'
)
if (is.null(fullbleed) ||
Expand Down
2 changes: 1 addition & 1 deletion man/bookdown_to_leanpub.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1610e8b

Please sign in to comment.