Skip to content

Commit

Permalink
Change internal handling of JavaScript libraries. Organize example fi…
Browse files Browse the repository at this point in the history
…les. Fix small bug (default CSS set to wrong default option). Rename functions.
  • Loading branch information
kenjisato committed Sep 3, 2023
1 parent 14ea442 commit a200b24
Show file tree
Hide file tree
Showing 42 changed files with 926 additions and 165 deletions.
2 changes: 0 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ Imports:
juicyjuice,
knitr,
markdown,
readxl,
rlang,
rvest,
sass,
stringr,
whisker,
xfun,
xml2
4 changes: 3 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ export(anki)
export(anki_setup)
export(convert)
export(counter)
export(hide_h2)
export(includeAudio)
export(includeFlickr)
export(includeGraphics)
export(includeQuestion)
export(includeText)
export(includeYT)
export(juicedown_example)
export(tweak_footnote_highlight)
export(tweak_moodle_heading)
importFrom(dplyr,"%>%")
importFrom(rlang,"%||%")
importFrom(stats,na.omit)
Expand Down
7 changes: 3 additions & 4 deletions R/anki.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#' Hide/Show text
#'
#' This function inserts JavaScript code for anki texts.
#' This command should be run at the end of the markdown document.
#'
#' @param background character. Background color of the blank box.
#' @param color character. Font color of the answer of the blank box.
Expand Down Expand Up @@ -34,10 +33,10 @@ anki_setup <- function(background = "yellow", color = "blue", border = "slateblu
}
</style>", .open = "(", .close = ")")

js <- '<script src="https://kenjisato.github.io/omuecon/inst/js/anki.js"></script>'
the$header_includes <- c(the$header_includes, css)
the$js <- c(the$js, const$juicedown_anki)

x <- paste(css, jq, js, sep = "\n")
knitr::asis_output(x)
invisible()
}


Expand Down
15 changes: 15 additions & 0 deletions R/constants.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

const <- list()

const$jquery <- "@npm/jquery@3.7.0/dist/jquery.min.js"

const$jquery_modal <- "@npm/jquery-modal@0.9.2/jquery.modal.min.js"

const$juicedown_modal <-
"https://kenjisato.github.io/resources/juicedown/js/modal.js"

const$juicedown_anki <-
"https://kenjisato.github.io/resources/juicedown/js/anki.js"

