Skip to content

Commit 2684cca

Browse files
committed
Merge branch 'main' of github.com:quarto-dev/quarto-cli into main
2 parents 1fdd274 + d8deb5a commit 2684cca

File tree

11 files changed

+183
-11
lines changed

11 files changed

+183
-11
lines changed

src/command/render/pandoc-html-dependencies.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { join } from "path/mod.ts";
99

1010
import * as ld from "../../core/lodash.ts";
1111

12-
import { Document, Element } from "../../core/deno-dom.ts";
12+
import { Document, Element, NodeType } from "../../core/deno-dom.ts";
1313

1414
import { pathWithForwardSlashes } from "../../core/path.ts";
1515

@@ -65,6 +65,8 @@ export function readAndInjectDependencies(
6565
if (htmlDependencies.length > 0) {
6666
const injector = domDependencyInjector(doc);
6767
processHtmlDependencies(htmlDependencies, inputDir, libDir, injector);
68+
// Finalize the injection
69+
injector.finalizeInjection();
6870
}
6971

7072
return Promise.resolve({
@@ -93,6 +95,8 @@ export function resolveDependencies(
9395
libDir,
9496
injector,
9597
);
98+
// Finalize the injection
99+
injector.finalizeInjection();
96100

97101
delete extras.html?.[kDependencies];
98102

@@ -144,6 +148,8 @@ interface HtmlInjector {
144148
injectHtml(html: string): void;
145149

146150
injectMeta(meta: Record<string, string>): void;
151+
152+
finalizeInjection(): void;
147153
}
148154

149155
function processHtmlDependencies(
@@ -225,9 +231,30 @@ function processHtmlDependencies(
225231
}
226232
}
227233

234+
const kDependencyTarget = "htmldependencies:E3FAD763";
235+
228236
function domDependencyInjector(
229237
doc: Document,
230238
): HtmlInjector {
239+
// Locates the placeholder target for inserting content
240+
const findTargetComment = () => {
241+
for (const node of doc.head.childNodes) {
242+
if (node.nodeType === NodeType.COMMENT_NODE) {
243+
if (
244+
node.textContent &&
245+
node.textContent.trim() === kDependencyTarget
246+
) {
247+
return node;
248+
}
249+
}
250+
}
251+
252+
// We couldn't find a placeholder comment, just insert
253+
// the nodes at the front of the head
254+
return doc.head.firstChild;
255+
};
256+
const targetComment = findTargetComment();
257+
231258
const injectEl = (
232259
el: Element,
233260
attribs?: Record<string, string>,
@@ -239,11 +266,11 @@ function domDependencyInjector(
239266
}
240267
}
241268
if (!afterBody) {
242-
doc.head.appendChild(el);
243-
doc.head.appendChild(doc.createTextNode("\n"));
269+
doc.head.insertBefore(doc.createTextNode("\n"), targetComment);
270+
doc.head.insertBefore(el, targetComment);
244271
} else {
245272
doc.body.appendChild(el);
246-
doc.head.appendChild(doc.createTextNode("\n"));
273+
doc.body.appendChild(doc.createTextNode("\n"));
247274
}
248275
};
249276

@@ -299,12 +326,18 @@ function domDependencyInjector(
299326
});
300327
};
301328

329+
const finalizeInjection = () => {
330+
// Remove the target comment
331+
targetComment.remove();
332+
};
333+
302334
return {
303335
injectScript,
304336
injectStyle,
305337
injectLink,
306338
injectMeta,
307339
injectHtml,
340+
finalizeInjection,
308341
};
309342
}
310343

@@ -393,11 +426,15 @@ function lineDependencyInjector(
393426
});
394427
};
395428

429+
const finalizeInjection = () => {
430+
};
431+
396432
return {
397433
injectScript,
398434
injectStyle,
399435
injectLink,
400436
injectMeta,
401437
injectHtml,
438+
finalizeInjection,
402439
};
403440
}

src/format/html/format-html-bootstrap.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,14 @@ function bootstrapHtmlPostprocessor(
360360
format.metadata[kAppendixStyle] !== false &&
361361
format.metadata[kAppendixStyle] !== "none"
362362
) {
363-
await processDocumentAppendix(input, inputTraits, format, flags, doc, offset);
363+
await processDocumentAppendix(
364+
input,
365+
inputTraits,
366+
format,
367+
flags,
368+
doc,
369+
offset,
370+
);
364371
}
365372

366373
// no resource refs
@@ -632,6 +639,28 @@ const processFigureMarginCaption = (
632639
}
633640
};
634641

