11package io.github.kbiakov.codeview.highlight
22
33import android.graphics.Color
4- import android.util.Log
54import io.github.kbiakov.codeview.highlight.prettify.PrettifyParser
65import io.github.kbiakov.codeview.highlight.parser.ParseResult
76import 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 */
255261fun 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