Skip to content

Commit 15872fc

Browse files
committed
Add pandoc jats templates
since jats templates can’t be emitted from pandoc directly, convert this all to just download the source and get the templates from there.
1 parent 259af84 commit 15872fc

File tree

6 files changed

+459
-51
lines changed

6 files changed

+459
-51
lines changed

package/src/common/update-pandoc.ts

Lines changed: 69 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { archiveBinaryDependency } from "./archive-binary-dependencies.ts";
2020

2121
import { execProcess } from "../../../src/core/process.ts";
2222
import { configureDependency } from "./dependencies/dependencies.ts";
23+
import { download, unzip } from "../util/utils.ts";
2324

2425
export function updatePandoc() {
2526
return new Command()
@@ -54,69 +55,86 @@ export function updatePandoc() {
5455
// Call archive-bin-deps for this file
5556
await withWorkingDir(async (workingDir) => {
5657
await archiveBinaryDependency(pandocDependency, workingDir);
57-
});
5858

59-
// Configure this version of pandoc
60-
await configureDependency(pandocDependency, configuration);
59+
// Configure this version of pandoc
60+
await configureDependency(pandocDependency, configuration);
6161

62-
// Generate templates
63-
await writePandocTemplates(configuration);
62+
// Generate templates
63+
await writePandocTemplates(configuration, version, workingDir);
64+
});
6465
});
6566
}
6667

67-
async function writePandocTemplates(config: Configuration) {
68-
info("Reading latest pandoc templates...");
68+
async function writePandocTemplates(
69+
config: Configuration,
70+
version: string,
71+
workingDir: string,
72+
) {
73+
info("Reading pandoc templates...");
6974
const formatSrcDir = join(
7075
config.directoryInfo.src,
7176
"resources",
7277
"formats",
7378
);
74-
const binPath = config.directoryInfo.bin;
75-
const formatTemplates = [{
76-
pandoc: "html",
77-
output: join(
78-
formatSrcDir,
79-
"html",
80-
"pandoc",
81-
"html.template",
82-
),
83-
}, {
84-
pandoc: "revealjs",
85-
output: join(formatSrcDir, "revealjs", "pandoc", "revealjs.template"),
86-
}, {
87-
pandoc: "latex",
88-
output: join(formatSrcDir, "pdf", "pandoc", "latex.template"),
89-
}];
90-
for (const temp of formatTemplates) {
91-
info(`> ${temp.pandoc}`);
92-
const template = await readTemplate(temp.pandoc, binPath);
93-
if (template) {
94-
ensureDirSync(dirname(temp.output));
95-
Deno.writeTextFileSync(temp.output, template);
96-
} else {
97-
throw new Error("Failed to read an expected template.");
79+
80+
const srcZipUrl =
81+
`https://github.com/jgm/pandoc/archive/refs/tags/${version}.zip`;
82+
83+
const pandocDir = `pandoc-${version}`;
84+
const zipFile = join(workingDir, "pandoc");
85+
await download(srcZipUrl, zipFile);
86+
await unzip(zipFile, workingDir);
87+
88+
// Jats templates are multi-part templates that
89+
// are not properly emitted by pandoc itself, so download
90+
// them from source instead
91+
const templateDir = join(workingDir, pandocDir, "data", "templates");
92+
const jatsOutDir = join(
93+
formatSrcDir,
94+
"jats",
95+
"pandoc",
96+
"default-templates",
97+
);
98+
const htmlOutdir = join(
99+
formatSrcDir,
100+
"html",
101+
"pandoc",
102+
);
103+
const latexOutdir = join(formatSrcDir, "pdf", "pandoc");
104+
const revealOutdir = join(formatSrcDir, "revealjs", "pandoc");
105+
106+
const templateDirFiles: Record<string, Array<{ from: string; to?: string }>> =
107+
{
108+
[jatsOutDir]: [
109+
{ from: "affiliations.jats" },
110+
{ from: "article.jats_publishing" },
111+
{ from: "default.jats_archiving" },
112+
{ from: "default.jats_articleauthoring" },
113+
{ from: "default.jats_publishing" },
114+
],
115+
[htmlOutdir]: [
116+
{ from: "default.html5", to: "html.template" },
117+
],
118+
[revealOutdir]: [
119+
{ from: "default.revealjs", to: "revealjs.template" },
120+
],
121+
[latexOutdir]: [
122+
{ from: "default.latex", to: "latex.template" },
123+
],
124+
};
125+
126+
// Move templates
127+
for (const outDir of Object.keys(templateDirFiles)) {
128+
ensureDirSync(outDir);
129+
for (const file of templateDirFiles[outDir]) {
130+
info(`> ${file.from}`);
131+
Deno.copyFileSync(
132+
join(templateDir, file.from),
133+
join(outDir, file.to || file.from),
134+
);
98135
}
99136
}
137+
100138
info("done.");
101139
info("");
102140
}
103-
104-
async function readTemplate(format: string, bin: string): Promise<string> {
105-
const result = await execProcess({
106-
cmd: [join(bin, "tools", "pandoc"), "--print-default-template", format],
107-
stdout: "piped",
108-
stderr: "piped",
109-
});
110-
111-
if (result.success) {
112-
if (result.stdout) {
113-
return result.stdout;
114-
} else {
115-
return "";
116-
}
117-
} else {
118-
throw new Error(
119-
`Failed to read default template for ${format} from Pandoc`,
120-
);
121-
}
122-
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
$--
2+
$-- Affiliations
3+
$--
4+
<aff id="aff-$it.id$">
5+
$-- wrap affiliation if it has a known institution identifier
6+
$if(it.group)$
7+
<institution content-type="group">${it.group}</institution>
8+
$endif$
9+
$if(it.department)$
10+
<institution content-type="dept">${it.department}</institution>
11+
$endif$
12+
<institution-wrap>
13+
$if(it.organization)$
14+
<institution>${it.organization}</institution>
15+
$else$
16+
<institution>${it.name}</institution>
17+
$endif$
18+
$if(it.isni)$
19+
<institution-id institution-id-type="ISNI">${it.isni}</institution-id>
20+
$endif$
21+
$if(it.ringgold)$
22+
<institution-id institution-id-type="Ringgold">${it.ringgold}</institution-id>
23+
$endif$
24+
$if(it.ror)$
25+
<institution-id institution-id-type="ROR">${it.ror}</institution-id>
26+
$endif$
27+
$for(it.pid)$
28+
<institution-id institution-id-type="${it.type}">${it.id}</institution-id>
29+
$endfor$
30+
</institution-wrap>$if(it.street-address)$,
31+
$for(it.street-address)$
32+
<addr-line>${it}</addr-line>$sep$,
33+
$endfor$
34+
$else$$if(it.city)$, <city>$it.city$</city>$endif$$endif$$if(it.country)$,
35+
<country$if(it.country-code)$ country="$it.country-code$"$endif$>$it.country$</country>$endif$
36+
</aff>

0 commit comments

Comments
 (0)