Escape characters when needed

This commit is contained in:
Matthias Neeracher 2007-05-29 08:52:53 +00:00
parent 2fdd04ac45
commit 9027c71510

View File

@ -148,6 +148,43 @@ static std::string LilypondPitchName(int8_t pitch, bool useSharps)
return kScale[pitch+1] + std::string("es"); return kScale[pitch+1] + std::string("es");
} }
static std::string EscapeSyllable(std::string syll)
{
for (size_t i=0; i<syll.size(); ++i)
if (isalpha(syll[i]))
continue;
else
switch (syll[i]) {
case '-':
case ':':
case '.':
case ',':
case ';':
case '\'':
case '_':
continue;
default:
goto escape;
}
//
// Purely alphabetic syllable, no need to escape.
//
return syll;
escape:
size_t q=0;
while ((q = syll.find_first_of('"', q)) != std::string::npos) {
syll.replace(q, 1, "\\\"", 2);
q += 2;
}
q = 0;
while ((q = syll.find_first_of('_', q)) != std::string::npos) {
syll.replace(q, 1, "\"_\"", 3);
q += 3;
}
return '"'+syll+'"';
}
void VLLilypondWriter::VisitNote(VLLyricsNote & n) void VLLilypondWriter::VisitNote(VLLyricsNote & n)
{ {
std::string nm = LilypondPitchName(n.fPitch, fUseSharps); std::string nm = LilypondPitchName(n.fPitch, fUseSharps);
@ -182,7 +219,7 @@ void VLLilypondWriter::VisitNote(VLLyricsNote & n)
if (n.fLyrics.size() <= i || !n.fLyrics[i]) { if (n.fLyrics.size() <= i || !n.fLyrics[i]) {
fL[i] += " \\skip1"; fL[i] += " \\skip1";
} else { } else {
fL[i] += ' ' + n.fLyrics[i].fText; fL[i] += ' ' + EscapeSyllable(n.fLyrics[i].fText);
if (n.fLyrics[i].fKind & VLSyllable::kHasNext) if (n.fLyrics[i].fKind & VLSyllable::kHasNext)
fL[i] += " --"; fL[i] += " --";
} }