Skip to content

Commit 868591f

Browse files
committed
Improved multiple lines coverage with font params
1 parent 57b1189 commit 868591f

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

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

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

33
import android.graphics.Color
4+
import android.util.Log
45
import io.github.kbiakov.codeview.highlight.prettify.PrettifyParser
56
import io.github.kbiakov.codeview.highlight.parser.ParseResult
67
import 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
*/
207208
fun 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
*/
218217
fun 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>"

example/src/main/java/io/github/kbiakov/codeviewexample/ListingsActivity.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,18 @@ public void onCodeLineClicked(int n, @NotNull String line) {
5151

5252
/**
5353
* 3: custom adapter with footer views
54-
*
54+
*/
5555

56-
final CustomAdapter adapter = new CustomAdapter(this, getString(R.string.listing_py));
56+
final CustomAdapter adapter = new CustomAdapter(this, getString(R.string.listing_md));
5757

5858
codeView.setAdapter(adapter);
5959
codeView.setColorTheme(ColorTheme.MONOKAI);
60-
codeView.highlightCode("python");
60+
codeView.highlightCode("md");
6161
codeView.setCodeListener(new OnCodeLineClickListener() {
6262
@Override
6363
public void onCodeLineClicked(int n, @NotNull String line) {
6464
adapter.addFooterEntity(n, new CustomAdapter.CustomModel("Line " + (n + 1), line));
6565
}
6666
});
67-
*/
6867
}
6968
}

0 commit comments

Comments
 (0)