Skip to content

Commit

Permalink
version 0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjisato authored and cran-robot committed Sep 5, 2023
0 parents commit 21ea2dd
Show file tree
Hide file tree
Showing 61 changed files with 2,243 additions and 0 deletions.
27 changes: 27 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Package: juicedown
Title: 'juice' + 'markdown': Convert 'R Markdown' into 'HTML' with
Inline Styles
Version: 0.1.1
Authors@R:
person("Kenji", "Sato", , "kenji@kenjisato.jp", role = c("aut", "cre", "cph"),
comment = c(ORCID = "0009-0000-4520-2560"))
Description: A convenience tool to create 'HTML' with inline styles using
'juicyjuice' and 'markdown' packages. It is particularly useful when working
on a content management system (CMS) whose code editor eliminates style and
link tags. The main use case of the package is the learning management system,
'Moodle'. Additional helper functions for teaching purposes are provided. Learn
more about 'juicedown' at <https://kenjisato.github.io/juicedown/>.
License: MIT + file LICENSE
Encoding: UTF-8
RoxygenNote: 7.2.3
URL: https://kenjisato.github.io/juicedown/
Suggests: readxl, testthat (>= 3.0.0)
Config/testthat/edition: 3
Imports: clipr, dplyr, glue, juicyjuice, knitr, markdown, rlang, rvest,
sass, stringr, xfun, xml2
NeedsCompilation: no
Packaged: 2023-09-05 02:35:33 UTC; kenjisato
Author: Kenji Sato [aut, cre, cph] (<https://orcid.org/0009-0000-4520-2560>)
Maintainer: Kenji Sato <kenji@kenjisato.jp>
Repository: CRAN
Date/Publication: 2023-09-05 16:50:02 UTC
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
YEAR: 2023
COPYRIGHT HOLDER: juicedown authors
60 changes: 60 additions & 0 deletions MD5
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
bfda899525aa2f2854508d65d6aab76a *DESCRIPTION
b3ca3a10749a6d8ebf1ce4b4bc5c98f2 *LICENSE
04feee9782ac0df63a93d2e2c0ce2bde *NAMESPACE
ce049050750cb0ea951587999f4a0205 *NEWS.md
5d9a1afead22e3fc46fa5520e131ba4e *R/aaa.R
cfe4672c6220fa0fc06485a67eb34511 *R/anki.R
71b602fcde8a0723b014af37ea0a79e5 *R/constants.R
7ed2552f3d40fb46622da83e7de42e89 *R/convert.R
324cd9156c17b8834433ab0954409334 *R/convert_html2cms.R
1529be137cfa36868ef80d273dffb0f1 *R/convert_markdown2html.R
413df886daf0f0c51813a43429b60334 *R/counter.R
fea50e4463cc1d702d2f7199f82040b5 *R/css_resolve.R
580d49e7e6773dd44d701042abc21b8a *R/includeFlickr.R
d3c2fec79798573960b1dfe028ad6ccd *R/includeMedia.R
5895ecdfffd185c7fb4b848b4545cbaf *R/includeQuestion.R
f01ca19172f5c792e77688fcd25f2d0c *R/jquery.R
9f79eb1c015696bb20636816861a5e0d *R/juicedown-package.R
418b27f0a2a385df0b9d3aa428e2549b *R/juicedown_example.R
a535018ec660d6316579b4236d637dea *R/math.R
93ae07366f1135e4576f9e0b374a8de4 *R/modal.R
58a72972f3271d37ab750c86adce2d86 *R/rstudio.R
f72e7bfb40b9712b6e68c8eab1274f4f *R/tweaks.R
d7d46493bda4e83751d4fe753e6a4e62 *R/util.R
eddeb536affc117b2900b918b4570f7f *R/zzz.R
fa2c6e1b19eb9e1a00cffb0a82d75cd1 *README.md
aae4997546f1fb862ca5b9e0be2303ed *inst/css/article.scss
f7df42940bc8b038d39470a3f49c05a0 *inst/css/div.scss
14cf932d46744ed5190672fb04319998 *inst/rmarkdown/templates/simple/skeleton/skeleton.Rmd
6e7a8e99487756e985d8cee529cb477c *inst/rmarkdown/templates/simple/template.yaml
76c06a3c9c05e5bff1a86602061d3beb *inst/samples/from-html/sample.html
db60d818d176abb53ec39f5ffd856357 *inst/samples/include/pic/pic1.png
2de0e3eff55e05d83ce11d8634e20176 *inst/samples/include/pic/pic2.png
adf5ade9785b034a538a5210ed92df2f *inst/samples/include/pic/pic3.png
31ea282e0db7d177eca7f56e4efe76ea *inst/samples/include/sample.Rmd
f84bef5eb49d607d53da055d4939b69e *inst/samples/javascript/economics.xlsx
6c0ca14cc14da835113e9ddf14aca49b *inst/samples/javascript/sample.Rmd
8a236141432e4689d17f9fd3c5d0950c *inst/samples/javascript/sample.html
cf25d6ff4a848780efac9abd1c379350 *inst/samples/markdown/sample.html
b7d6e43ce09cdf2ca88beb7234034834 *inst/samples/markdown/sample.md
c57748bad9178a3bf96b15e82f6357f4 *inst/samples/yaml-meta/yaml-metadata.Rmd
de58bd1ffd766726c57bb0e24ba1f1b2 *inst/xml/template.html
7c2fd9f0c70f112899272b9d64a9b6d1 *man/anki.Rd
456557a0e794cdf7878fd5c8b632e5e2 *man/anki_setup.Rd
4c08d0f9263efaff46bbf05721fd019e *man/convert.Rd
75259fc39f24a0b5df1b5cf3a2003edf *man/convert_html2cms.Rd
e909b4fe97307fd0c5db10511bb8720c *man/counter.Rd
ac68a47a5b018f2570c5d9ff399ade3a *man/includeAudio.Rd
23c64382e3c72c9971b5eefd89db4e27 *man/includeFlickr.Rd
e4f489fee17a147b805671e6f04892c2 *man/includeGraphics.Rd
7aa5c47971c1565a7e09477316711a17 *man/includeQuestion.Rd
115192d219f800d2a91a13c0aa437a8c *man/includeText.Rd
b1702f042e6e05062f48dc3e06be5694 *man/includeYT.Rd
ad276e8bb36e64f190aad896f3598a3f *man/juicedown-package.Rd
4380e796bc068f35f706ce5a3bb0d563 *man/juicedown_example.Rd
938b2740963171181015507ab06595d3 *man/tweak_footnote_highlight.Rd
38e99700d055f82f349592a808b0761d *man/tweak_moodle_heading.Rd
c6f2e3cf6962693b1aed81c5ef50efdd *tests/testthat.R
3bf1575cb29d89a9291d13f56513f3b8 *tests/testthat/test-convert_html2cms.R
1dbb62dd0e535304d8e5525ffec1ceb6 *tests/testthat/test-convert_markdown2html.R
85e5ad5ba1465b785315fcbe4abeb7e9 *tests/testthat/test-util.R
25 changes: 25 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by roxygen2: do not edit by hand

export(anki)
export(anki_setup)
export(convert)
export(counter)
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)
importFrom(stringr,str_extract)
importFrom(stringr,str_glue)
importFrom(stringr,str_remove)
importFrom(stringr,str_replace_all)
importFrom(stringr,str_split)
importFrom(stringr,str_split_1)
importFrom(stringr,str_squish)
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# juicedown 0.1.1

