Skip to content

Commit 02737f5

Browse files
committed
Format Display Names
Formats now have display names which are used in the HTML format.
1 parent 5690e24 commit 02737f5

File tree

14 files changed

+185
-67
lines changed

14 files changed

+185
-67
lines changed

src/command/render/render-contexts.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ import {
3131
kBibliography,
3232
kCache,
3333
kCss,
34+
kDisplayName,
3435
kEcho,
3536
kEngine,
3637
kExecuteDaemon,
3738
kExecuteDaemonRestart,
3839
kExecuteDebug,
3940
kExecuteEnabled,
41+
kExtensionName,
4042
kHeaderIncludes,
4143
kIncludeAfter,
4244
kIncludeAfterBody,
@@ -156,7 +158,9 @@ export async function resolveFormatsFromMetadata(
156158
const config = mergeFormatMetadata(baseFormat, format);
157159

158160
// apply any metadata filter
159-
const resolveFormat = defaultWriterFormat(to).resolveFormat;
161+
const defaultFormat = defaultWriterFormat(to);
162+
config[kDisplayName] = config[kDisplayName] || defaultFormat[kDisplayName];
163+
const resolveFormat = defaultFormat.resolveFormat;
160164
if (resolveFormat) {
161165
resolveFormat(config);
162166
}
@@ -638,6 +642,8 @@ const readExtensionFormat = async (
638642
if (extensionFormat) {
639643
const extensionMetadata =
640644
(extensionFormat[formatDesc.baseFormat] || {}) as Metadata;
645+
extensionMetadata[kExtensionName] = extensionMetadata[kExtensionName] ||
646+
formatDesc.extension;
641647

642648
const formats = await resolveFormatsFromMetadata(
643649
extensionMetadata,

src/config/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
export const kMetadataFormat = "format";
99

10+
export const kDisplayName = "display-name";
11+
export const kExtensionName = "extension-name";
1012
export const kRenderDefaults = "render";
1113
export const kExecuteDefaults = "execute";
1214
export const kPandocDefaults = "pandoc";

src/config/metadata.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ import { mergeArrayCustomizer } from "../core/config.ts";
1616
import { Schema } from "../core/lib/yaml-schema/types.ts";
1717

1818
import {
19+
kDisplayName,
1920
kExecuteDefaults,
2021
kExecuteDefaultsKeys,
2122
kExecuteEnabled,
23+
kExtensionName,
2224
kHeaderIncludes,
2325
kIncludeAfter,
2426
kIncludeBefore,
@@ -169,6 +171,8 @@ export function metadataAsFormat(metadata: Metadata): Format {
169171
// allow stuff already sorted into a top level key through unmodified
170172
if (
171173
[
174+
kDisplayName,
175+
kExtensionName,
172176
kRenderDefaults,
173177
kExecuteDefaults,
174178
kPandocDefaults,
@@ -178,7 +182,9 @@ export function metadataAsFormat(metadata: Metadata): Format {
178182
.includes(key)
179183
) {
180184
// special case for 'execute' as boolean
181-
if (typeof (metadata[key]) == "boolean") {
185+
if ([kDisplayName, kExtensionName].includes(key)) {
186+
format[key] = metadata[key];
187+
} else if (typeof (metadata[key]) == "boolean") {
182188
if (key === kExecuteDefaults) {
183189
format[key] = format[key] || {};
184190
format[kExecuteDefaults][kExecuteEnabled] = metadata[key];

src/config/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
kCrossrefThmTitle,
4949
kCss,
5050
kDfPrint,
51+
kDisplayName,
5152
kEcho,
5253
kEmbedResources,
5354
kEngine,
@@ -62,6 +63,7 @@ import {
6263
kExecuteDebug,
6364
kExecuteEnabled,
6465
kExecuteIpynb,
66+
kExtensionName,
6567
kFigAlign,
6668
kFigDpi,
6769
kFigEnv,
@@ -307,6 +309,8 @@ export interface FormatExtras {
307309

308310
// pandoc output format
309311
export interface Format {
312+
[kDisplayName]?: string;
313+
[kExtensionName]?: string;
310314
render: FormatRender;
311315
execute: FormatExecute;
312316
pandoc: FormatPandoc;

src/format/docx/format-docx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const kIconWarning = "icon-warning";
1919

2020
export function docxFormat(): Format {
2121
return mergeConfigs(
22-
createWordprocessorFormat("docx"),
22+
createWordprocessorFormat("MS Word", "docx"),
2323
{
2424
formatExtras: () => {
2525
return {

src/format/epub/format-epub.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { createEbookFormat } from "../formats-shared.ts";
2020

2121
export function epubFormat(): Format {
2222
return mergeConfigs(
23-
createEbookFormat("epub"),
23+
createEbookFormat("ePub", "epub"),
2424
{
2525
extensions: {
2626
book: epubBookExtension,

src/format/formats-shared.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
kCodeTools,
1717
kDefaultImageExtension,
1818
kDfPrint,
19+
kDisplayName,
1920
kEcho,
2021
kError,
2122
kEval,
@@ -74,9 +75,13 @@ import { Format } from "../config/types.ts";
7475
import { formatResourcePath } from "../core/resources.ts";
7576
import { quartoConfig } from "../core/quarto.ts";
7677

77-
export function createFormat(ext: string, ...formats: Array<unknown>): Format {
78+
export function createFormat(
79+
displayName: string,
80+
ext: string,
81+
...formats: Array<unknown>
82+
): Format {
7883
return mergeConfigs(
79-
defaultFormat(),
84+
defaultFormat(displayName),
8085
...formats,
8186
{
8287
render: {
@@ -87,10 +92,11 @@ export function createFormat(ext: string, ...formats: Array<unknown>): Format {
8792
}
8893

8994
export function createHtmlFormat(
95+
displayName: string,
9096
figwidth: number,
9197
figheight: number,
9298
) {
93-
return createFormat("html", {
99+
return createFormat(displayName, "html", {
94100
metadata: {
95101
[kLang]: "en",
96102
[kFigResponsive]: true,
@@ -113,11 +119,12 @@ export function createHtmlFormat(
113119
}
114120

115121
export function createHtmlPresentationFormat(
122+
displayName: string,
116123
figwidth: number,
117124
figheight: number,
118125
): Format {
119126
return mergeConfigs(
120-
createHtmlFormat(figwidth, figheight),
127+
createHtmlFormat(displayName, figwidth, figheight),
121128
{
122129
metadata: {
123130
[kFigResponsive]: false,
@@ -130,8 +137,8 @@ export function createHtmlPresentationFormat(
130137
);
131138
}
132139

133-
export function createEbookFormat(ext: string): Format {
134-
return createFormat(ext, {
140+
export function createEbookFormat(displayName: string, ext: string): Format {
141+
return createFormat(displayName, ext, {
135142
formatExtras: () => {
136143
return {
137144
[kIncludeInHeader]: [
@@ -153,8 +160,11 @@ export function createEbookFormat(ext: string): Format {
153160
});
154161
}
155162

156-
export function createWordprocessorFormat(ext: string): Format {
157-
return createFormat(ext, {
163+
export function createWordprocessorFormat(
164+
displayName: string,
165+
ext: string,
166+
): Format {
167+
return createFormat(displayName, ext, {
158168
render: {
159169
[kPageWidth]: 6.5,
160170
},
@@ -168,17 +178,18 @@ export function createWordprocessorFormat(ext: string): Format {
168178
});
169179
}
170180

171-
export function plaintextFormat(ext: string): Format {
172-
return createFormat(ext, {
181+
export function plaintextFormat(displayName: string, ext: string): Format {
182+
return createFormat(displayName, ext, {
173183
pandoc: {
174184
standalone: true,
175185
[kDefaultImageExtension]: "png",
176186
},
177187
});
178188
}
179189

180-
function defaultFormat(): Format {
190+
function defaultFormat(displayName: string): Format {
181191
return {
192+
[kDisplayName]: displayName,
182193
execute: {
183194
[kFigWidth]: 7,
184195
[kFigHeight]: 5,

0 commit comments

Comments
 (0)