-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext2chordpro.js
121 lines (98 loc) · 2.42 KB
/
text2chordpro.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
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
module.exports = {
isChordpro: function(input) {
return hasCurlyBraces(input);
},
fromText: function(input) {
title = "";
artist = "";
body = "";
output = "";
lines = input.split("\n");
if (lines.length >= 1) {
output += "{t:" + lines[0].trim() + "}\n";
}
if (lines.length >= 2) {
output += "{st:" + lines[1].trim() + "}\n";
}
chordLine = "";
for (i = 2; i < lines.length; i++) {
if (isAChordLine(lines[i])) {
chordLine = lines[i].trim();
} else {
output += mergeLine(chordLine, lines[i].trim()) + "\n";
chordLine = "";
}
}
return output;
}
};
function isAChord(word) {
result = false;
if (word.length > 0 && word[0] >= 'A' && word[0] <= 'G') {
result = true;
}
return result;
}
function isAChordLine(line) {
isChord = true;
words = line.split(" ");
if (words.length == 0) {
isChord = false;
}
for (j = 0; j < words.length; j++) {
if (words[j].trim().length > 0 && !isAChord(words[j])) {
isChord = false;
}
}
return isChord;
}
function mergeLine(chordLine, lyricsLine) {
mergedLine = "";
chordLineIndex = 0;
if (chordLine.length == 0) {
mergedLine = lyricsLine;
} else {
maxLength = lyricsLine.length;
if (chordLine.length > maxLength) {
maxLength = chordLine.length;
}
for (k = 0; k < lyricsLine.length; k++) {
if (chordLineIndex < chordLine.length) {
if (chordLine[chordLineIndex] != ' ') {
mergedLine += "[";
while (chordLineIndex < chordLine.length && chordLine[chordLineIndex] != ' ') {
mergedLine += chordLine[chordLineIndex];
chordLineIndex++;
}
mergedLine += "]";
} else {
chordLineIndex++;
}
}
if (k < lyricsLine.length) {
mergedLine += lyricsLine[k];
}
}
}
while (chordLineIndex < chordLine.length) {
if (chordLine[chordLineIndex] != ' ') {
mergedLine += "[";
while (chordLineIndex < chordLine.length && chordLine[chordLineIndex] != ' ') {
mergedLine += chordLine[chordLineIndex];
chordLineIndex++;
}
mergedLine += "]";
} else {
mergedLine += " ";
chordLineIndex++;
}
}
return mergedLine;
}
function hasCurlyBraces(input) {
result = false;
if (input.indexOf("{") >= 0 && input.indexOf("}") >= 0) {
result = true;
}
return result;
}