Skip to content

Commit 64b3269

Browse files
committed
Done support multiline tag
1 parent 868591f commit 64b3269

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

codeview/src/main/java/io/github/kbiakov/codeview/highlight/CodeHighlighter.kt

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.github.kbiakov.codeview.highlight
22

33
import android.graphics.Color
4-
import android.util.Log
54
import io.github.kbiakov.codeview.highlight.prettify.PrettifyParser
65
import io.github.kbiakov.codeview.highlight.parser.ParseResult
76
import java.util.*
@@ -37,8 +36,6 @@ object CodeHighlighter {
3736
results.forEach { result ->
3837
val color = colorsMap.getColor(result)
3938
val content = parseContent(source, result)
40-
//Log.e("!!!", content)
41-
4239
highlighted.append(content.withFontParams(color))
4340
}
4441

@@ -63,7 +60,6 @@ object CodeHighlighter {
6360
/**
6461
* Color accessor from built color map for selected color theme.
6562
*
66-
* @param colorsMap Colors map built from color theme
6763
* @param result Syntax unit
6864
* @return Color for syntax unit
6965
*/
@@ -231,30 +227,36 @@ fun String.withFontParams(color: String?): String {
231227
var idx = 0
232228
var newIdx = indexOf("\n")
233229

234-
if (newIdx.notFound())
230+
if (newIdx.notFound()) // covers expected tag coverage (within only one line)
235231
parametrizedString.append(inFontTag(color))
236-
else while (newIdx.isFound()) {
237-
if (idx > 0)
238-
parametrizedString.append("\n")
232+
else { // may contain multiple lines with line breaks
239233

240-
val part = substring(idx..newIdx - 1).inFontTag(color)
241-
parametrizedString.append(part)
234+
// put tag on the borders (end & start of line, ..., end of tag)
235+
while (newIdx.isFound()) { // until closing tag is reached
236+
val part = substring(idx..newIdx).inFontTag(color)
237+
parametrizedString.append(part)
242238

243-
idx = newIdx
244-
newIdx = indexOf("\n", idx + 1)
245-
}
239+
idx = newIdx
240+
newIdx = indexOf("\n", idx + 1)
241+
}
246242

247-
Log.e("!!!", parametrizedString.toString())
243+
if (idx != indexOf("\n")) // if not replaced only once (for multiline tag coverage)
244+
parametrizedString.append(substring(idx).inFontTag(color))
245+
}
248246

249247
return parametrizedString.toString()
250248
}
251249

250+
/**
251+
* @return String with escaped line break at start
252+
*/
253+
fun String.escLineBreakAtStart() =
254+
if (startsWith("\n") && length >= 2)
255+
substring(1)
256+
else this
257+
252258
/**
253259
* @return String surrounded by font tag
254260
*/
255261
fun String.inFontTag(color: String?) =
256-
"<font color=\"$color\">${
257-
if (startsWith("\n") && length > 2)
258-
substring(2)
259-
else this
260-
}</font>"
262+
"<font color=\"$color\">${escLineBreakAtStart()}</font>"

0 commit comments

Comments
 (0)