-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasySlidesLyricsConverter.ts
92 lines (76 loc) · 2.23 KB
/
easySlidesLyricsConverter.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
// ---
// Parser which parses the initial XML from easy slides to markdown files
// Should be done only once
// ---
import _ from 'lodash';
import fsExtra from 'fs-extra';
import xml2js from 'xml2js';
import fs from 'fs';
import path from 'path';
import dotenv from 'dotenv';
dotenv.config();
const OUTPUT_DIR = './out/easy_slides';
const EMPTY_STRING = '';
const EMPTY_SPACE = ' ';
const parser = new xml2js.Parser({});
const rawXML = fs.readFileSync(
path.join(__dirname, process.env.FOLDER_INPUT_EASY_SLIDES),
);
const xmlAsString = rawXML.toString();
const generateFileNameByTitle = (title: string) => {
const normalizedTitle = title
.replaceAll('/', EMPTY_STRING)
.replaceAll('!', EMPTY_STRING);
const fileName = _.capitalize(
_.trimStart(_.trimEnd(normalizedTitle))
.split(EMPTY_SPACE)
.filter(Boolean)
.join(EMPTY_SPACE)
.toLowerCase(),
);
return `${OUTPUT_DIR}/${fileName}.txt`;
};
const transformSong = (parsedSongAsXML: {
Title1: string[];
Contents: string[];
Sequence: string[];
}) => {
const { Title1, Contents, Sequence } = parsedSongAsXML;
const title = Title1.join(EMPTY_STRING);
const syncLyrics = Contents.join(EMPTY_STRING);
const sequenceAsString = Sequence.join(EMPTY_STRING);
const separators = syncLyrics.match(/(\[).*/gim);
return {
title,
syncLyrics,
sequenceAsString,
separators,
};
};
const writeToFileAndReturn = (item: {
title: string;
syncLyrics: string;
sequenceAsString: string;
separators: string;
}) => {
const { title, syncLyrics, sequenceAsString, separators } = item;
const exportFileNamePath = generateFileNameByTitle(title);
const enrichedMarkdownContent = `[title] ${title}
[sequence] ${sequenceAsString}
${syncLyrics}
`;
fs.writeFileSync(exportFileNamePath, enrichedMarkdownContent);
return item;
};
(async () => {
fsExtra.emptyDirSync(OUTPUT_DIR);
const result = await parser.parseStringPromise(xmlAsString);
const parsedSongs =
result.Easyslides.Item.map(transformSong).map(writeToFileAndReturn);
const uniqueSeparators = _.uniq(
_.flatten(parsedSongs.map(({ separators }: any) => separators)),
)
.sort()
.join(EMPTY_STRING);
console.log(`The unique separators are ${uniqueSeparators}`);
})();