Compare commits
2 commits
9914ded4e2
...
50fa9edb08
Author | SHA1 | Date | |
---|---|---|---|
dece | 50fa9edb08 | ||
dece | 5257824ab4 |
|
@ -18,6 +18,9 @@ android {
|
||||||
buildFeatures {
|
buildFeatures {
|
||||||
viewBinding true
|
viewBinding true
|
||||||
}
|
}
|
||||||
|
testOptions {
|
||||||
|
unitTests.returnDefaultValues = true
|
||||||
|
}
|
||||||
buildTypes {
|
buildTypes {
|
||||||
release {
|
release {
|
||||||
minifyEnabled false
|
minifyEnabled false
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
package dev.lowrespalmtree.comet
|
|
||||||
|
|
||||||
import androidx.test.platform.app.InstrumentationRegistry
|
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
|
||||||
|
|
||||||
import org.junit.Test
|
|
||||||
import org.junit.runner.RunWith
|
|
||||||
|
|
||||||
import org.junit.Assert.*
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Instrumented test, which will execute on an Android device.
|
|
||||||
*
|
|
||||||
* See [testing documentation](http://d.android.com/tools/testing).
|
|
||||||
*/
|
|
||||||
@RunWith(AndroidJUnit4::class)
|
|
||||||
class ExampleInstrumentedTest {
|
|
||||||
@Test
|
|
||||||
fun useAppContext() {
|
|
||||||
// Context of the app under test.
|
|
||||||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
|
|
||||||
assertEquals("dev.lowrespalmtree.comet", appContext.packageName)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,76 @@
|
||||||
|
package dev.lowrespalmtree.comet
|
||||||
|
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Example local unit test, which will execute on the development machine (host).
|
||||||
|
*
|
||||||
|
* See [testing documentation](http://d.android.com/tools/testing).
|
||||||
|
*/
|
||||||
|
class UriUtilsTest {
|
||||||
|
@Test
|
||||||
|
fun joinUrls() {
|
||||||
|
assertEquals(
|
||||||
|
"gemini://dece.space/some-file.gmi",
|
||||||
|
joinUrls("gemini://dece.space/", "some-file.gmi").toString()
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
"gemini://dece.space/some-file.gmi",
|
||||||
|
joinUrls("gemini://dece.space/", "./some-file.gmi").toString()
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
"gemini://dece.space/some-file.gmi",
|
||||||
|
joinUrls("gemini://dece.space/dir1", "/some-file.gmi").toString()
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
"gemini://dece.space/dir1/other-file.gmi",
|
||||||
|
joinUrls("gemini://dece.space/dir1/file.gmi", "other-file.gmi").toString()
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
"gemini://dece.space/top-level.gmi",
|
||||||
|
joinUrls("gemini://dece.space/dir1/file.gmi", "../top-level.gmi").toString()
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
"s://hard/test/b/d/a.gmi",
|
||||||
|
joinUrls("s://hard/dir/a", "./../test/b/c/../d/e/f/../.././a.gmi").toString()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun removeDotSegments() {
|
||||||
|
arrayOf(
|
||||||
|
Pair("index.gmi", "index.gmi"),
|
||||||
|
Pair("/index.gmi", "/index.gmi"),
|
||||||
|
Pair("./index.gmi", "index.gmi"),
|
||||||
|
Pair("/./index.gmi", "/index.gmi"),
|
||||||
|
Pair("/../index.gmi", "/index.gmi"),
|
||||||
|
Pair("/a/b/c/./../../g", "/a/g"),
|
||||||
|
Pair("mid/content=5/../6", "mid/6"),
|
||||||
|
Pair("../../../../g", "g")
|
||||||
|
).forEach { (path, expected) ->
|
||||||
|
assertEquals(expected, removeDotSegments(path))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun removeLastSegment() {
|
||||||
|
assertEquals("", removeLastSegment(""))
|
||||||
|
assertEquals("", removeLastSegment("/"))
|
||||||
|
assertEquals("", removeLastSegment("/a"))
|
||||||
|
assertEquals("/a", removeLastSegment("/a/"))
|
||||||
|
assertEquals("/a", removeLastSegment("/a/b"))
|
||||||
|
assertEquals("/a/b/c", removeLastSegment("/a/b/c/d"))
|
||||||
|
assertEquals("//", removeLastSegment("///"))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun popFirstSegment() {
|
||||||
|
assertEquals(Pair("", ""), popFirstSegment(""))
|
||||||
|
assertEquals(Pair("a", ""), popFirstSegment("a"))
|
||||||
|
assertEquals(Pair("/a", ""), popFirstSegment("/a"))
|
||||||
|
assertEquals(Pair("/a", "/"), popFirstSegment("/a/"))
|
||||||
|
assertEquals(Pair("/a", "/b"), popFirstSegment("/a/b"))
|
||||||
|
assertEquals(Pair("a", "/b"), popFirstSegment("a/b"))
|
||||||
|
}
|
||||||
|
}
|
109
app/src/main/java/dev/lowrespalmtree/comet/ContentRecycler.kt
Normal file
109
app/src/main/java/dev/lowrespalmtree/comet/ContentRecycler.kt
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
package dev.lowrespalmtree.comet
|
||||||
|
|
||||||
|
import android.text.SpannableString
|
||||||
|
import android.text.style.UnderlineSpan
|
||||||
|
import android.util.Log
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import dev.lowrespalmtree.comet.databinding.*
|
||||||
|
|
||||||
|
class ContentAdapter(private var content: List<Line>, private val listener: ContentAdapterListen) :
|
||||||
|
RecyclerView.Adapter<ContentAdapter.ContentViewHolder>() {
|
||||||
|
|
||||||
|
interface ContentAdapterListen {
|
||||||
|
fun onLinkClick(url: String)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemViewType(position: Int): Int =
|
||||||
|
when (content[position]) {
|
||||||
|
is EmptyLine -> TYPE_EMPTY
|
||||||
|
is ParagraphLine -> TYPE_PARAGRAPH
|
||||||
|
is LinkLine -> TYPE_LINK
|
||||||
|
is PreFenceLine -> TYPE_PRE_FENCE
|
||||||
|
is PreTextLine -> TYPE_PRE_TEXT
|
||||||
|
is BlockquoteLine -> TYPE_BLOCKQUOTE
|
||||||
|
is ListItemLine -> TYPE_LIST_ITEM
|
||||||
|
is TitleLine -> when ((content[position] as TitleLine).level) {
|
||||||
|
1 -> TYPE_TITLE_1
|
||||||
|
2 -> TYPE_TITLE_2
|
||||||
|
3 -> TYPE_TITLE_3
|
||||||
|
else -> error("invalid title level")
|
||||||
|
}
|
||||||
|
else -> error("unknown line type")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContentViewHolder {
|
||||||
|
Log.d(TAG, "onCreateViewHolder: type $viewType")
|
||||||
|
return LayoutInflater.from(parent.context).let {
|
||||||
|
when (viewType) {
|
||||||
|
TYPE_EMPTY -> ContentViewHolder.Empty(GemtextEmptyBinding.inflate(it))
|
||||||
|
TYPE_PARAGRAPH -> ContentViewHolder.Paragraph(GemtextParagraphBinding.inflate(it))
|
||||||
|
TYPE_TITLE_1 -> ContentViewHolder.Title1(GemtextTitle1Binding.inflate(it))
|
||||||
|
TYPE_TITLE_2 -> ContentViewHolder.Title2(GemtextTitle2Binding.inflate(it))
|
||||||
|
TYPE_TITLE_3 -> ContentViewHolder.Title3(GemtextTitle3Binding.inflate(it))
|
||||||
|
TYPE_LINK -> ContentViewHolder.Link(GemtextLinkBinding.inflate(it))
|
||||||
|
TYPE_PRE_FENCE -> ContentViewHolder.PreFence(GemtextEmptyBinding.inflate(it))
|
||||||
|
TYPE_PRE_TEXT -> ContentViewHolder.PreText(GemtextPreformattedBinding.inflate(it))
|
||||||
|
TYPE_BLOCKQUOTE -> ContentViewHolder.Blockquote(GemtextBlockquoteBinding.inflate(it))
|
||||||
|
TYPE_LIST_ITEM -> ContentViewHolder.ListItem(GemtextListItemBinding.inflate(it))
|
||||||
|
else -> error("invalid view type")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBindViewHolder(holder: ContentViewHolder, position: Int) {
|
||||||
|
Log.d(TAG, "onBindViewHolder: position $position")
|
||||||
|
val line = content[position]
|
||||||
|
when (holder) {
|
||||||
|
is ContentViewHolder.Paragraph -> holder.binding.textView.text =
|
||||||
|
(line as ParagraphLine).text
|
||||||
|
is ContentViewHolder.Title1 -> holder.binding.textView.text = (line as TitleLine).text
|
||||||
|
is ContentViewHolder.Title2 -> holder.binding.textView.text = (line as TitleLine).text
|
||||||
|
is ContentViewHolder.Title3 -> holder.binding.textView.text = (line as TitleLine).text
|
||||||
|
is ContentViewHolder.Link -> {
|
||||||
|
val text = if ((line as LinkLine).label.isNotEmpty()) line.label else line.url
|
||||||
|
val underlined = SpannableString(text)
|
||||||
|
underlined.setSpan(UnderlineSpan(), 0, underlined.length, 0)
|
||||||
|
holder.binding.textView.text = underlined
|
||||||
|
holder.binding.root.setOnClickListener { listener.onLinkClick(line.url) }
|
||||||
|
}
|
||||||
|
else -> {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemCount(): Int = content.size
|
||||||
|
|
||||||
|
fun setContent(content: List<Line>) {
|
||||||
|
this.content = content
|
||||||
|
notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class ContentViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
||||||
|
class Empty(val binding: GemtextEmptyBinding) : ContentViewHolder(binding.root)
|
||||||
|
class Paragraph(val binding: GemtextParagraphBinding) : ContentViewHolder(binding.root)
|
||||||
|
class Title1(val binding: GemtextTitle1Binding) : ContentViewHolder(binding.root)
|
||||||
|
class Title2(val binding: GemtextTitle2Binding) : ContentViewHolder(binding.root)
|
||||||
|
class Title3(val binding: GemtextTitle3Binding) : ContentViewHolder(binding.root)
|
||||||
|
class Link(val binding: GemtextLinkBinding) : ContentViewHolder(binding.root)
|
||||||
|
class PreFence(val binding: GemtextEmptyBinding) : ContentViewHolder(binding.root)
|
||||||
|
class PreText(val binding: GemtextPreformattedBinding) : ContentViewHolder(binding.root)
|
||||||
|
class Blockquote(val binding: GemtextBlockquoteBinding) : ContentViewHolder(binding.root)
|
||||||
|
class ListItem(val binding: GemtextListItemBinding) : ContentViewHolder(binding.root)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val TAG = "ContentRecycler"
|
||||||
|
const val TYPE_EMPTY = 0
|
||||||
|
const val TYPE_PARAGRAPH = 1
|
||||||
|
const val TYPE_TITLE_1 = 2
|
||||||
|
const val TYPE_TITLE_2 = 3
|
||||||
|
const val TYPE_TITLE_3 = 4
|
||||||
|
const val TYPE_LINK = 5
|
||||||
|
const val TYPE_PRE_FENCE = 6
|
||||||
|
const val TYPE_PRE_TEXT = 7
|
||||||
|
const val TYPE_BLOCKQUOTE = 8
|
||||||
|
const val TYPE_LIST_ITEM = 9
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,16 +7,16 @@ import java.nio.ByteBuffer
|
||||||
import java.nio.CharBuffer
|
import java.nio.CharBuffer
|
||||||
import java.nio.charset.Charset
|
import java.nio.charset.Charset
|
||||||
|
|
||||||
open class Line
|
interface Line
|
||||||
|
|
||||||
class EmptyLine : Line()
|
class EmptyLine : Line
|
||||||
class ParagraphLine(val text: String) : Line()
|
class ParagraphLine(val text: String) : Line
|
||||||
class TitleLine(val level: Int, val text: String) : Line()
|
class TitleLine(val level: Int, val text: String) : Line
|
||||||
class LinkLine(val url: String, val label: String) : Line()
|
class LinkLine(val url: String, val label: String) : Line
|
||||||
class PreFenceLine(val caption: String) : Line()
|
class PreFenceLine(val caption: String) : Line
|
||||||
class PreTextLine(val text: String) : Line()
|
class PreTextLine(val text: String) : Line
|
||||||
class BlockquoteLine(val text: String) : Line()
|
class BlockquoteLine(val text: String) : Line
|
||||||
class ListItemLine(val text: String) : Line()
|
class ListItemLine(val text: String) : Line
|
||||||
|
|
||||||
fun parseData(
|
fun parseData(
|
||||||
inChannel: Channel<ByteArray>,
|
inChannel: Channel<ByteArray>,
|
||||||
|
@ -60,5 +60,3 @@ private fun parseLine(line: CharBuffer, isPreformatted: Boolean): Line =
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getCharsFrom(line: CharBuffer, index: Int) = line.substring(index).removePrefix(" ")
|
private fun getCharsFrom(line: CharBuffer, index: Int) = line.substring(index).removePrefix(" ")
|
||||||
|
|
||||||
private const val TAG = "Gemtext"
|
|
|
@ -2,6 +2,8 @@ package dev.lowrespalmtree.comet
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.Intent.ACTION_VIEW
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
@ -13,15 +15,17 @@ import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import dev.lowrespalmtree.comet.databinding.ActivityMainBinding
|
import dev.lowrespalmtree.comet.databinding.ActivityMainBinding
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.nio.ByteBuffer
|
import java.net.UnknownHostException
|
||||||
import java.nio.charset.Charset
|
import java.nio.charset.Charset
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
|
||||||
private lateinit var binding: ActivityMainBinding
|
private lateinit var binding: ActivityMainBinding
|
||||||
private lateinit var pageViewModel: PageViewModel
|
private lateinit var pageViewModel: PageViewModel
|
||||||
|
private lateinit var adapter: ContentAdapter
|
||||||
|
|
||||||
@SuppressLint("SetTextI18n")
|
@SuppressLint("SetTextI18n")
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
@ -30,10 +34,13 @@ class MainActivity : AppCompatActivity() {
|
||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
|
|
||||||
pageViewModel = ViewModelProvider(this)[PageViewModel::class.java]
|
pageViewModel = ViewModelProvider(this)[PageViewModel::class.java]
|
||||||
|
adapter = ContentAdapter(listOf(), this)
|
||||||
|
binding.contentRecycler.layoutManager = LinearLayoutManager(this)
|
||||||
|
binding.contentRecycler.adapter = adapter
|
||||||
|
|
||||||
binding.addressBar.setOnEditorActionListener { view, actionId, _ ->
|
binding.addressBar.setOnEditorActionListener { view, actionId, _ ->
|
||||||
if (actionId == EditorInfo.IME_ACTION_DONE) {
|
if (actionId == EditorInfo.IME_ACTION_DONE) {
|
||||||
pageViewModel.sendRequest(view.text.toString())
|
openUrl(view.text.toString())
|
||||||
val imm = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
|
val imm = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
|
||||||
imm.hideSoftInputFromWindow(view.windowToken, 0)
|
imm.hideSoftInputFromWindow(view.windowToken, 0)
|
||||||
view.clearFocus()
|
view.clearFocus()
|
||||||
|
@ -43,37 +50,64 @@ class MainActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pageViewModel.sourceLiveData.observe(this, {
|
pageViewModel.linesLiveData.observe(this, { adapter.setContent(it) })
|
||||||
binding.sourceBlock.text = it
|
pageViewModel.alertLiveData.observe(this, { alert(it) })
|
||||||
})
|
}
|
||||||
pageViewModel.alertLiveData.observe(this, {
|
|
||||||
|
override fun onLinkClick(url: String) {
|
||||||
|
val base = binding.addressBar.text.toString()
|
||||||
|
openUrl(url, base = if (base.isNotEmpty()) base else null)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun openUrl(url: String, base: String? = null) {
|
||||||
|
var uri = Uri.parse(url)
|
||||||
|
if (!uri.isAbsolute) {
|
||||||
|
uri = if (!base.isNullOrEmpty()) joinUrls(base, url) else toGeminiUri(uri)
|
||||||
|
Log.d(TAG, "openUrl: '$url' - '$base' - '$uri'")
|
||||||
|
Log.d(TAG, "openUrl: ${uri.authority} - ${uri.path} - ${uri.query}")
|
||||||
|
binding.addressBar.setText(uri.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
when (uri.scheme) {
|
||||||
|
"gemini" -> pageViewModel.sendGeminiRequest(uri)
|
||||||
|
else -> startActivity(Intent(ACTION_VIEW, uri))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun alert(message: String) {
|
||||||
AlertDialog.Builder(this)
|
AlertDialog.Builder(this)
|
||||||
.setTitle(R.string.alert_title)
|
.setTitle(R.string.alert_title)
|
||||||
.setMessage(it)
|
.setMessage(message)
|
||||||
.create()
|
.create()
|
||||||
.show()
|
.show()
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class PageViewModel : ViewModel() {
|
class PageViewModel : ViewModel() {
|
||||||
var source = ""
|
private var lines = ArrayList<Line>()
|
||||||
val sourceLiveData: MutableLiveData<String> by lazy { MutableLiveData<String>() }
|
val linesLiveData: MutableLiveData<List<Line>> by lazy { MutableLiveData<List<Line>>() }
|
||||||
val alertLiveData: MutableLiveData<String> by lazy { MutableLiveData<String>() }
|
val alertLiveData: MutableLiveData<String> by lazy { MutableLiveData<String>() }
|
||||||
|
|
||||||
fun sendRequest(url: String) {
|
/**
|
||||||
Log.d(TAG, "sendRequest: $url")
|
* Perform a request against this URI.
|
||||||
source = ""
|
*
|
||||||
|
* The URI must be valid, absolute and with a gemini scheme.
|
||||||
|
*/
|
||||||
|
fun sendGeminiRequest(uri: Uri) {
|
||||||
|
Log.d(TAG, "sendRequest: $uri")
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
val uri = Uri.parse(url)
|
val response = try {
|
||||||
if (uri.scheme != "gemini") {
|
|
||||||
alertLiveData.postValue("Can't process scheme \"${uri.scheme}\".")
|
|
||||||
return@launch
|
|
||||||
}
|
|
||||||
|
|
||||||
val request = Request(uri)
|
val request = Request(uri)
|
||||||
val socket = request.connect()
|
val socket = request.connect()
|
||||||
val channel = request.proceed(socket, this)
|
val channel = request.proceed(socket, this)
|
||||||
val response = Response.from(channel, viewModelScope)
|
Response.from(channel, viewModelScope)
|
||||||
|
} catch (e: UnknownHostException) {
|
||||||
|
alertLiveData.postValue("Unknown host \"${uri.authority}\".")
|
||||||
|
return@launch
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "sendGeminiRequest coroutine: ${e.stackTraceToString()}")
|
||||||
|
alertLiveData.postValue("Oops! Whatever we tried to do failed!")
|
||||||
|
return@launch
|
||||||
|
}
|
||||||
if (response == null) {
|
if (response == null) {
|
||||||
alertLiveData.postValue("Can't parse server response.")
|
alertLiveData.postValue("Can't parse server response.")
|
||||||
return@launch
|
return@launch
|
||||||
|
@ -85,22 +119,15 @@ class MainActivity : AppCompatActivity() {
|
||||||
else -> alertLiveData.postValue("Can't handle code ${response.code}.")
|
else -> alertLiveData.postValue("Can't handle code ${response.code}.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun handleRequestSuccess(response: Response) {
|
private suspend fun handleRequestSuccess(response: Response) {
|
||||||
|
lines.clear()
|
||||||
val charset = Charset.defaultCharset()
|
val charset = Charset.defaultCharset()
|
||||||
for (line in parseData(response.data, charset, viewModelScope)) {
|
for (line in parseData(response.data, charset, viewModelScope)) {
|
||||||
when (line) {
|
lines.add(line)
|
||||||
is EmptyLine -> { source += "\n" }
|
linesLiveData.postValue(lines)
|
||||||
is ParagraphLine -> { source += line.text + "\n" }
|
|
||||||
is TitleLine -> { source += "TTL-${line.level} ${line.text}\n" }
|
|
||||||
is LinkLine -> { source += "LNK ${line.url} + ${line.label}\n" }
|
|
||||||
is PreFenceLine -> { source += "PRE ${line.caption}\n" }
|
|
||||||
is PreTextLine -> { source += line.text + "\n" }
|
|
||||||
is BlockquoteLine -> { source += "QUO ${line.text}\n" }
|
|
||||||
is ListItemLine -> { source += "LST ${line.text}\n" }
|
|
||||||
}
|
|
||||||
sourceLiveData.postValue(source)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
88
app/src/main/java/dev/lowrespalmtree/comet/UriUtils.kt
Normal file
88
app/src/main/java/dev/lowrespalmtree/comet/UriUtils.kt
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
package dev.lowrespalmtree.comet
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import android.util.Log
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform a relative URI to an absolute Gemini URI
|
||||||
|
*
|
||||||
|
* This is mostly to translate user-friendly URLs such as "medusae.space" into
|
||||||
|
* "gemini://medusae.space". This assumes that the Uri parsing put what the user probably intended
|
||||||
|
* as the hostname into the path field instead of the authority. Thus, it will NOT merely translate
|
||||||
|
* any absolute URI into a gemini-scheme URI.
|
||||||
|
*/
|
||||||
|
fun toGeminiUri(uri: Uri): Uri =
|
||||||
|
Uri.Builder()
|
||||||
|
.scheme("gemini")
|
||||||
|
.authority(uri.path)
|
||||||
|
.query(uri.query)
|
||||||
|
.fragment(uri.fragment)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
/** Return the URI obtained from considering the relative part of an URI wrt/ a base URI. */
|
||||||
|
fun joinUrls(base: String, relative: String): Uri {
|
||||||
|
val baseUri = Uri.parse(base)
|
||||||
|
val relUri = Uri.parse(relative)
|
||||||
|
val newPath = removeDotSegments(
|
||||||
|
if (relative.startsWith("/")) {
|
||||||
|
relUri.path ?: ""
|
||||||
|
} else {
|
||||||
|
removeLastSegment(
|
||||||
|
baseUri.path ?: ""
|
||||||
|
) + "/" +
|
||||||
|
(relUri.path ?: "")
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return Uri.Builder()
|
||||||
|
.scheme(baseUri.scheme)
|
||||||
|
.authority(baseUri.authority)
|
||||||
|
.path(newPath)
|
||||||
|
.query(relUri.query)
|
||||||
|
.fragment(relUri.fragment)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Remove all the sneaky dot segments from the path. */
|
||||||
|
internal fun removeDotSegments(path: String): String {
|
||||||
|
var output = ""
|
||||||
|
var slice = path
|
||||||
|
while (slice.isNotEmpty()) {
|
||||||
|
if (slice.startsWith("../")) {
|
||||||
|
slice = slice.substring(3)
|
||||||
|
} else if (slice.startsWith("./") || slice.startsWith("/./")) {
|
||||||
|
slice = slice.substring(2)
|
||||||
|
} else if (slice == "/.") {
|
||||||
|
slice = "/"
|
||||||
|
} else if (slice.startsWith("/../")) {
|
||||||
|
slice = "/" + slice.substring(4)
|
||||||
|
output = removeLastSegment(output)
|
||||||
|
} else if (slice == "/..") {
|
||||||
|
slice = "/"
|
||||||
|
output = removeLastSegment(output)
|
||||||
|
} else if (slice == "." || slice == "..") {
|
||||||
|
slice = ""
|
||||||
|
} else {
|
||||||
|
val (firstSegment, remaining) = popFirstSegment(slice)
|
||||||
|
output += firstSegment
|
||||||
|
slice = remaining
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Remove the last segment of that path, including preceding "/" if any. */
|
||||||
|
internal fun removeLastSegment(path: String): String {
|
||||||
|
val lastSlashIndex = path.indexOfLast { it == '/' }
|
||||||
|
return if (lastSlashIndex > -1) path.slice(0 until lastSlashIndex) else path
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Return first segment and the rest. */
|
||||||
|
internal fun popFirstSegment(path: String): Pair<String, String> {
|
||||||
|
if (path.isEmpty())
|
||||||
|
return Pair(path, "")
|
||||||
|
var nextSlash = path.substring(1).indexOf("/")
|
||||||
|
if (nextSlash == -1)
|
||||||
|
return Pair(path, "")
|
||||||
|
nextSlash++
|
||||||
|
return Pair(path.substring(0, nextSlash), path.substring(nextSlash))
|
||||||
|
}
|
|
@ -38,26 +38,14 @@
|
||||||
<androidx.core.widget.NestedScrollView
|
<androidx.core.widget.NestedScrollView
|
||||||
android:id="@+id/scroll_view"
|
android:id="@+id/scroll_view"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="wrap_content"
|
||||||
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
|
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
|
||||||
|
|
||||||
<LinearLayout
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
|
android:id="@+id/content_recycler"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:orientation="vertical">
|
android:orientation="vertical" />
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/source_block"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_marginStart="16dp"
|
|
||||||
android:layout_marginEnd="16dp"
|
|
||||||
android:text=""
|
|
||||||
android:textAlignment="textStart"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
|
||||||
app:layout_constraintStart_toStartOf="parent" />
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
</androidx.core.widget.NestedScrollView>
|
</androidx.core.widget.NestedScrollView>
|
||||||
|
|
||||||
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
6
app/src/main/res/layout/gemtext_blockquote.xml
Normal file
6
app/src/main/res/layout/gemtext_blockquote.xml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/text_view"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="@color/teal_200" />
|
6
app/src/main/res/layout/gemtext_empty.xml
Normal file
6
app/src/main/res/layout/gemtext_empty.xml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Space xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="8dp"
|
||||||
|
android:background="@color/black">
|
||||||
|
</Space>
|
12
app/src/main/res/layout/gemtext_link.xml
Normal file
12
app/src/main/res/layout/gemtext_link.xml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/text_view"
|
||||||
|
android:textSize="16sp"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingStart="8dp"
|
||||||
|
android:paddingEnd="8dp"
|
||||||
|
android:paddingTop="4dp"
|
||||||
|
android:paddingBottom="4dp"
|
||||||
|
android:background="@color/teal_200"
|
||||||
|
android:textIsSelectable="false" />
|
6
app/src/main/res/layout/gemtext_list_item.xml
Normal file
6
app/src/main/res/layout/gemtext_list_item.xml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/text_view"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="@color/teal_200" />
|
11
app/src/main/res/layout/gemtext_paragraph.xml
Normal file
11
app/src/main/res/layout/gemtext_paragraph.xml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/text_view"
|
||||||
|
android:textSize="16sp"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingStart="8dp"
|
||||||
|
android:paddingEnd="8dp"
|
||||||
|
android:paddingTop="4dp"
|
||||||
|
android:paddingBottom="4dp"
|
||||||
|
android:background="@color/teal_200" />
|
6
app/src/main/res/layout/gemtext_preformatted.xml
Normal file
6
app/src/main/res/layout/gemtext_preformatted.xml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/text_view"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="@color/teal_200" />
|
12
app/src/main/res/layout/gemtext_title1.xml
Normal file
12
app/src/main/res/layout/gemtext_title1.xml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/text_view"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:textSize="30sp"
|
||||||
|
android:textStyle="bold"
|
||||||
|
android:paddingStart="8dp"
|
||||||
|
android:paddingEnd="8dp"
|
||||||
|
android:paddingTop="16dp"
|
||||||
|
android:paddingBottom="4dp"
|
||||||
|
android:background="@color/teal_700" />
|
12
app/src/main/res/layout/gemtext_title2.xml
Normal file
12
app/src/main/res/layout/gemtext_title2.xml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/text_view"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:textSize="24sp"
|
||||||
|
android:textStyle="bold"
|
||||||
|
android:paddingStart="8dp"
|
||||||
|
android:paddingEnd="8dp"
|
||||||
|
android:paddingTop="8dp"
|
||||||
|
android:paddingBottom="4dp"
|
||||||
|
android:background="@color/teal_700" />
|
12
app/src/main/res/layout/gemtext_title3.xml
Normal file
12
app/src/main/res/layout/gemtext_title3.xml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/text_view"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:textSize="18sp"
|
||||||
|
android:textStyle="bold"
|
||||||
|
android:paddingStart="8dp"
|
||||||
|
android:paddingEnd="8dp"
|
||||||
|
android:paddingTop="8dp"
|
||||||
|
android:paddingBottom="4dp"
|
||||||
|
android:background="@color/teal_700" />
|
|
@ -1,17 +0,0 @@
|
||||||
package dev.lowrespalmtree.comet
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
Reference in a new issue