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 linebreaks before and after figures #2033

Closed
wants to merge 3 commits into from
Closed
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
22 changes: 18 additions & 4 deletions R/hooks-md.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ hook_plot_md_base = function(x, options) {
res = sprintf('![%s](%s%s)', cap, base, .upload.url(x))
if (!is.null(lnk) && !is.na(lnk)) res = sprintf('[%s](%s)', res, lnk)
res = paste0(res, if (nocap) '<!-- -->' else '', if (is_latex_output()) ' ' else '')
return(res)
return(add_linebreak(res, options))
}
add_link = function(x) {
if (is.null(lnk) || is.na(lnk)) return(x)
Expand All @@ -84,10 +84,10 @@ hook_plot_md_base = function(x, options) {
} else {
paste0(d1, img, if (plot2) paste0('\n', d2, '\n</div>'))
}
} else add_link(.img.tag(
} else add_linebreak(add_link(.img.tag(
.upload.url(x), w, h, alt,
c(s, sprintf('style="%s"', css_align(a)))
))
)), options)
}

hook_plot_md_pandoc = function(x, options) {
Expand All @@ -106,7 +106,21 @@ hook_plot_md_pandoc = function(x, options) {
)
if (at != '') at = paste0('{', at, '}')

sprintf('![%s](%s%s)%s', cap, base, .upload.url(x), at)
add_linebreak(sprintf('![%s](%s%s)%s', cap, base, .upload.url(x), at), options)
}

# Add linebreaks so to treat images as independent blocks
add_linebreak <- function(x, options) {
fig.show = options$fig.show
fig.cur = options$fig.cur
fig.num = options$fig.num
if (fig.cur == 1 || fig.show != "hold") {
x = paste0("\n\n", x)
}
if (fig.cur == fig.num) {
x = paste0(x, "\n\n")
}
x
}

css_align = function(align) {
Expand Down
3 changes: 2 additions & 1 deletion tests/testit/test-hooks-md.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ img_output = function(path, opts = list()) {
}

assert('include_graphics() includes custom images correctly', {
(img_output('a.png') %==% '![](a.png)')
(img_output('a.png', list(fig.cur = 1L, fig.num = 1L)) %==% '\n\n![](a.png)\n\n')
(img_output(c('a.png', 'b.png'), list(fig.cur = 1L, fig.num = 2L)) %==% '\n\n![](a.png)\n\n![](b.png)\n\n')
(img_output(c('a.png', 'b.png'), list(fig.show = 'hold')) %==% '![](a.png)![](b.png)')
(img_output('a.png', list(fig.cap = 'foo bar')) %==% '![foo bar](a.png)')
(img_output('a.png', list(out.width = '50%')) %==% '<img src="a.png" width="50%" />')
Expand Down