|
| 1 | +package com.hackeros.app.data.parser |
| 2 | + |
| 3 | +import com.hackeros.app.data.model.ReleaseInfo |
| 4 | + |
| 5 | +object ReleaseParser { |
| 6 | + |
| 7 | + fun parse(text: String): List<ReleaseInfo> { |
| 8 | + // 1. Clean outer brackets |
| 9 | + val cleanText = text.trim().removePrefix("[").removeSuffix("]").trim() |
| 10 | + |
| 11 | + val lines = cleanText.lines().map { it.trim() }.filter { it.isNotEmpty() } |
| 12 | + |
| 13 | + val releases = mutableListOf<ReleaseInfo>() |
| 14 | + |
| 15 | + var currentVersion: String? = null |
| 16 | + var currentSection: String? = null |
| 17 | + val tempNews = mutableListOf<String>() |
| 18 | + val tempEditions = mutableListOf<String>() |
| 19 | + val tempDescription = mutableListOf<String>() |
| 20 | + |
| 21 | + fun saveCurrentRelease() { |
| 22 | + currentVersion?.let { ver -> |
| 23 | + releases.add( |
| 24 | + ReleaseInfo( |
| 25 | + version = ver, |
| 26 | + description = tempDescription.joinToString(" "), |
| 27 | + news = tempNews.joinToString("\n"), |
| 28 | + editions = tempEditions.joinToString("\n") |
| 29 | + ) |
| 30 | + ) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + for (line in lines) { |
| 35 | + when { |
| 36 | + line.matches(Regex("^V\\d.*")) -> { |
| 37 | + saveCurrentRelease() |
| 38 | + currentVersion = line |
| 39 | + currentSection = null |
| 40 | + tempNews.clear() |
| 41 | + tempEditions.clear() |
| 42 | + tempDescription.clear() |
| 43 | + } |
| 44 | + line.lowercase().startsWith("new:") -> currentSection = "new" |
| 45 | + line.lowercase().startsWith("release:") -> currentSection = "release" |
| 46 | + line.lowercase().startsWith("description:") -> currentSection = "description" |
| 47 | + line.startsWith("=") -> { |
| 48 | + val content = line.substring(1).trim() |
| 49 | + when (currentSection) { |
| 50 | + "new" -> tempNews.add(content) |
| 51 | + "release" -> tempEditions.add(content) |
| 52 | + "description" -> tempDescription.add(content) |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + saveCurrentRelease() |
| 58 | + return releases |
| 59 | + } |
| 60 | +} |
0 commit comments