diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/Solfa.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/Solfa.kt index 5face48..48ec8b7 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/Solfa.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/Solfa.kt @@ -11,6 +11,7 @@ class Solfa(val sharedScreenModel: SharedScreenModel, private val fileRepository private val T: MutableList = mutableListOf() private val N: MutableList = mutableListOf() private val L: MutableList = mutableListOf() + private val O: MutableMap> = mutableMapOf() private val unparsedNote = mutableListOf() private var refrainBeginsAt = -1 companion object { @@ -31,6 +32,7 @@ class Solfa(val sharedScreenModel: SharedScreenModel, private val fileRepository val REGEX_MALAGASY_MN = Regex("([aeio])_([nm])([tdjkbp])") val REGEX_MALAGASY_MN_STAGE2 = Regex("_([mn])-") val REGEX_PAREN_RECURSIVE = Regex("(\\([^\\(\\)]*)\\(([^\\)]*)\\)") + val REGEX_COMMENT = Regex("\\$\\{[^\\}]*\\}") } var nextTIndex: Int = -1 var nextNIndex: Int = -1 @@ -269,14 +271,15 @@ class Solfa(val sharedScreenModel: SharedScreenModel, private val fileRepository value = line.trim().substring(3) } - if (key != "") { - if ((key == "T") && (index == 0)) { + when (key) { + "T" -> if (index == 0) { loadT(value) - } else if (key == "N") { + } + "N" -> // Les lignes de type N seront parsées à la fin. preloadN() se chargera d'abord de réarranger la ligne // pour bien gérer les parenthèses. unparsedNote.add(index.toString()+value) - } else if (key == "M") { + "M" -> { val metaChunks: List = value.split(REGEX_PARSE_META) metaChunks.forEach { parseMeta(it) } parseMeta(value) @@ -285,17 +288,18 @@ class Solfa(val sharedScreenModel: SharedScreenModel, private val fileRepository sharedScreenModel.setSongAuthor(meta["a"] ?: "") sharedScreenModel.setSongComposer(meta["h"] ?: "") sharedScreenModel.setSongRhythm(meta["r"] ?: "") - } else if (key == "L") { - loadL(index, value) - } else if (key == "Y") { - loadY(index, value) - } else if (key == "E") { - loadE(index, value) - } else if (key == "U") { - loadU(value) - } else { - //setData(key, index, value) } + "L" -> + loadL(index, value) + "Y" -> + loadY(index, value) + "E" -> + loadE(index, value) + "U" -> + loadU(value) + "O" -> + loadO(index, value) + } } @@ -374,6 +378,9 @@ class Solfa(val sharedScreenModel: SharedScreenModel, private val fileRepository val parsedULine = uObject.parsed() loadT(parsedULine) } + fun loadO(index:Int, line:String) { + O.getOrPut(index) { mutableListOf() }.add(line) + } fun parseMeta(line: String) { /* $_a_keyAbbrev = array( 'a' => 'author', @@ -430,16 +437,21 @@ class Solfa(val sharedScreenModel: SharedScreenModel, private val fileRepository private fun loadL(stanzaNumber: Int, lyrics: String) { try { getLyricsComments(lyrics) - val loadedLyrics = lyrics.replace(REGEX_LYRICS_COMMENT, "") + val comments = REGEX_COMMENT.findAll(lyrics) + val commentsIterator = comments.iterator() + val loadedLyrics = lyrics//.replace(REGEX_LYRICS_COMMENT, "") .replace(REGEX_LYRICS_REPETITION) { matchResult -> val repeating = matchResult.destructured.match.groupValues[1] "_".repeat(repeating.toString().toInt()) } - //.replace("/", "_") .addHyphens() - val arrayLyrics = loadedLyrics.split(Regex("[/_]")) + val lyricsFinal = REGEX_COMMENT.replace(loadedLyrics) { matchResult -> + commentsIterator.next().value + } + val arrayLyrics = lyricsFinal.split(Regex("[/_]")) arrayLyrics.forEachIndexed { i, lyricsItem -> - addLyricsItem(stanzaNumber, i, lyricsItem) + val originalLyrics = REGEX_COMMENT.replace(lyricsItem, "") + addLyricsItem(stanzaNumber, i, originalLyrics) } if (refrainBeginsAt > 0 && stanzaNumber > 1 && !lyrics.contains($$"${R!}")) { copyRefrainToStanza(stanzaNumber) @@ -449,6 +461,7 @@ class Solfa(val sharedScreenModel: SharedScreenModel, private val fileRepository val lyricsIterator = lyricsComment.iterator() while (lyricsIterator.hasNext()) { val item = lyricsIterator.next() + // @todo: if item == ${O:...} // ${D:xxx} lyrics of DC and DSs if (item.substring(0, 4) == $$"${D:") { item.replace(REGEX_LYRICS_COMMENT, "$2") @@ -469,16 +482,14 @@ class Solfa(val sharedScreenModel: SharedScreenModel, private val fileRepository } lyricsComment.clear() } - // loadY is a smart lyrics parser for Malagasy language - private fun loadY(intKey: Int, lyrics: String) { + fun smartYLyrics(lyrics: String): String { + // Les ${O:1} risquent de changer en ${O:_1}. Sauvegardons-les dans comments. + val comments = REGEX_COMMENT.findAll(lyrics) + val commentsIterator = comments.iterator() val loadedLyrics = lyrics - .also { println(it)} .replace(REGEX_VOWELS_STAGE1, "$0_") - .also { println(it)} .replace(REGEX_VOWELS_STAGE2, "$1_") - .also { println(it)} .replace(REGEX_VOWELS_STAGE3, "$1_") - .also { println(it)} .replace(" ", " _") .replace("_\\ _", " ") .replace("_\\", "") @@ -487,10 +498,18 @@ class Solfa(val sharedScreenModel: SharedScreenModel, private val fileRepository .replace(REGEX_MALAGASY_MN, "$1$2_$3") .replace(REGEX_MALAGASY_MN_STAGE2, "$1-_") .replace("_n'", "n'_") - loadL(intKey, loadedLyrics) + // Et remettons les valeurs originales de ${...} + val lyricsFinal = REGEX_COMMENT.replace(loadedLyrics) { matchResult -> + commentsIterator.next().value + } + return lyricsFinal + } + // loadY is a smart lyrics parser for Malagasy language + private fun loadY(intKey: Int, lyrics: String) { + val smartLyrics = smartYLyrics(lyrics) + loadL(intKey, smartLyrics) } - // loadE is a smart Lyrics parser for English language - private fun loadE(intKey: Int, lyrics: String) { + fun smartELyrics(lyrics: String): String { val loadedLyrics = lyrics .replace(" ", " _") .replace("\\ _", " ") @@ -498,7 +517,12 @@ class Solfa(val sharedScreenModel: SharedScreenModel, private val fileRepository .replace("_/", "/") .replace("_0", "") getLyricsComments(loadedLyrics) - loadL(intKey, loadedLyrics.replace(REGEX_LYRICS_COMMENT, "")) + return loadedLyrics + } + // loadE is a smart Lyrics parser for English language + private fun loadE(intKey: Int, lyrics: String) { + val smartLyrics = smartELyrics(lyrics) + loadL(intKey, smartLyrics) } private fun getLyricsComments(lyrics: String) { val matchResult = REGEX_LYRICS_COMMENT.findAll(lyrics) @@ -535,7 +559,4 @@ class Solfa(val sharedScreenModel: SharedScreenModel, private val fileRepository L[i].appendDSLyrics(stanzaNumber, lyricsItem) } } -} -fun getOpt(x: String) : String { - return "" } \ No newline at end of file