Skip to content

Commit

Permalink
#296 refactor string concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
zuevval committed Aug 10, 2021
1 parent 3e7f11c commit 3ead0b9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ interface XmlAble {
val xmlParams: Map<String, String>
val xmlBody: HtmlText
fun toXml(): HtmlText {
var paramsString = ""
val paramsStringBuilder = StringBuilder()
for ((key, value) in xmlParams) {
val formattedValue = value.replace("\"", "'")
paramsString += " $key=\"$formattedValue\""
val formattedValue = value.replace("\"", "'").replace("<br>", "")
paramsStringBuilder.append(" $key=\"$formattedValue\"")
}

val formattedBody = xmlBody.replace("\n +".toRegex(), "\n")
return "<$xmlTag$paramsString>\n$formattedBody\n</$xmlTag>"
return "<$xmlTag$paramsStringBuilder>\n$formattedBody\n</$xmlTag>"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import org.junit.Test

import com.github.braillesystems.learnbraille.data.entities.BrailleDot.F
import com.github.braillesystems.learnbraille.data.entities.BrailleDot.E
import com.github.braillesystems.learnbraille.res.MarkerType
import com.github.braillesystems.learnbraille.res.golubinaIntroLessons
import com.github.braillesystems.learnbraille.res.SymbolType
import java.io.File
import java.lang.StringBuilder

internal val testLessons by lessons {
lesson(name = "first lesson") {
Expand Down Expand Up @@ -99,11 +99,12 @@ internal fun toXml(lesson: LessonWithSteps): HtmlText {

override val xmlBody: HtmlText
get() = {
var stepBuilder: HtmlText = ""
val stepBuilder = StringBuilder()
for (step in lesson.second.dropLast(1)) {
stepBuilder += (toXml(step.first.data) + "\n") // TODO [... <br>] -> [<p>...</p>]
stepBuilder.append(toXml(step.first.data) + "\n")
}
stepBuilder + toXml(lesson.second.takeLast(1)[0].first.data)
stepBuilder.append(toXml(lesson.second.takeLast(1)[0].first.data))
stepBuilder.toString()
}()
}.toXml()
}
Expand All @@ -126,19 +127,27 @@ class ToXmlTest {

@Test
fun lessonToXml() {
println(toXml(testLessons.lessons[0]))
assertEquals(
"""<lesson name="first lesson"><text type="info">first title</text></lesson>""",
toXml(testLessons.lessons[0]).replace("\n", "")
)
}

@Test
fun dumpGolubinaLessons() {
var xmlText = ""
for (lessons in golubinaIntroLessons.lessons) {
xmlText += (toXml(lessons) + "\n")
}

val xmlLinesList = xmlText.split("\n")
File("course.xml").printWriter().use { out ->
val outputPath = "course.xml"
File(outputPath).printWriter().use { out ->
out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
out.println("<root>")
xmlLinesList.forEach { out.println(it) }
out.println("</root>")
}
print("course dumped to file: `$outputPath`")
}
}

0 comments on commit 3ead0b9

Please sign in to comment.