-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (68 loc) · 1.43 KB
/
index.js
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
const fs = require('fs');
const tinyError = require('tiny-error');
const ditty = require('parse-ditty');
const entities = require('stringify-entities');
const pdf = require('html-pdf');
const renderSong = (song) => (
`<div>
<h2>${song.number}. ${song.title}</h2>
${
song.parts.map((part) => (
` <p class="${part.type}">${
entities(part.lyrics
.replace(/\n$/, '')
.replace(/(?:– *)?\n/g, ' — ')
)
}</p>
`
)).join('')
}</div>
`
);
const style = (
`html {
font-size: 9pt;
line-height: 1.3rem;
font-family: Andada, serif;
}
* {
font-size: inherit;
margin: 0;
padding: 0;
}
h2 {
margin-top: 1.5em;
}
p:nth-child(2n + 1) {
padding-left: 2em;
}
p.refrain {
font-style: italic;
}
`
);
module.exports = (args) => {
if (args.length < 2) throw tinyError(
'Usage: ditty-to-pdf ...<file> <destination>'
);
const destination = args[args.length - 1];
const inputFiles = args.slice(0, args.length - 1);
const rawSongs = inputFiles.map((filePath) => (
fs.readFileSync(filePath, 'utf8')
));
const renderedSongs = rawSongs.map(ditty).map(renderSong);
const html = (
`<!doctype html>
<style>${style}</style>
${renderedSongs.join('')}
`
);
pdf.create(html, {
format: 'A5',
border: { top: '8mm', right: '12mm', bottom: '8mm', left: '12mm' },
})
.toFile(destination, (error, result) => {
if (error) throw error;
process.stdout.write(`${result.filename}\n`);
});
};