Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to parse text in the lesson definition. #202

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ Imports:
yaml,
RCurl,
digest,
tools
tools,
whisker
Roxygen: list(wrap = FALSE)
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ importFrom(tools,file_ext)
importFrom(tools,file_path_sans_ext)
importFrom(yaml,yaml.load)
importFrom(yaml,yaml.load_file)
importFrom(whisker,whisker.render)
6 changes: 6 additions & 0 deletions R/menu.R
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ loadLesson.default <- function(e, courseU, lesson){
clearCustomTests()
loadCustomTests(lesPath)

# Check if there is a localization file
localization <- file.path(lesPath, "locale.yaml")
if(file.exists(localization)){
dataName <- localize_lesson(localization, lesPath, shortname)
}

# Attached class to content based on file extension
class(dataName) <- get_content_class(dataName)

Expand Down
29 changes: 29 additions & 0 deletions R/parse_content.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,33 @@ parse_content.yaml <- function(file, e){
lesson(df, lesson_name=meta$Lesson, course_name=meta$Course,
author=meta$Author, type=meta$Type, organization=meta$Organization,
version=meta$Version)
}


#' @importFrom tools file_ext
#' @importFrom tools file_path_sans_ext
#' @importFrom yaml yaml.load_file
#' @importFrom whisker whisker.render
localize_lesson <- function(file, lesPath, shortname){

# Load localization file
locale_yaml <- yaml.load_file(file)

# TO DO: Process localization. Placeholder loads first element.
locale_yaml <- locale_yaml[[1]]

# Load lesson file
lesson <- readLines(con = file.path(lesPath, shortname))

# Render localized lesson
lesson <- whisker.render(template = lesson, data = locale_yaml)

# Save localized lesson as a temporary file
file <- file_path_sans_ext(shortname)
extension <- paste0(".", file_ext(shortname))
localized_lesson <- tempfile(pattern = file, fileext = extension)
writeLines(lesson, con = localized_lesson)

# Return file path for localized lesson
return(localized_lesson)
}