const$juicedown_css_footnote <-
"https://kenjisato.github.io/resources/juicedown/css/highlightfootnote.css"
75 changes: 48 additions & 27 deletions R/convert.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Converts R Markdown file into HTML fragment with inlined styles
#' Converts R Markdown file into HTML fragment with inline styles
#'
#' @description
#' This function facilitates writing contents in R Markdown formats for CMS the
Expand All @@ -24,8 +24,27 @@
#' alter the appearance of the result.
#'
#' * `juicedown.template`: Defaults to `juicedown:::pkg_file("xml", "template.html")`
#' * `juicedown.article.css`: Defaults to `juicedown:::pkg_file("css", "article.css")`
#' * `juicedown.div.css`: `juicedown:::pkg_file("css", "article.css")`
#' * `juicedown.article.css`: Defaults to `juicedown:::pkg_file("css", "article.scss")`
#' * `juicedown.div.css`: `juicedown:::pkg_file("css", "div.scss")`
#'
#' ## Priority order
#'
#' You can pass conversion parameters in function argument and YAML metadata
#' (under `juicedown` key) and for some parameters, global options, with priority
#' given in that order. For instance, if the `stylesheet` parameter exists in the
#' function call, it is used. If not and if YAML metadata has stylesheet key
#' under juicedown key, then that will be used. In the below example, some.css
#' used.
#' ```markdown
#' ---
#' juicedown:
#' stylesheet:
#' some.css
#' ---
#' ```
#'
#' If neither the function argument nor missing YAML metadata exist, then
#' the global option (such as `juicedown.article.css`) will be used.
#'
#' @param file character. Path to the (R)markdown file.
#' @param dir character. Output directory.
Expand All @@ -36,23 +55,22 @@
#' (set to FALSE) if `full_html = TRUE`. Default is TRUE.
#' @param full_html logical. Produce the complete HTML or HTML block only?
#' @param remove_script logical. Whether or not remove script tags. Ignored
#' (set to FALSE) if `full_html = TRUE`. Default is FALSE
#' (set to FALSE) if `full_html = TRUE`.
#' @param stylesheet character. Paths to the CSS files used in markdown::mark()
#' @param template character. Path to the template used in markdown::mark()
#'
#' @return Invisibly returns a character vector identical to the result file.
#' @export
#'
convert <- function(file = NULL, dir = NULL, tag = c("article", "div"),
id = NULL, clip = TRUE, full_html = FALSE,
remove_script = FALSE, stylesheet = NULL, template = NULL) {
convert <- function(file = NULL, dir = NULL, tag = NULL,
id = NULL, clip = TRUE, full_html = NULL,
remove_script = NULL, stylesheet = NULL, template = NULL) {

# Input file and output directory.
if (length(file) > 1) {
stop(str_glue("{sQuote('convert()')} can handle only one file at a time."))
}
file <- file %||% file.choose()
dir <- dir %||% dirname(file)

# Clear 'the' internal data store.
rm(list = ls(the), envir = the)
Expand All @@ -63,23 +81,26 @@ convert <- function(file = NULL, dir = NULL, tag = c("article", "div"),
# Read the Document
text <- readLines(file, warn = FALSE)
doc <- xfun::yaml_body(text)
yaml <- doc$yaml
yaml <- doc$yaml$juicedown
body <- doc$body

# Conversion options
# Conversion parameters shared with other functions
the$file <- file
the$root.dir <- dirname(file)
the$dir <- yaml$dir %||% dir
the$tag <- match.arg(tag)
the$id <- yaml$id %||% id
the$full_html <- yaml$full_html %||% full_html
the$clip <- yaml$clip %||% clip
the$remove_script <- yaml$remove_script %||% remove_script

## Overwrite parameters.
## Function argument > YAML Metadata > Package default
the$dir <- dir %||% yaml$dir %||% dirname(file)
the$tag <- match.arg(tag %||% yaml$tag, c("article", "div"))
the$id <- id %||% yaml$id
the$full_html <- full_html %||% yaml$full_html %||% FALSE
the$clip <- clip %||% yaml$clip %||% TRUE
the$remove_script <- remove_script %||% yaml$remove_script %||% FALSE

the$stylesheet <- (stylesheet %||% yaml$stylesheet %||%
getOption(str_glue("omu.{the$tag}.css")))
the$template <- (template %||% yaml$templaste %||%
getOption(str_glue("omu.{the$tag}.template")))
getOption(str_glue("juicedown.{the$tag}.css")))
the$template <- (template %||% yaml$template %||%
getOption(str_glue("juicedown.{the$tag}.template")))


html <- if (grepl("r?md", tolower(tools::file_ext(file)))) {
Expand All @@ -89,21 +110,21 @@ convert <- function(file = NULL, dir = NULL, tag = c("article", "div"),
body
}

# Full HTML to HTML Fragment for Moodle
moodle <- convert_html2moodle(html)
# Full HTML to HTML Fragment for CMS
cms <- convert_html2cms(html)

# Copy to Clipboard
if (clip && full_html) {
if (the$clip && the$full_html) {
message("Not copied to the clipboard because full_html is set to TRUE.")
} else if (clip && !full_html) {
clipr::write_clip(moodle, breaks = "\n")
message("HTML code has been copied to the clipboard. Now you can paste it to Moodle.")
} else if (the$clip && !the$full_html) {
clipr::write_clip(cms, breaks = "\n")
message("HTML code has been copied to the clipboard. Now you can paste it to CMS.")
}

out_file <- with_ext(file, ext = "html", dir = dir)
out_file <- dodge_name(out_file, file)

writeLines(moodle, out_file)
invisible(moodle)
writeLines(cms, out_file)
invisible(cms)
}

4 changes: 2 additions & 2 deletions R/convert_html2moodle.R → R/convert_html2cms.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Convert a HTML file into a HTML block to be copied and pasted for Moodle.
#' Convert a HTML file into a HTML block to be copied and pasted for CMS.
#'
#' @param in_text character. Character vector representing the HTML file to convert.
#' @param full_html logical. If TRUE, produce complete html output,
Expand All @@ -9,7 +9,7 @@
#' @param remove_script logical. If TRUE, script tags are all stripped out.
#'
#' @return character. HTML block.
convert_html2moodle <- function(
convert_html2cms <- function(
in_text,
full_html = the$full_html %||% FALSE,
tag = the$tag %||% "body",
Expand Down
5 changes: 4 additions & 1 deletion R/convert_markdown2html.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ convert_markdown2html <- function(
text = intermediate_text,
output = NULL, format = "html",
template = template,
meta = list(css = combined_stylesheet),
meta = list(css = combined_stylesheet,
js = the$js,
header_includes = the$header_includes,
script = the$script),
options = "-smartypants"
)

Expand Down
14 changes: 0 additions & 14 deletions R/hide_h2.R

This file was deleted.

4 changes: 0 additions & 4 deletions R/includeMedia.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ includeAudio <- function(url, preload = c("metadata", "auto", "none"),
#' Include graphic media.
#'
#' This function is a wrapper for [knitr::include_graphics()].
#' Since omuecon creates intermediate files in a temporary directory,
#' calling [knitr::include_graphics()] does not include local images.
#' This function copies the media into the temporary directory and
#' calls [knitr::include_graphics()].
#'
#' @param src character. Path to the image.
#' @param ... Parameters passed to [knitr::include_graphics()], other than path.
Expand Down
46 changes: 0 additions & 46 deletions R/includeSchedule.R

This file was deleted.

20 changes: 8 additions & 12 deletions R/jquery.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@

jquery <- function(){

jq <- if (the$jquery %||% FALSE) {
# the$jquery is TRUE if this function has been already called.
# NULL means FALSE, so go to else block...
NULL
} else {
the$jquery <- TRUE
'<script
src="https://code.jquery.com/jquery-3.7.0.slim.min.js"
integrity="sha256-tG5mcZUtJsZvyKAxYLVXrmjKBVLd6VpVccqz/r4ypFE="
crossorigin="anonymous"></script>'
the$js <- c(the$js, const$jquery)
}
jq
invisible()
}

jquery_modal <- function() {
jq <- jquery()
jqm <- if (the$jquery_modal %||% FALSE) {
# the$jquery_modal is TRUE if this function has been already called.
# NULL means FALSE, so go to else block...
NULL
} else {
the$jquery_modal <- TRUE
c(
jq,
'<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js"></script>',
'<script src="https://kenjisato.github.io/omuecon/inst/js/modal.js"></script>'
)
the$js <- c(the$js, const$jquery_modal, const$juicedown_modal)
}
paste(jqm, collapse = "\n")
invisible()
}
40 changes: 37 additions & 3 deletions R/juicedown_example.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@

juicedown_example <- function(path = NULL) {
if (is.null(path)) {
#' List example files
#'
#' @description
#' List example files for the package. Show the path when the exact name is given.
#'
#' @details
#' The package sample files are organized under samples directory like so:
#'
#' samples/
#' ├── from-html/
#' │ └── sample.html
#' └── include/
#' ├── pic/
#' └── sample.Rmd
#'
#' * `juicedown_example()` is equivalent to `ls samples`,
#' * `juicedown_example("from-html")` is to `ls samples/from-html`, and
#' * `juicedown_example("from-html", "sample.html")` shows the full path.
#'
#' @param name character. A single string for example name.
#' @param file character. A single string for the main file.
#'
#' @return character. A list of example files or a path string to each example file.
#' @export
#'
#' @examples
#' juicedown_example()
#' juicedown_example("from-html")
#' juicedown_example("from-html", "sample.html")
#'
juicedown_example <- function(name = NULL, file = NULL) {
if (is.null(name)) {
list.files(pkg_file("samples"))
} else {
pkg_file("samples", path)
if (is.null(file)) {
list.files(pkg_file("samples", name))
} else {
pkg_file("samples", name, file)
}
}
}
Loading

0 comments on commit a200b24

Please sign in to comment.