@@ -20,6 +20,7 @@ import { archiveBinaryDependency } from "./archive-binary-dependencies.ts";
2020
2121import  {  execProcess  }  from  "../../../src/core/process.ts" ; 
2222import  {  configureDependency  }  from  "./dependencies/dependencies.ts" ; 
23+ import  {  download ,  unzip  }  from  "../util/utils.ts" ; 
2324
2425export  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 }  ; 
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 }  , 
120-     ) ; 
121-   } 
122- } 
0 commit comments