* First release.
1 change: 1 addition & 0 deletions R/aaa.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
the <- new.env(parent = emptyenv())
59 changes: 59 additions & 0 deletions R/anki.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

#' Hide/Show text
#'
#' This function inserts JavaScript code for anki texts.
#' Call this function in a code chunk somewhere in your R Markdown file.
#' Use [anki()] function with a inline r code, to make a hidden block.
#'
#' @param background character. Background color of the blank box.
#' @param color character. Font color of the answer of the blank box.
#' @param border character. Border color of the blank box.
#' @param padding integer. Space between the answer text and the border.
#' @param margin integer. Left and right margin around the border.
#' @param cursor character.
#'
#' @return character. CSS/JavaScript code for anki texts.
#' @export
#'
anki_setup <- function(background = "yellow", color = "blue", border = "slateblue",
padding = 3, margin = 3, cursor = "pointer"){

jq <- jquery()

css <- glue::glue("<style>
.anki-marker {
background-color: (background);
padding: (padding)px;
margin-left: (margin)px;
margin-right: (margin)px;
border: 1px solid (border);
cursor: (cursor);
}
.anki-marker .anki-text {
color: (color);
opacity: 0;
}
</style>", .open = "(", .close = ")")

the$header_includes <- c(the$header_includes, css)
the$js <- c(the$js, const$juicedown_anki)

invisible()
}


#' Hide/Show text
#'
#' This function inserts HTML snippet to help students memorize important concepts.
#' To make this function to work, you need to run [anki_setup()] somewhere in
#' the markdown document.
#'
#' @param text character. Text to toggle.
#'
#' @return HTML code span.
#' @export
#'
anki <- function(text) {
x <- glue::glue('<span class = "anki-marker"><span class = "anki-text">{text}</span></span>')
knitr::asis_output(x)
}
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"
135 changes: 135 additions & 0 deletions R/convert.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#' Converts R Markdown file into HTML fragment with inline styles
#'
#' @description
#' This function facilitates writing contents in R Markdown formats for CMS the
#' source editor of which silently removes style tags. When run interactively,
#' the resulting HTML code will be copied to the clipboard.
#'
#' It basically does three things:
#'
#' 1. it converts (R) Markdown file into a HTML file with
#' [knitr::knit()] + [markdown::mark()],
#' 2. embed CSS information in style atribute with [juicyjuice::css_inline()],
#' and then
#' 3. extracts article/div element that is ready for copying and pasting into
#' CMS's source editor.
#'
#' @details
#'
#' # Details
#'
#' ## Change default behaviors with [options()]
#'
#' Currently, three global defaults are available. You can set these defaults to
#' 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.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.
#' @param tag character. Surrounding tag for the HTML block, "article" and "div"
#' are allowed.
#' @param id character. ID attribute for the surrounding tag.
#' @param clip logical. Whether or not copy the result to clipboard. Ignored
#' (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`.
#' @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
#'
#' @examples
#' file <- juicedown_example("markdown", "sample.md")
#' tdir <- tempdir()
#' convert(file, dir = tdir, clip = FALSE)
#' unlink(file.path(tdir, "sample.html"))
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()

# Clear 'the' internal data store.
rm(list = ls(the), envir = the)
the$tempdir <- tempfile(pattern = "dir")
dir.create(the$tempdir)
on.exit(unlink(the$tempdir, recursive = TRUE), add = TRUE)

# Read the Document
text <- readLines(file, warn = FALSE)
doc <- xfun::yaml_body(text)
yaml <- doc$yaml$juicedown
body <- doc$body

# Conversion parameters shared with other functions
the$file <- file
the$root.dir <- dirname(file)

## 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("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)))) {
## knitr::knit() & markdown::mark()
convert_markdown2html(body)
} else {
body
}

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

# Copy to Clipboard
if (the$clip && the$full_html) {
message("Not copied to the clipboard because full_html is set to TRUE.")
} 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(cms, out_file)
invisible(cms)
}

0 comments on commit 21ea2dd

Please sign in to comment.