@@ -20,6 +20,7 @@ import { archiveBinaryDependency } from "./archive-binary-dependencies.ts";
20
20
21
21
import { execProcess } from "../../../src/core/process.ts" ;
22
22
import { configureDependency } from "./dependencies/dependencies.ts" ;
23
+ import { download , unzip } from "../util/utils.ts" ;
23
24
24
25
export function updatePandoc ( ) {
25
26
return new Command ( )
@@ -54,69 +55,86 @@ export function updatePandoc() {
54
55
// Call archive-bin-deps for this file
55
56
await withWorkingDir ( async ( workingDir ) => {
56
57
await archiveBinaryDependency ( pandocDependency , workingDir ) ;
57
- } ) ;
58
58
59
- // Configure this version of pandoc
60
- await configureDependency ( pandocDependency , configuration ) ;
59
+ // Configure this version of pandoc
60
+ await configureDependency ( pandocDependency , configuration ) ;
61
61
62
- // Generate templates
63
- await writePandocTemplates ( configuration ) ;
62
+ // Generate templates
63
+ await writePandocTemplates ( configuration , version , workingDir ) ;
64
+ } ) ;
64
65
} ) ;
65
66
}
66
67
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..." ) ;
69
74
const formatSrcDir = join (
70
75
config . directoryInfo . src ,
71
76
"resources" ,
72
77
"formats" ,
73
78
) ;
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
+ ) ;
98
135
}
99
136
}
137
+
100
138
info ( "done." ) ;
101
139
info ( "" ) ;
102
140
}
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
- }
0 commit comments