Skip to content

Commit 4f5d375

Browse files
committed
Provide more notebook style affordances
1 parent b9bc053 commit 4f5d375

File tree

10 files changed

+101
-31
lines changed

10 files changed

+101
-31
lines changed

src/config/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const kFormatResources = "format-resources";
4141
export const kFormatLinks = "format-links";
4242
export const kNotebookLinks = "notebook-links";
4343
export const kNotebookView = "notebook-view";
44+
export const kNotebookViewStyle = "notebook-view-style";
4445

4546
export const kKeepHidden = "keep-hidden";
4647

@@ -170,6 +171,7 @@ export const kRenderDefaultsKeys = [
170171
kFormatLinks,
171172
kNotebookLinks,
172173
kNotebookView,
174+
kNotebookViewStyle,
173175
];
174176

175177
// language fields

src/config/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ import {
132132
kMermaidFormat,
133133
kNotebookLinks,
134134
kNotebookView,
135+
kNotebookViewStyle,
135136
kNumberOffset,
136137
kNumberSections,
137138
kOutput,
@@ -410,6 +411,7 @@ export interface FormatRender {
410411
[kFormatResources]?: string[];
411412
[kFormatLinks]?: boolean | string[];
412413
[kNotebookLinks]?: boolean | "inline" | "global";
414+
[kNotebookViewStyle]?: "document" | "notebook";
413415
[kNotebookView]?:
414416
| boolean
415417
| NotebookPublishOptions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ import {
7979
isPresentationOutput,
8080
} from "../../config/format.ts";
8181
import { basename } from "path/mod.ts";
82-
import { processNotebookEmbeds } from "./format-html-embed.ts";
82+
import { processNotebookEmbeds } from "./format-html-notebook.ts";
8383

8484
export function formatPageLayout(format: Format) {
8585
return format.metadata[kPageLayout] as string || kPageLayoutArticle;
Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as ld from "../../core/lodash.ts";
1313
import {
1414
kNotebookLinks,
1515
kNotebookView,
16+
kNotebookViewStyle,
1617
kOutputFile,
1718
kRelatedNotebooksTitle,
1819
kSourceNotebookPrefix,
@@ -21,7 +22,10 @@ import {
2122
} from "../../config/constants.ts";
2223
import { Format, NotebookPublishOptions } from "../../config/types.ts";
2324

24-
import { RenderServices } from "../../command/render/types.ts";
25+
import {
26+
HtmlPostProcessResult,
27+
RenderServices,
28+
} from "../../command/render/types.ts";
2529
import { render, renderServices } from "../../command/render/render-shared.ts";
2630

2731
import { basename, dirname, join } from "path/mod.ts";
@@ -36,6 +40,46 @@ interface NotebookViewOptions {
3640
href?: string;
3741
}
3842

43+
export const kNotebookViewStyleNotebook = "notebook";
44+
45+
const kQuartoNbClass = "quarto-notebook";
46+
const kQuartoCellContainerClass = "cell-container";
47+
const kQuartoCellDecoratorClass = "cell-decorator";
48+
49+
export function notebookViewPostProcessor() {
50+
return (doc: Document): Promise<HtmlPostProcessResult> => {
51+
doc.body.classList.add(kQuartoNbClass);
52+
const cells = doc.querySelectorAll("div.cell[data-execution_count]");
53+
for (const cell of cells) {
54+
const cellEl = cell as Element;
55+
const count = cellEl.getAttribute("data-execution_count");
56+
if (count) {
57+
const containerNode = doc.createElement("div");
58+
containerNode.classList.add(kQuartoCellContainerClass);
59+
containerNode.classList.add("column-page-left");
60+
61+
const decoratorNode = doc.createElement("div");
62+
decoratorNode.classList.add(kQuartoCellDecoratorClass);
63+
64+
const contentsEl = doc.createElement("pre");
65+
contentsEl.appendChild(doc.createTextNode(`In [${count}]:`));
66+
decoratorNode.appendChild(contentsEl);
67+
68+
containerNode.appendChild(decoratorNode);
69+
cell.parentElement?.insertBefore(containerNode, cell);
70+
containerNode.appendChild(cell);
71+
}
72+
}
73+
74+
const resources: string[] = [];
75+
const supporting: string[] = [];
76+
return Promise.resolve({
77+
resources,
78+
supporting,
79+
});
80+
};
81+
}
82+
3983
export async function processNotebookEmbeds(
4084
doc: Document,
4185
format: Format,
@@ -57,7 +101,6 @@ export async function processNotebookEmbeds(
57101
const linkedNotebooks: string[] = [];
58102
for (const nbDivNode of notebookDivNodes) {
59103
const nbDivEl = nbDivNode as Element;
60-
nbDivEl.classList.add("quarto-notebook");
61104
const notebookPath = nbDivEl.getAttribute("data-notebook");
62105
if (notebookPath) {
63106
linkedNotebooks.push(notebookPath);
@@ -236,6 +279,7 @@ async function renderHtmlView(
236279
[kTheme]: format.metadata[kTheme],
237280
[kOutputFile]: nbPreviewFile,
238281
[kTemplate]: templatePath,
282+
[kNotebookViewStyle]: kNotebookViewStyleNotebook,
239283
},
240284
quiet: false,
241285
},

src/format/html/format-html.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
kLinkExternalIcon,
2929
kLinkExternalNewwindow,
3030
kNotebookLinks,
31+
kNotebookViewStyle,
3132
kTheme,
3233
} from "../../config/constants.ts";
3334

@@ -89,6 +90,10 @@ import {
8990
} from "../../core/giscus.ts";
9091
import { metadataPostProcessor } from "./format-html-meta.ts";
9192
import { kHtmlEmptyPostProcessResult } from "../../command/render/constants.ts";
93+
import {
94+
kNotebookViewStyleNotebook,
95+
notebookViewPostProcessor,
96+
} from "./format-html-notebook.ts";
9297

9398
export function htmlFormat(
9499
figwidth: number,
@@ -528,6 +533,15 @@ export async function htmlFormatExtras(
528533
partials: partials.map((partial) => join(templateDir, partial)),
529534
};
530535

536+
const htmlPostProcessors = [
537+
htmlFormatPostprocessor(format, featureDefaults),
538+
metadataPostProcessor(input, format, offset),
539+
];
540+
const viewStyle = format.render[kNotebookViewStyle];
541+
if (viewStyle === kNotebookViewStyleNotebook) {
542+
htmlPostProcessors.push(notebookViewPostProcessor());
543+
}
544+
531545
const metadata: Metadata = {};
532546
return {
533547
[kIncludeInHeader]: includeInHeader,
@@ -537,10 +551,7 @@ export async function htmlFormatExtras(
537551
html: {
538552
[kDependencies]: dependencies,
539553
[kSassBundles]: sassBundles,
540-
[kHtmlPostprocessors]: [
541-
htmlFormatPostprocessor(format, featureDefaults),
542-
metadataPostProcessor(input, format, offset),
543-
],
554+
[kHtmlPostprocessors]: htmlPostProcessors,
544555
},
545556
};
546557
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,24 @@ figure .quarto-notebook-link {
10171017
margin-right: 0.4em;
10181018
}
10191019

1020+
.quarto-notebook .cell-container {
1021+
display: flex;
1022+
}
1023+
1024+
.quarto-notebook .cell-container .cell {
1025+
flex-grow: 4;
1026+
}
1027+
1028+
.quarto-notebook .cell-container .cell-decorator {
1029+
padding-top: 1.5em;
1030+
padding-right: 1em;
1031+
text-align: right;
1032+
}
1033+
1034+
.quarto-notebook h2 {
1035+
border-bottom: none;
1036+
}
1037+
10201038
.sidebar .quarto-alternate-formats a,
10211039
.sidebar .quarto-alternate-notebooks a {
10221040
text-decoration: none;

src/resources/formats/html/embed/template.ejs.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
</style>
1616

1717
<style>
18-
.cell {
19-
border-bottom: solid 1px;
20-
}
21-
2218
.quarto-embed-header {
2319
height: 3em;
2420
width: 100%;

src/resources/schema/document-links.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,11 @@
7878
required: [notebook]
7979

8080
description: "Configures the HTML viewer for notebooks that provide embedded content."
81+
82+
- name: notebook-view-style
83+
tags:
84+
formats: [$html-doc]
85+
schema:
86+
enum: [document, notebook]
87+
hidden: true
88+
description: "The style of document to render. Setting this to `notebook` will create additional notebook style affordances."

tests/docs/smoke-all/2023/01/05/notebook-preview.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ grid:
1919
body-width: 1000px
2020
margin-width: 300px
2121
gutter-width: 1.5rem
22-
theme: darkly
22+
theme: spacelab
2323

2424
notebook-view: true
2525
notebook-links: true

tests/docs/smoke-all/2023/01/05/plots.ipynb

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,17 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"id": "4170aedd",
5+
"id": "5627eef3",
66
"metadata": {},
77
"source": [
8-
"---\n",
9-
"title: This is a test notebook\n",
10-
"author: Charles Teague\n",
11-
"date: today\n",
12-
"---"
13-
]
14-
},
15-
{
16-
"cell_type": "markdown",
17-
"id": "21d4bff7",
18-
"metadata": {},
19-
"source": [
20-
"## Hi Dog\n",
21-
"This is a **test** of a markdown cell"
8+
"## Hi\n",
9+
"\n",
10+
"This is some markdown"
2211
]
2312
},
2413
{
2514
"cell_type": "code",
26-
"execution_count": 1,
15+
"execution_count": 7,
2716
"id": "3d8a9364",
2817
"metadata": {
2918
"tags": [
@@ -54,7 +43,7 @@
5443
},
5544
{
5645
"cell_type": "code",
57-
"execution_count": 2,
46+
"execution_count": 8,
5847
"id": "176f6946",
5948
"metadata": {},
6049
"outputs": [
@@ -83,7 +72,7 @@
8372
},
8473
{
8574
"cell_type": "code",
86-
"execution_count": 3,
75+
"execution_count": 9,
8776
"id": "ff7727fa",
8877
"metadata": {},
8978
"outputs": [
@@ -108,7 +97,7 @@
10897
},
10998
{
11099
"cell_type": "code",
111-
"execution_count": 4,
100+
"execution_count": 10,
112101
"id": "9b3f7a5f",
113102
"metadata": {},
114103
"outputs": [

0 commit comments

Comments
 (0)