Skip to content

Commit

Permalink
I can't use knitr_block/knitr_inline as class names since it would br…
Browse files Browse the repository at this point in the history
…eak the lightparser package; let me just get rid of these S3 methods, which are unnecessarily advanced---just use normal functions instead (i.e., use inherits() to do the dispatch by myself)
  • Loading branch information
yihui committed Apr 5, 2024
1 parent ab3b247 commit b950e72
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 29 deletions.
6 changes: 0 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ S3method(knit_print,default)
S3method(knit_print,knit_asis)
S3method(knit_print,knit_asis_url)
S3method(knit_print,knitr_kable)
S3method(print,knitr_block)
S3method(print,knitr_inline)
S3method(print,knitr_kable)
S3method(process_group,knitr_block)
S3method(process_group,knitr_inline)
S3method(process_tangle,knitr_block)
S3method(process_tangle,knitr_inline)
S3method(sew,character)
S3method(sew,default)
S3method(sew,error)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

- Faster processing of cache dependencies in `dep_auto()` (thanks, @knokknok, #2318).

- Removed some S3 methods that are used internally and changed them to normal functions: `print.block -> print_block`, `print.inline -> print_inline`, `process_group.block/process_group.inline -> process_group`, and `process_tangle.block/process_tangle.inline -> process_tangle`.

# CHANGES IN knitr VERSION 1.45

## NEW FEATURES
Expand Down
28 changes: 11 additions & 17 deletions R/block.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
# S3 method to deal with chunks and inline text respectively
# process chunks and inline text
process_group = function(x) {
UseMethod('process_group', x)
}
#' @export
process_group.knitr_block = function(x) call_block(x)
#' @export
process_group.knitr_inline = function(x) {
x = call_inline(x)
knit_hooks$get('text')(x)
if (inherits(x, 'block')) call_block(x) else {
x = call_inline(x)
knit_hooks$get('text')(x)
}
}


call_block = function(block) {
# now try eval all options except those in eval.after and their aliases
af = opts_knit$get('eval.after'); al = opts_knit$get('aliases')
Expand Down Expand Up @@ -55,7 +50,7 @@ call_block = function(block) {
optc = opts_current$get(); on.exit(opts_current$restore(optc), add = TRUE)
opts_current$restore(params)

if (opts_knit$get('progress')) print(block)
if (opts_knit$get('progress')) print_block(block)

params[['code']] = parse_chunk(params[['code']]) # parse sub-chunk references

Expand Down Expand Up @@ -554,7 +549,7 @@ call_inline = function(block) {
optc = opts_current$get(); on.exit(opts_current$restore(optc), add = TRUE)
params = opts_chunk$merge(list(label = unnamed_chunk()))
opts_current$restore(params)
if (opts_knit$get('progress')) print(block)
if (opts_knit$get('progress')) print_inline(block)
in_input_dir(inline_exec(block))
}

Expand Down Expand Up @@ -585,10 +580,10 @@ inline_exec = function(
}

process_tangle = function(x) {
UseMethod('process_tangle', x)
if (inherits(x, 'block')) tangle_block(x) else tangle_inline(x)
}
#' @export
process_tangle.knitr_block = function(x) {

tangle_block = function(x) {
params = opts_chunk$merge(x$params)
for (o in c('purl', 'eval', 'child')) {
if (inherits(try(params[o] <- list(eval_lang(params[[o]]))), 'try-error')) {
Expand All @@ -614,9 +609,8 @@ process_tangle.knitr_block = function(x) {
# e.g. when documentation 1 or 2 with purl()
label_code(code, x)
}
#' @export
process_tangle.knitr_inline = function(x) {

tangle_inline = function(x) {
output = if (opts_knit$get('documentation') == 2L) {
output = paste("#'", gsub('\n', "\n#' ", x$input, fixed = TRUE))
} else ''
Expand Down
10 changes: 4 additions & 6 deletions R/parser.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ parse_block = function(code, header, params.src, markdown_mode = out_format('mar
}
}

structure(class = 'knitr_block', list(
structure(class = 'block', list(
params = params, params.src = params.src, params.chunk = parts$src)
)
}
Expand Down Expand Up @@ -196,8 +196,7 @@ partition_chunk = function(engine, code) {
xfun::divide_chunk(engine, code)
}

#' @export
print.knitr_block = function(x, ...) {
print_block = function(x) {
params = x$params
if (opts_knit$get('verbose')) {
code = knit_code$get(params$label)
Expand Down Expand Up @@ -231,12 +230,11 @@ parse_inline = function(input, patterns) {

structure(
list(input = input, location = loc, code = code2, code.src = code1),
class = 'knitr_inline'
class = 'inline'
)
}

#' @export
print.knitr_inline = function(x, ...) {
print_inline = function(x) {
if (opts_knit$get('verbose')) {
cat('\n')
if (nrow(x$location)) {
Expand Down

0 comments on commit b950e72

Please sign in to comment.