mirror of https://github.com/jlelse/teleposter
Improved code
This commit is contained in:
parent
006e1bef2b
commit
ad7b1e235b
|
@ -35,30 +35,21 @@ class Api {
|
|||
}
|
||||
|
||||
private fun JSONArray.parseContent(result: Page) {
|
||||
try {
|
||||
for (child in this) {
|
||||
try {
|
||||
if (child is String) result.content += child
|
||||
else if (child is JSONObject) {
|
||||
result.content += "<${child.optString("tag", "")}"
|
||||
child.optJSONObject("attrs")?.let {
|
||||
for (key in it.keys()) {
|
||||
result.content += " $key=\"${it.optString(key, "")}\""
|
||||
}
|
||||
for (i in 0 until length()) {
|
||||
result.content += "${it.names()}"
|
||||
}
|
||||
}
|
||||
result.content += ">"
|
||||
child.optJSONArray("children").parseContent(result)
|
||||
result.content += "</${child.optString("tag", "")}>"
|
||||
for (i in 0 until length()) {
|
||||
optJSONObject(i)?.let {
|
||||
result.content += "<${it.optString("tag", "")}"
|
||||
it.optJSONObject("attrs")?.let {
|
||||
for (key in it.keys()) {
|
||||
result.content += " $key=\"${it.optString(key, "")}\""
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
result.content += ">"
|
||||
it.optJSONArray("children")?.parseContent(result)
|
||||
result.content += "</${it.optString("tag", "")}>"
|
||||
}
|
||||
if (optJSONObject(i) == null) optString(i)?.let {
|
||||
result.content += it
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,2 @@
|
|||
package telegra.ph
|
||||
|
||||
import org.json.JSONArray
|
||||
|
||||
operator fun JSONArray.iterator(): Iterator<Any> = (0..length() - 1).map { opt(it) }.iterator()
|
|
@ -33,7 +33,8 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
|||
var html = "<h1>${it.title}</h1>"
|
||||
if (!it.author_name.isNullOrEmpty() && !it.author_url.isNullOrBlank()) html += "<a href=\"${it.author_url}\">${it.author_name}</a><br>"
|
||||
else if (!it.author_name.isNullOrEmpty()) html += "${it.author_name}<br>"
|
||||
html += it.content
|
||||
if (it.views != 0) html += "${it.views} times viewed<br><br>"
|
||||
if (it.content.isNullOrBlank()) html += it.description.replace("\n", "<br>") else html += it.content
|
||||
webView?.loadDataWithBaseURL(it.url, html, "text/html; charset=UTF-8", null, null)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue