# let's create an operation that adds markdown emphasis
# to elements of a vector
# A) define the function
fun_emph = function(x, ...) paste0("*", x, "*")
# B) register it
string_magic_register_fun(fun_emph, "emph")
# C) use it
x = string_vec("right, now")
string_magic("Take heed, {emph, c? x}.")
#
# now let's add the option "strong"
fun_emph = function(x, options, ...) {
if("strong" %in% options){
paste0("***", x, "***")
} else {
paste0("*", x, "*")
}
}
string_magic_register_fun(fun_emph, "emph", "strong")
x = string_vec("right, now")
string_magic("Take heed, {emph.strong, c? x}.")
#
# now let's add an argument
fun_emph = function(x, argument, options, ...){
arg = argument
if(nchar(arg) == 0) arg = "*"
if("strong" %in% options){
arg = paste0(rep(arg, 3), collapse = "")
}
paste0(arg, x, arg)
}
string_magic_register_fun(fun_emph, "emph", "strong")
x = string_vec("right, now")
string_magic("Take heed, {'_'emph.s, c? x}.")
#
# using string_magic_register_ops
#
# create a 'header' maker
string_magic_register_ops("tws, '# 'paste, ' 'paste.right, '40|-'fill", "h1")
cat_magic("{h1 ! My title}\n my content")
Run the code above in your browser using DataLab