mirror of https://git.sr.ht/~oppen/ariane
code highlighting implemented
This commit is contained in:
parent
39c8ba0da6
commit
3cd926b3aa
|
@ -105,15 +105,13 @@ class GeminiDatasource: Datasource{
|
|||
|
||||
println("Tva: header: $header")
|
||||
|
||||
lines.forEach {
|
||||
println("Tva: line: $it")
|
||||
}
|
||||
|
||||
println("Tva: ends -----------------")
|
||||
|
||||
outputStreamWriter.close()
|
||||
bufferedWriter.close()
|
||||
outWriter.close()
|
||||
onUpdate(TvaState.GeminiResponse(uri, header, lines))
|
||||
|
||||
|
||||
|
||||
|
||||
onUpdate(TvaState.GeminiResponse(uri, header, GemtextHelper.findCodeBlocks(lines)))
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package oppen.tva.io
|
||||
|
||||
import java.lang.StringBuilder
|
||||
|
||||
object GemtextHelper {
|
||||
|
||||
|
||||
|
||||
fun findCodeBlocks(source: List<String>): List<String>{
|
||||
val sb = StringBuilder()
|
||||
var inCodeBlock = false
|
||||
val parsed = mutableListOf<String>()
|
||||
source.forEach { line ->
|
||||
if (line.contains("```")) {
|
||||
if (!inCodeBlock) {
|
||||
//New code block starting
|
||||
sb.clear()
|
||||
sb.append("```")
|
||||
} else {
|
||||
//End of code block
|
||||
parsed.add(sb.toString())
|
||||
}
|
||||
inCodeBlock = !inCodeBlock
|
||||
} else {
|
||||
if (inCodeBlock) {
|
||||
sb.append("$line\n")
|
||||
} else {
|
||||
parsed.add(line)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return parsed
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ import android.view.LayoutInflater
|
|||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import kotlinx.android.synthetic.main.gemtext_code_block.view.*
|
||||
import kotlinx.android.synthetic.main.gemtext_link.view.*
|
||||
import kotlinx.android.synthetic.main.gemtext_text.view.gemtext_text_textview
|
||||
import oppen.tva.R
|
||||
|
@ -19,6 +20,7 @@ class GemtextAdapter(val onLink: (link: URI, longTap: Boolean, view: View?) -> U
|
|||
val typeH3 = 3
|
||||
val typeListItem = 4
|
||||
val typeLink = 5
|
||||
val typeCodeBlock = 6
|
||||
|
||||
sealed class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
|
||||
class Text(itemView: View): ViewHolder(itemView)
|
||||
|
@ -44,6 +46,7 @@ class GemtextAdapter(val onLink: (link: URI, longTap: Boolean, view: View?) -> U
|
|||
typeH3 -> ViewHolder.H3(LayoutInflater.from(parent.context).inflate(R.layout.gemtext_h3, parent, false))
|
||||
typeListItem -> ViewHolder.ListItem(LayoutInflater.from(parent.context).inflate(R.layout.gemtext_text, parent, false))
|
||||
typeLink -> ViewHolder.Link(LayoutInflater.from(parent.context).inflate(R.layout.gemtext_link, parent, false))
|
||||
typeCodeBlock-> ViewHolder.Code(LayoutInflater.from(parent.context).inflate(R.layout.gemtext_code_block, parent, false))
|
||||
else -> ViewHolder.Text(LayoutInflater.from(parent.context).inflate(R.layout.gemtext_text, parent, false))
|
||||
}
|
||||
}
|
||||
|
@ -51,6 +54,7 @@ class GemtextAdapter(val onLink: (link: URI, longTap: Boolean, view: View?) -> U
|
|||
override fun getItemViewType(position: Int): Int {
|
||||
val line = lines[position]
|
||||
return when {
|
||||
line.startsWith("```") -> typeCodeBlock
|
||||
line.startsWith("###") -> typeH3
|
||||
line.startsWith("##") -> typeH2
|
||||
line.startsWith("#") -> typeH1
|
||||
|
@ -67,6 +71,7 @@ class GemtextAdapter(val onLink: (link: URI, longTap: Boolean, view: View?) -> U
|
|||
|
||||
when(holder){
|
||||
is ViewHolder.Text -> holder.itemView.gemtext_text_textview.text = line
|
||||
is ViewHolder.Code-> holder.itemView.gemtext_text_monospace_textview.text = line.substring(3).trim()
|
||||
is ViewHolder.H1 -> holder.itemView.gemtext_text_textview.text = line.substring(2).trim()
|
||||
is ViewHolder.H2 -> holder.itemView.gemtext_text_textview.text = line.substring(3).trim()
|
||||
is ViewHolder.H3 -> holder.itemView.gemtext_text_textview.text = line.substring(4).trim()
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/gemtext_text_monospace_textview"
|
||||
android:textSize="@dimen/code_text_size"
|
||||
android:layout_marginLeft="@dimen/default_margin"
|
||||
android:layout_marginRight="@dimen/default_margin"
|
||||
android:paddingLeft="@dimen/default_margin"
|
||||
android:paddingRight="@dimen/default_margin"
|
||||
android:paddingTop="@dimen/default_margin"
|
||||
android:paddingBottom="@dimen/default_margin"
|
||||
android:fontFamily="monospace"
|
||||
android:background="#000000"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
|
@ -3,6 +3,7 @@
|
|||
<dimen name="default_margin">8dp</dimen>
|
||||
<dimen name="button_margin">12dp</dimen>
|
||||
<dimen name="default_text_size">16sp</dimen>
|
||||
<dimen name="code_text_size">14sp</dimen>
|
||||
<dimen name="h1_text_size">28sp</dimen>
|
||||
<dimen name="h2_text_size">22sp</dimen>
|
||||
<dimen name="h3_text_size">18sp</dimen>
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
package oppen.tva
|
||||
|
||||
import org.junit.Test
|
||||
|
||||
import org.junit.Assert.*
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* See [testing documentation](http://d.android.com/tools/testing).
|
||||
*/
|
||||
class ExampleUnitTest {
|
||||
@Test
|
||||
fun addition_isCorrect() {
|
||||
assertEquals(4, 2 + 2)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package oppen.tva
|
||||
|
||||
import oppen.tva.io.GemtextHelper
|
||||
import org.junit.Test
|
||||
|
||||
import org.junit.Assert.*
|
||||
|
||||
class GemtextHelperUnitTest {
|
||||
|
||||
@Test
|
||||
fun `GemtextHelper correctly finds code blocks`() {
|
||||
val source = arrayListOf(
|
||||
"Header\n",
|
||||
"Some source code:\n",
|
||||
"```\n",
|
||||
"Code line A\n",
|
||||
"Code line B\n",
|
||||
"Code line C\n",
|
||||
"```\n",
|
||||
"Footer\n"
|
||||
)
|
||||
|
||||
val parsed = GemtextHelper.findCodeBlocks(source)
|
||||
assertEquals(4, parsed.size)
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GemtextHelper correctly finds TWO code blocks`() {
|
||||
val source = arrayListOf(
|
||||
"Header\n",
|
||||
"Some source code:\n",
|
||||
"```\n",
|
||||
"Code line A\n",
|
||||
"Code line B\n",
|
||||
"Code line C\n",
|
||||
"```\n",
|
||||
"Interval\n",
|
||||
"Some more source:\n",
|
||||
"```",
|
||||
"Code line D\n",
|
||||
"Code line E\n",
|
||||
"Code line F\n",
|
||||
"```",
|
||||
"FOOTER"
|
||||
)
|
||||
|
||||
val parsed = GemtextHelper.findCodeBlocks(source)
|
||||
|
||||
println("===============================")
|
||||
println("")
|
||||
parsed.forEach{line ->
|
||||
println("> $line")
|
||||
}
|
||||
println("")
|
||||
println("-------------------------------")
|
||||
assertEquals(7, parsed.size)
|
||||
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue