openlyrics-parser
Version:
Parses and extracts data from OpenLyrics files, and creates them!
260 lines (259 loc) • 10.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Parser = void 0;
const fast_xml_parser_1 = require("fast-xml-parser");
class Parser {
constructor() {
this.lyricLineParser = new fast_xml_parser_1.XMLParser({
ignoreAttributes: false,
attributeNamePrefix: '',
isArray: (_n, jPath) => {
return ['beat.chord'].includes(jPath);
},
});
}
getSongMeta(olSong) {
var _a, _b, _c, _d;
return {
createdIn: (_a = olSong.createdIn) !== null && _a !== void 0 ? _a : '',
chordNotation: (_b = olSong.chordNotation) !== null && _b !== void 0 ? _b : '',
lang: (_c = olSong['xml:lang']) !== null && _c !== void 0 ? _c : '',
modifiedDate: olSong.modifiedDate != null ? new Date(olSong.modifiedDate) : null,
modifiedIn: (_d = olSong.modifiedIn) !== null && _d !== void 0 ? _d : '',
version: olSong.version.toString(),
};
}
getSongProperties(props) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
if (props.releaseDate != null) {
props.released = props.releaseDate;
}
return {
authors: this.getSongPropertyAuthors(props.authors),
ccliNo: (_b = (_a = props.ccliNo) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : '',
comments: this.getSongPropertyComments(props.comments),
copyright: (_d = (_c = props.copyright) === null || _c === void 0 ? void 0 : _c.toString()) !== null && _d !== void 0 ? _d : '',
key: (_e = props.key) !== null && _e !== void 0 ? _e : '',
keywords: (_f = props.keywords) !== null && _f !== void 0 ? _f : '',
publisher: (_g = props.publisher) !== null && _g !== void 0 ? _g : '',
released: (_j = (_h = props.released) === null || _h === void 0 ? void 0 : _h.toString()) !== null && _j !== void 0 ? _j : '',
songBooks: this.getSongPropertySongBooks(props.songbooks),
tempo: (_l = (_k = props.tempo) === null || _k === void 0 ? void 0 : _k['#text'].toString()) !== null && _l !== void 0 ? _l : '',
tempoType: (_o = (_m = props.tempo) === null || _m === void 0 ? void 0 : _m.type) !== null && _o !== void 0 ? _o : '',
themes: this.getSongPropertyThemes(props.themes),
timeSignature: (_p = props.timeSignature) !== null && _p !== void 0 ? _p : '',
titles: this.getSongPropertyTitles(props.titles),
transposition: (_r = (_q = props.transposition) === null || _q === void 0 ? void 0 : _q.toString()) !== null && _r !== void 0 ? _r : '',
variant: (_s = props.variant) !== null && _s !== void 0 ? _s : '',
verseOrder: (_t = props.verseOrder) !== null && _t !== void 0 ? _t : '',
version: (_v = (_u = props.version) === null || _u === void 0 ? void 0 : _u.toString()) !== null && _v !== void 0 ? _v : '',
};
}
getSongFormat(format) {
let application = '';
let tags = [];
if (format) {
application = format.tags.application;
tags = format.tags.tag.map((t) => {
var _a;
return {
name: t.name,
open: t.open,
close: (_a = t.close) !== null && _a !== void 0 ? _a : '',
};
});
}
return { application, tags };
}
getSongVerses(verses) {
var _a, _b, _c;
const versesArr = [];
if (verses) {
for (const v of verses) {
versesArr.push({
break: (_a = v.break) !== null && _a !== void 0 ? _a : '',
name: v.name,
lang: (_b = v.lang) !== null && _b !== void 0 ? _b : '',
transliteration: (_c = v.translit) !== null && _c !== void 0 ? _c : '',
lines: this.getVerseLines(v.lines),
});
}
}
return versesArr;
}
getSongInstruments(instruments) {
const instrumentsArr = [];
if (instruments) {
for (const i of instruments) {
instrumentsArr.push({
name: i.name,
lines: this.getInstrumentLines(i.lines),
});
}
}
return instrumentsArr;
}
getVerseLines(lines) {
const linesArr = [];
for (const line of lines) {
const rawLineTextAndXml = this.getStringOrTextProp(line);
const textAndXmlArr = this.parseLineTextForXml(rawLineTextAndXml);
linesArr.push({
break: this.getOptionalPropOnPossibleObject(line, 'break', ''),
content: this.getVerseContentObjects(textAndXmlArr),
part: this.getOptionalPropOnPossibleObject(line, 'part', ''),
repeat: this.getOptionalPropOnPossibleObject(line, 'repeat', ''),
});
}
return linesArr;
}
getInstrumentLines(lines) {
const linesArr = [];
for (const line of lines) {
const rawLineTextAndXml = this.getStringOrTextProp(line);
const textAndXmlArr = this.parseLineTextForXml(rawLineTextAndXml);
linesArr.push({
content: this.getInstrumentContentObjects(textAndXmlArr),
part: this.getOptionalPropOnPossibleObject(line, 'part', ''),
repeat: this.getOptionalPropOnPossibleObject(line, 'repeat', ''),
});
}
return linesArr;
}
parseLineTextForXml(str) {
return (str
.split(/(<[^/]+?>[\s\S]+?<\/.+?>)|(<[^/]+?\/>)/g)
.filter((x) => x !== '' && typeof x !== 'undefined'));
}
getVerseContentObjects(textAndXmlArr) {
var _a;
const contentArr = [];
for (const part of textAndXmlArr) {
if (part.startsWith('<')) {
const parsedTag = this.lyricLineParser.parse(part);
if (parsedTag.comment != null) {
contentArr.push({
type: 'comment',
value: parsedTag.comment,
});
}
else if (parsedTag.tag != null) {
contentArr.push({
type: 'tag',
name: parsedTag.tag.name,
value: (_a = parsedTag.tag['#text']) !== null && _a !== void 0 ? _a : '',
});
}
else if (parsedTag.chord != null) {
contentArr.push(this.getChordObject(parsedTag.chord));
}
}
else {
contentArr.push({
type: 'text',
value: part,
});
}
}
return contentArr;
}
getInstrumentContentObjects(textAndXmlArr) {
const contentArr = [];
for (const part of textAndXmlArr) {
const parsedTag = this.lyricLineParser.parse(part);
if (parsedTag.chord != null) {
contentArr.push(this.getChordObject(parsedTag.chord));
}
else if (parsedTag.beat != null) {
contentArr.push({
type: 'beat',
chords: parsedTag.beat.chord.map((c) => this.getChordObject(c)),
});
}
}
return contentArr;
}
getChordObject(chordObj) {
const chord = { type: 'chord' };
Object.keys(chordObj).forEach((k) => {
let keyName = k === '#text' ? 'value' : k;
if (keyName === 'name') {
keyName = 'root';
}
chord[keyName] = chordObj[k];
});
return chord;
}
getSongPropertyAuthors(authors) {
const authorsArr = [];
if (authors) {
for (const a of authors.author) {
authorsArr.push({
lang: this.getOptionalPropOnPossibleObject(a, 'lang', ''),
type: this.getOptionalPropOnPossibleObject(a, 'type', ''),
value: this.getStringOrTextProp(a),
});
}
}
return authorsArr;
}
getSongPropertyComments(comments) {
let commentArr = [];
if (comments) {
commentArr = comments.comment.map((c) => this.getStringOrTextProp(c));
}
return commentArr;
}
getSongPropertyThemes(themes) {
const titlesArr = [];
if (themes) {
for (const t of themes.theme) {
titlesArr.push({
lang: this.getOptionalPropOnPossibleObject(t, 'lang', ''),
value: this.getStringOrTextProp(t),
});
}
}
return titlesArr;
}
getSongPropertyTitles(titles) {
const titlesArr = [];
if (titles) {
for (const t of titles.title) {
titlesArr.push({
lang: this.getOptionalPropOnPossibleObject(t, 'lang', ''),
original: this.getOptionalPropOnPossibleObject(t, 'original', null),
transliteration: this.getOptionalPropOnPossibleObject(t, 'translit', ''),
value: this.getStringOrTextProp(t),
});
}
}
return titlesArr;
}
getSongPropertySongBooks(songBooks) {
var _a, _b;
const titlesArr = [];
if (songBooks) {
for (const t of songBooks.songbook) {
titlesArr.push({
entry: (_b = (_a = t.entry) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : '',
name: t.name,
});
}
}
return titlesArr;
}
getStringOrTextProp(str) {
return typeof str === 'string' ? str : str['#text'];
}
getOptionalPropOnPossibleObject(obj, propName, defaultVal) {
var _a;
if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean') {
return defaultVal;
}
else {
return (_a = obj[propName]) !== null && _a !== void 0 ? _a : defaultVal;
}
}
}
exports.Parser = Parser;