11package io.github.kbiakov.codeview.highlight
22
33import android.graphics.Color
4+ import android.util.Log
45import io.github.kbiakov.codeview.highlight.prettify.PrettifyParser
56import io.github.kbiakov.codeview.highlight.parser.ParseResult
67import java.util.*
@@ -34,9 +35,11 @@ object CodeHighlighter {
3435 val highlighted = StringBuilder ()
3536
3637 results.forEach { result ->
37- val color = getColor(colorsMap, result)
38+ val color = colorsMap. getColor(result)
3839 val content = parseContent(source, result)
39- highlighted.append(" <font color=\" $color \" >$content </font>" )
40+ // Log.e("!!!", content)
41+
42+ highlighted.append(content.withFontParams(color))
4043 }
4144
4245 return highlighted.toString()
@@ -64,8 +67,8 @@ object CodeHighlighter {
6467 * @param result Syntax unit
6568 * @return Color for syntax unit
6669 */
67- private fun getColor ( colorsMap : HashMap <String , String >, result : ParseResult ) =
68- colorsMap [result.styleKeys[0 ]] ? : colorsMap [" pln" ]
70+ private fun HashMap <String , String >. getColor ( result : ParseResult ) =
71+ this [result.styleKeys[0 ]] ? : this [" pln" ]
6972
7073 /* *
7174 * Build fast accessor (as map) for selected color theme.
@@ -200,9 +203,7 @@ data class SyntaxColors(
200203 val attrValue : Int = 0x269186 )
201204
202205/* *
203- * Convert hex int to color by adding alpha-channel.
204- *
205- * @return Color int
206+ * @return Converted hex int to color by adding alpha-channel
206207 */
207208fun Int.color () = try {
208209 Color .parseColor(" #FF${Integer .toHexString(this )} " )
@@ -211,8 +212,49 @@ fun Int.color() = try {
211212}
212213
213214/* *
214- * Convert hex int to hex string.
215- *
216- * @return Hex string
215+ * @return Converted hex int to hex string
217216 */
218217fun Int.hex () = " #${Integer .toHexString(this )} "
218+
219+ /* *
220+ * @return Is value equals to found or not condition
221+ */
222+ fun Int.isFound () = this >= 0
223+ fun Int.notFound () = this == - 1
224+
225+ /* *
226+ * @return String with applied font params
227+ */
228+ fun String.withFontParams (color : String? ): String {
229+ val parametrizedString = StringBuilder ()
230+
231+ var idx = 0
232+ var newIdx = indexOf(" \n " )
233+
234+ if (newIdx.notFound())
235+ parametrizedString.append(inFontTag(color))
236+ else while (newIdx.isFound()) {
237+ if (idx > 0 )
238+ parametrizedString.append(" \n " )
239+
240+ val part = substring(idx.. newIdx - 1 ).inFontTag(color)
241+ parametrizedString.append(part)
242+
243+ idx = newIdx
244+ newIdx = indexOf(" \n " , idx + 1 )
245+ }
246+
247+ Log .e(" !!!" , parametrizedString.toString())
248+
249+ return parametrizedString.toString()
250+ }
251+
252+ /* *
253+ * @return String surrounded by font tag
254+ */
255+ fun String.inFontTag (color : String? ) =
256+ " <font color=\" $color \" >${
257+ if (startsWith(" \n " ) && length > 2 )
258+ substring(2 )
259+ else this
260+ } </font>"
0 commit comments