-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.ts
318 lines (282 loc) · 8.09 KB
/
transform.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import * as path from "@std/path";
import * as yaml from "@std/yaml";
import Handlebars from "npm:handlebars";
import { CodeMeta } from "./codemeta.ts";
import { gitOrgOrPerson, gitReleaseHash } from "./gitcmds.ts";
export function getFormatFromExt(
filename: string | undefined,
defaultFormat: string,
): string {
if (filename !== undefined) {
switch (path.extname(filename)) {
case ".cff":
return "cff";
case ".ts":
return "ts";
case ".js":
return "js"
case ".go":
return "go";
case ".py":
return "py";
case ".md":
return "md";
case ".hbs":
return "hbs";
case ".tmpl":
return "pdtmpl";
case ".pdtmpl":
return "pdtmpl";
}
}
return defaultFormat;
}
export function isSupportedFormat(format: string | undefined): boolean {
if (format === undefined) {
return false;
}
return ["cff", "ts", "js", "go", "py", "md", "hbs", "pdtmpl"].indexOf(format) > -1;
}
// FIXME: need to handle the special case renderings for README.md,
// INSTALL.md and the installer scripts.
export async function transform(
cm: CodeMeta,
format: string,
): Promise<string | undefined> {
if (!isSupportedFormat(format)) {
return undefined;
}
let obj: { [key: string]: any } = cm.toObject();
obj["project_name"] = path.basename(Deno.cwd());
obj["releaseHash"] = await gitReleaseHash();
if (obj['dateModified'] === undefined || obj['dateModified'] === '') {
const d = new Date();
const year = `${d.getFullYear()}`;
const month = `${d.getMonth() + 1}`.padStart(2, '0');
const day = `${d.getDate() + 1}`.padStart(2, '0');
obj['dateModified'] = `${year}-${month}-${day}`;
}
(obj['releaseDate'] === undefined) ? obj['releaseDate'] = obj['dateModified'] : '';
//obj['git_org_or_person'] = await gitOrgOrPerson();
let licenseText: string = "";
try {
licenseText = await Deno.readTextFile("LICENSE");
} catch (err) {
console.log(`warning: missing license file, ${err}`);
licenseText = "";
}
if (licenseText !== undefined && licenseText !== "") {
obj["licenseText"] = licenseText;
}
if (cm.codeRepository !== "") {
obj["repositoryLink"] = cm.codeRepository.replace("git+https", "https");
}
switch (format) {
case "cff":
return renderTemplate(obj, cffTemplateText);
case "ts":
return renderTemplate(obj, tsTemplateText);
case "js":
return renderTemplate(obj, tsTemplateText);
case "go":
return renderTemplate(obj, goTemplateText);
case "py":
return renderTemplate(obj, pyTemplateText);
case "md":
return renderTemplate(obj, mdTemplateText);
case "hbs":
return renderTemplate(obj, hbsTemplateText)?.replace(
"$$content$$",
"{{{content}}}",
);
case "pdtmpl": // render as Pandoc template
return renderTemplate(obj, hbsTemplateText)?.replace(
"$$content$$",
"${body}",
);
default:
return undefined;
}
}
export function renderTemplate(obj: {[key: string]: any}, tmpl: string): string | undefined {
const template = Handlebars.compile(tmpl);
if (template === undefined) {
console.log(`templates failed to compile, ${tmpl}`);
return undefined;
}
return template(obj);
}
const cffTemplateText = `
cff-version: 1.2.0
message: "If you use this software, please cite it as below."
type: software
{{#if name}}title: {{name}}{{/if}}
{{#if description}}abstract: "{{description}}"{{/if}}
{{#if author}}authors:
{{#each author}}
- family-names: {{familyName}}
given-names: {{givenName}}{{#if id}}
orcid: {{id}}{{/if}}{{#if email}}
email: {{email}}{{/if}}
{{/each}}{{/if}}
{{#if maintainer}}contacts:
{{#each maintainer}}
- family-names: {{familyName}}
given-names: {{givenName}}{{#if id}}
orcid: {{id}}{{/if}}{{#if email}}
email: {{email}}{{/if}}
{{/each}}{{/if}}
{{#if codeRepository}}repository-code: "{{codeRepository}}"{{/if}}
{{#if version}}version: {{version}}{{/if}}
{{#if datePublished}}date-released: {{datePublished}}{{/if}}
{{#if identifier}}doi: {{identifier}}{{/if}}
{{#if license}}license-url: "{{license}}"{{/if}}{{#if keywords}}
keywords:
{{#each keywords}}
- {{.}}
{{/each}}{{/if}}
`;
const tsTemplateText = `// {{name}} version and license information.
export const version = '{{version}}',
releaseDate = '{{releaseDate}}',
releaseHash = '{{releaseHash}}'{{#if licenseText}},
licenseText = ` + "`" + `
{{licenseText}}
` + "`{{/if}};";
const pyTemplateText = `# {{name}} version and license information.
export const version = '{{version}}',
releaseDate = '{{releaseDate}}',
releaseHash = '{{releaseHash}}'{{#if licenseText}},
licenseText = '''
{{licenseText}}
'''{{/if}};
`;
const goTemplateText = `package {{name}}
import (
"strings"
)
const (
// Version number of release
Version = "{{version}}"
// ReleaseDate, the date version.go was generated
ReleaseDate = "{{releaseDate}}"
// ReleaseHash, the Git hash when version.go was generated
ReleaseHash = "{{releaseHash}}"
{{#if licenseText}}
LicenseText = ` + "`" + `
{{licenseText}}
` + "`" + `{{/if}}
)
// FmtHelp lets you process a text block with simple curly brace markup.
func FmtHelp(src string, appName string, version string, releaseDate string, releaseHash string) string {
m := map[string]string {
"{app_name}": appName,
"{version}": version,
"{release_date}": releaseDate,
"{release_hash}": releaseHash,
}
for k, v := range m {
if strings.Contains(src, k) {
src = strings.ReplaceAll(src, k, v)
}
}
return src
}
`;
const mdTemplateText = `---
{{#if name}}title: {{name}}{{/if}}
{{#if description}}abstract: "{{description}}"{{/if}}
{{#if author}}authors:
{{#each author}}
- family_name: {{familyName}}
given_name: {{givenName}}{{#if id}}
orcid: {{id}}{{/if}}
{{/each}}{{/if}}
{{#if contributor}}contributor:
{{#each contributor}}
- family_name: {{familyName}}
given_name: {{givenName}}{{#if id}}
orcid: {{id}}{{/if}}
{{/each}}{{/if}}
{{#if maintainer}}maintainer:
{{#each maintainer}}
- family_name: {{familyName}}
given_name: {{givenName}}{{#if id}}
orcid: {{id}}{{/if}}
{{/each}}{{/if}}
{{#if codeRepository}}repository_code: {{codeRepository}}{{/if}}
{{#if version}}version: {{version}}{{/if}}
{{#if license}}license_url: {{license}}{{/if}}
{{#if operatingSystem}}operating_system:
{{#each operatingSystem}}
- {{.}}
{{/each}}{{/if}}
{{#if programmingLanguage}}programming_language:
{{#each programmingLanguage}}
- {{.}}
{{/each}}{{/if}}
{{#if keywords}}keywords:
{{#each keywords}}
- {{.}}
{{/each}}{{/if}}
{{#if datePublished}}date_released: {{datePublished}}{{/if}}
---
About this software
===================
## {{name}} {{version}}
{{#if author}}
### Authors
{{#each author}}
- {{givenName}} {{familyName}} {{id}}{{/each}}{{/if}}
{{#if contributor}}
### Contributors
{{#each contributor}}
- {{givenName}} {{familyName}} {{id}}{{/each}}{{/if}}
{{#if maintainer}}
### Maintainers
{{#each maintainer}}
- {{givenName}} {{familyName}} {{id}}{{/each}}{{/if}}
{{#if description}}{{description}}{{/if}}
{{#if license}}- License: <{{license}}>{{/if}}
{{#if codeRepository}}- GitHub: <{{codeRepository}}>{{/if}}
{{#if issueTracker}}- Issues: <{{issueTracker}}>{{/if}}
{{#if programmingLanguage}}
### Programming languages
{{#each programmingLanguage}}
- {{.}}
{{/each}}{{/if}}
{{#if operatingSystem}}
### Operating Systems
{{#each operatingSystem}}
- {{.}}
{{/each}}{{/if}}
{{#if softwareRequirements}}
### Software Requirements
{{#each softwareRequirements}}
- {{.}}
{{/each}}{{/if}}
`;
const hbsTemplateText = `<!DOCTYPE html>
<html lang="en-US">
<head>
<title>{{project_name}}</title>
<link rel="stylesheet" href="/css/site.css">
</head>
<body>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="index.html">README</a></li>
<li><a href="LICENSE">LICENSE</a></li>
<li><a href="INSTALL.html">INSTALL</a></li>
<li><a href="user_manual.html">User Manual</a></li>
<li><a href="about.html">About</a></li>
<li><a href="search.html">Search</a></li>
{{#if repositoryLink}} <li><a href="{{repositoryLink}}">GitHub</a></li>{{/if}}
</ul>
</nav>
<section>
$$content$$
</section>
</body>
</html>`;