Skip to content

Commit 075919d

Browse files
committed
Improve list of pandoc variants
Automatically generate the list of reserved variants fixes #4080
1 parent ca545fb commit 075919d

File tree

3 files changed

+248
-87
lines changed

3 files changed

+248
-87
lines changed

package/src/common/update-pandoc.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ import { lines } from "../../../src/core/text.ts";
1818
import { pandoc } from "./dependencies/pandoc.ts";
1919
import { archiveBinaryDependency } from "./archive-binary-dependencies.ts";
2020

21-
import { execProcess } from "../../../src/core/process.ts";
2221
import { configureDependency } from "./dependencies/dependencies.ts";
2322
import { download, unzip } from "../util/utils.ts";
23+
import {
24+
pandocListFormatDefaultExtensions,
25+
pandocListFormats,
26+
} from "../../../src/core/pandoc/pandoc-formats.ts";
2427

2528
export function updatePandoc() {
2629
return new Command()
@@ -61,10 +64,63 @@ export function updatePandoc() {
6164

6265
// Generate templates
6366
await writePandocTemplates(configuration, version, workingDir);
67+
68+
// Generate variants
69+
await writeVariants(configuration);
6470
});
6571
});
6672
}
6773

74+
async function writeVariants(
75+
config: Configuration,
76+
) {
77+
info("Generating Pandoc extensions source file...");
78+
info("Reading pandoc formats and extensions");
79+
const formats = await pandocListFormats();
80+
const extensions: Set<string> = new Set();
81+
for (const format of formats) {
82+
const formatExtensions = await pandocListFormatDefaultExtensions(format);
83+
formatExtensions.forEach((ext) => {
84+
const bareExtension = ext.replace(/^[\+\-]/, "");
85+
extensions.add(bareExtension);
86+
});
87+
}
88+
const extArr = Array.from(extensions.values());
89+
info(` ${formats.length} formats.`);
90+
info(` ${extArr.length} extensions.`);
91+
92+
const extArrExpanded = extArr.toSorted().flatMap((ext) => {
93+
return [`"+${ext}"`, `"-${ext}"`];
94+
});
95+
96+
const extensionFile = join(
97+
config.directoryInfo.src,
98+
"core",
99+
"pandoc",
100+
"format-extension.ts",
101+
);
102+
103+
// Generate the extension list
104+
const tsContents = `
105+
/*
106+
* pandoc-extensions.ts
107+
*
108+
* Copyright (C) 2020-2022 Posit Software, PBC
109+
*
110+
*/
111+
//
112+
// THIS FILE IS GENERATED BY update-pandoc.ts. DO NOT EDIT MANUALLY
113+
//
114+
115+
export const kPandocExtensions = [
116+
${extArrExpanded.join(",\n ")}
117+
];
118+
`;
119+
120+
// Write the file
121+
Deno.writeTextFileSync(extensionFile, tsContents);
122+
}
123+
68124
async function writePandocTemplates(
69125
config: Configuration,
70126
version: string,
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
* pandoc-extensions.ts
3+
*
4+
* Copyright (C) 2020-2022 Posit Software, PBC
5+
*
6+
*/
7+
//
8+
// THIS FILE IS GENERATED BY update-pandoc.ts. DO NOT EDIT MANUALLY
9+
//
10+
11+
export const kPandocExtensions = [
12+
"+abbreviations",
13+
"-abbreviations",
14+
"+all_symbols_escapable",
15+
"-all_symbols_escapable",
16+
"+amuse",
17+
"-amuse",
18+
"+angle_brackets_escapable",
19+
"-angle_brackets_escapable",
20+
"+ascii_identifiers",
21+
"-ascii_identifiers",
22+
"+attributes",
23+
"-attributes",
24+
"+auto_identifiers",
25+
"-auto_identifiers",
26+
"+autolink_bare_uris",
27+
"-autolink_bare_uris",
28+
"+backtick_code_blocks",
29+
"-backtick_code_blocks",
30+
"+blank_before_blockquote",
31+
"-blank_before_blockquote",
32+
"+blank_before_header",
33+
"-blank_before_header",
34+
"+bracketed_spans",
35+
"-bracketed_spans",
36+
"+citations",
37+
"-citations",
38+
"+compact_definition_lists",
39+
"-compact_definition_lists",
40+
"+definition_lists",
41+
"-definition_lists",
42+
"+east_asian_line_breaks",
43+
"-east_asian_line_breaks",
44+
"+element_citations",
45+
"-element_citations",
46+
"+emoji",
47+
"-emoji",
48+
"+empty_paragraphs",
49+
"-empty_paragraphs",
50+
"+epub_html_exts",
51+
"-epub_html_exts",
52+
"+escaped_line_breaks",
53+
"-escaped_line_breaks",
54+
"+example_lists",
55+
"-example_lists",
56+
"+fancy_lists",
57+
"-fancy_lists",
58+
"+fenced_code_attributes",
59+
"-fenced_code_attributes",
60+
"+fenced_code_blocks",
61+
"-fenced_code_blocks",
62+
"+fenced_divs",
63+
"-fenced_divs",
64+
"+footnotes",
65+
"-footnotes",
66+
"+four_space_rule",
67+
"-four_space_rule",
68+
"+gfm_auto_identifiers",
69+
"-gfm_auto_identifiers",
70+
"+grid_tables",
71+
"-grid_tables",
72+
"+gutenberg",
73+
"-gutenberg",
74+
"+hard_line_breaks",
75+
"-hard_line_breaks",
76+
"+header_attributes",
77+
"-header_attributes",
78+
"+ignore_line_breaks",
79+
"-ignore_line_breaks",
80+
"+implicit_figures",
81+
"-implicit_figures",
82+
"+implicit_header_references",
83+
"-implicit_header_references",
84+
"+inline_code_attributes",
85+
"-inline_code_attributes",
86+
"+inline_notes",
87+
"-inline_notes",
88+
"+intraword_underscores",
89+
"-intraword_underscores",
90+
"+latex_macros",
91+
"-latex_macros",
92+
"+line_blocks",
93+
"-line_blocks",
94+
"+link_attributes",
95+
"-link_attributes",
96+
"+lists_without_preceding_blankline",
97+
"-lists_without_preceding_blankline",
98+
"+literate_haskell",
99+
"-literate_haskell",
100+
"+markdown_attribute",
101+
"-markdown_attribute",
102+
"+markdown_in_html_blocks",
103+
"-markdown_in_html_blocks",
104+
"+mmd_header_identifiers",
105+
"-mmd_header_identifiers",
106+
"+mmd_link_attributes",
107+
"-mmd_link_attributes",
108+
"+mmd_title_block",
109+
"-mmd_title_block",
110+
"+multiline_tables",
111+
"-multiline_tables",
112+
"+native_divs",
113+
"-native_divs",
114+
"+native_numbering",
115+
"-native_numbering",
116+
"+native_spans",
117+
"-native_spans",
118+
"+ntb",
119+
"-ntb",
120+
"+old_dashes",
121+
"-old_dashes",
122+
"+pandoc_title_block",
123+
"-pandoc_title_block",
124+
"+pipe_tables",
125+
"-pipe_tables",
126+
"+raw_attribute",
127+
"-raw_attribute",
128+
"+raw_html",
129+
"-raw_html",
130+
"+raw_markdown",
131+
"-raw_markdown",
132+
"+raw_tex",
133+
"-raw_tex",
134+
"+rebase_relative_paths",
135+
"-rebase_relative_paths",
136+
"+short_subsuperscripts",
137+
"-short_subsuperscripts",
138+
"+shortcut_reference_links",
139+
"-shortcut_reference_links",
140+
"+simple_tables",
141+
"-simple_tables",
142+
"+smart",
143+
"-smart",
144+
"+sourcepos",
145+
"-sourcepos",
146+
"+space_in_atx_header",
147+
"-space_in_atx_header",
148+
"+spaced_reference_links",
149+
"-spaced_reference_links",
150+
"+startnum",
151+
"-startnum",
152+
"+strikeout",
153+
"-strikeout",
154+
"+styles",
155+
"-styles",
156+
"+subscript",
157+
"-subscript",
158+
"+superscript",
159+
"-superscript",
160+
"+table_captions",
161+
"-table_captions",
162+
"+task_lists",
163+
"-task_lists",
164+
"+tex_math_dollars",
165+
"-tex_math_dollars",
166+
"+tex_math_double_backslash",
167+
"-tex_math_double_backslash",
168+
"+tex_math_single_backslash",
169+
"-tex_math_single_backslash",
170+
"+xrefs_name",
171+
"-xrefs_name",
172+
"+xrefs_number",
173+
"-xrefs_number",
174+
"+yaml_metadata_block",
175+
"-yaml_metadata_block",
176+
];

src/core/pandoc/pandoc-formats.ts

Lines changed: 15 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { execProcess } from "../process.ts";
1010
import { pandocBinaryPath, resourcePath } from "../resources.ts";
1111
import { lines } from "../text.ts";
1212
import { readYaml } from "../yaml.ts";
13+
import { kPandocExtensions } from "./format-extension.ts";
1314

1415
export const kYamlMetadataBlock = "yaml_metadata_block";
1516

@@ -26,6 +27,19 @@ export async function pandocListFormats() {
2627
}
2728
}
2829