642+
const processTableMarginCaption = (
643+
captionContainer: Element,
644+
doc: Document,
645+
) => {
646+
// Find a caption
647+
const caption = captionContainer.querySelector("caption");
648+
if (caption) {
649+
const marginCapEl = doc.createElement("DIV");
650+
marginCapEl.classList.add("quarto-table-caption");
651+
marginCapEl.classList.add("margin-caption");
652+
marginCapEl.innerHTML = caption.innerHTML;
653+
654+
captionContainer.parentElement?.insertBefore(
655+
marginCapEl,
656+
captionContainer.nextElementSibling,
657+
);
658+
659+
caption.remove();
660+
removeCaptionClass(captionContainer);
661+
}
662+
};
663+
635664
// Process any captions that appear in margins
636665
const processMarginCaptions = (doc: Document) => {
637666
// Forward caption class from parents to the child fig caps
@@ -649,6 +678,23 @@ const processMarginCaptions = (doc: Document) => {
649678
} else {
650679
processFigureMarginCaption(captionContainer, doc);
651680
}
681+
} else {
682+
// Deal with table margin captions
683+
if (figureEl.classList.contains("tbl-parent")) {
684+
// This is table panel, so only grab the main caption
685+
const capDivEl = figureEl.querySelector("div.panel-caption");
686+
if (capDivEl) {
687+
capDivEl.classList.add("margin-caption");
688+
capDivEl.remove();
689+
figureEl.appendChild(capDivEl);
690+
}
691+
} else {
692+
// This is just a table, grab that caption
693+
const table = figureEl.querySelector("table");
694+
if (table) {
695+
processTableMarginCaption(table, doc);
696+
}
697+
}
652698
}
653699
removeCaptionClass(figureEl);
654700
});

src/resources/formats/html/bootstrap/_bootstrap-rules.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,11 @@ aside,
655655
font-size: 0.825rem;
656656
}
657657

658+
.panel-caption.margin-caption {
659+
text-align: inherit;
660+
}
661+
662+
658663
.column-margin.column-container p {
659664
margin-bottom: 0;
660665
}

src/resources/formats/html/pandoc/template.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
$styles.html()$
1010
</style>
1111

12+
<!-- htmldependencies:E3FAD763 -->
1213
$for(header-includes)$
1314
$header-includes$
1415
$endfor$

src/resources/rmd/hooks.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ knitr_hooks <- function(format, resourceDir, handledLanguages) {
9696
opts_hooks[["collapse"]] <- function(options) {
9797
if (isTRUE(options[["collapse"]])) {
9898
comment <- options[["comment"]]
99-
if (is.null(comment) || !nzchar(comment) || is.na(comment)) {
99+
if (is.null(comment) || is.na(comment)) {
100100
options[["comment"]] <- "##"
101101
}
102102
}
@@ -380,7 +380,7 @@ knitr_hooks <- function(format, resourceDir, handledLanguages) {
380380
}
381381
lineNumbers <- options[["code-line-numbers"]]
382382
if (!is.null(lineNumbers)) {
383-
attr <- paste(attr, paste0('code-line-numbers="', as.character(lineNumbers), '"'))
383+
attr <- paste(attr, paste0('code-line-numbers="', tolower(as.character(lineNumbers)), '"'))
384384
}
385385

386386
lang <- tolower(options$engine)

src/resources/schema/definitions.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@
481481
site-path:
482482
string:
483483
description: |
484-
Path to site (defaults to '/'). Not required if you specify `site-url`.
484+
Path to site (defaults to `/`). Not required if you specify `site-url`.
485485
repo-url:
486486
string:
487487
description: "Base URL for website source code repository"

src/resources/schema/project.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
enum: [pdf, epub, docx]
9393
description: |
9494
Download buttons for other formats to include on navbar or sidebar
95-
(one or more of `pdf`, `epub`, and `docx)
95+
(one or more of `pdf`, `epub`, and `docx`)
9696
tools:
9797
arrayOf:
9898
schema:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: "Plot Test"
3+
format: html
4+
code-line-numbers: true
5+
knitr:
6+
opts_chunk:
7+
eval: false
8+
---
9+
10+
# with line number comment {#with-numbering}
11+
12+
```{r}
13+
1 + 2
14+
```
15+
16+
# no line number comment {#no-numbering}
17+
18+
```{r}
19+
#| code-line-numbers: false
20+
1 + 2
21+
```

tests/docs/test-knitr-options.Rmd

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: "Plot Test"
3+
knit: quarto render
4+
---
5+
6+
# collapse and empty comment {#comment-empty}
7+
8+
```{r}
9+
#| collapse: true
10+
#| prompt: true
11+
#| comment: ""
12+
1 + 2
13+
```
14+
15+
# collapse and specific comment {#comment-change}
16+
17+
```{r}
18+
#| collapse: true
19+
#| prompt: true
20+
#| comment: $
21+
1 + 2
22+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* render-r.test.ts
3+
*
4+
* Copyright (C) 2020 by RStudio, PBC
5+
*
6+
*/
7+
8+
import { fileLoader } from "../../utils.ts";
9+
import { ensureHtmlElements } from "../../verify.ts";
10+
import { testRender } from "./render.ts";
11+
12+
const doc = fileLoader("code-highlighting")(
13+
"code-line-number-knitr.qmd",
14+
"html",
15+
);
16+
testRender(doc.input, "html", false, [
17+
ensureHtmlElements(
18+
doc.output.outputPath,
19+
["#with-numbering div.cell-code > pre.number-lines"],
20+
["#no-numbering div.cell-code > pre.number-lines"],
21+
),
22+
]);

0 commit comments

Comments
 (0)