30+
export async function pandocListFormatDefaultExtensions(format: string) {
31+
const result = await execProcess({
32+
cmd: [pandocBinaryPath(), "--list-extensions", format],
33+
stdout: "piped",
34+
});
35+
if (result.success) {
36+
return lines(result.stdout!)
37+
.filter((line) => line.length > 0);
38+
} else {
39+
return Promise.reject();
40+
}
41+
}
42+
2943
// yield a new format w/ extensions enabled and disabled
3044
export function pandocFormat(
3145
format: string,
@@ -188,7 +202,7 @@ function breakFormatString(
188202
const variants: string[] = [];
189203
const modifiers: string[] = [];
190204
variantCommands.forEach((cmd) => {
191-
if (pandocVariants.includes(cmd)) {
205+
if (kPandocExtensions.includes(cmd)) {
192206
variants.push(cmd);
193207
} else {
194208
modifiers.push(cmd);
@@ -214,88 +228,3 @@ function breakFormatString(
214228
}
215229
}
216230
}
217-
218-
const pandocVariants = [
219-
"-abbreviations",
220-
"+all_symbols_escapable",
221-
"-amuse",
222-
"-angle_brackets_escapable",
223-
"-ascii_identifiers",
224-
"-attributes",
225-
"+auto_identifiers",
226-
"-autolink_bare_uris",
227-
"+backtick_code_blocks",
228-
"+blank_before_blockquote",
229-
"+blank_before_header",
230-
"+bracketed_spans",
231-
"+citations",
232-
"-compact_definition_lists",
233-
"+definition_lists",
234-
"-east_asian_line_breaks",
235-
"-element_citations",
236-
"-emoji",
237-
"-empty_paragraphs",
238-
"-epub_html_exts",
239-
"+escaped_line_breaks",
240-
"+example_lists",
241-
"+fancy_lists",
242-
"+fenced_code_attributes",
243-
"+fenced_code_blocks",
244-
"+fenced_divs",
245-
"+footnotes",
246-
"-four_space_rule",
247-
"-gfm_auto_identifiers",
248-
"+grid_tables",
249-
"-gutenberg",
250-
"-hard_line_breaks",
251-
"+header_attributes",
252-
"-ignore_line_breaks",
253-
"+implicit_figures",
254-
"+implicit_header_references",
255-
"+inline_code_attributes",
256-
"+inline_notes",
257-
"+intraword_underscores",
258-
"+latex_macros",
259-
"+line_blocks",
260-
"+link_attributes",
261-
"-lists_without_preceding_blankline",
262-
"-literate_haskell",
263-
"-markdown_attribute",
264-
"+markdown_in_html_blocks",
265-
"-mmd_header_identifiers",
266-
"-mmd_link_attributes",
267-
"-mmd_title_block",
268-
"+multiline_tables",
269-
"+native_divs",
270-
"+native_spans",
271-
"-native_numbering",
272-
"-ntb",
273-
"-old_dashes",
274-
"+pandoc_title_block",
275-
"+pipe_tables",
276-
"+raw_attribute",
277-
"+raw_html",
278-
"+raw_tex",
279-
"-raw_markdown",
280-
"-rebase_relative_paths",
281-
"-short_subsuperscripts",
282-
"+shortcut_reference_links",
283-
"+simple_tables",
284-
"+smart",
285-
"-sourcepos",
286-
"+space_in_atx_header",
287-
"-spaced_reference_links",
288-
"+startnum",
289-
"+strikeout",
290-
"+subscript",
291-
"+superscript",
292-
"-styles",
293-
"+task_lists",
294-
"+table_captions",
295-
"+tex_math_dollars",
296-
"-tex_math_double_backslash",
297-
"-tex_math_single_backslash",
298-
"-xrefs_name",
299-
"-xrefs_number",
300-
"+yaml_metadata_block",
301-
];

0 commit comments

Comments
 (0)