mirror of https://github.com/jlelse/teleposter
Improved code
This commit is contained in:
parent
006e1bef2b
commit
ad7b1e235b
|
@ -35,31 +35,22 @@ class Api {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun JSONArray.parseContent(result: Page) {
|
private fun JSONArray.parseContent(result: Page) {
|
||||||
try {
|
for (i in 0 until length()) {
|
||||||
for (child in this) {
|
optJSONObject(i)?.let {
|
||||||
try {
|
result.content += "<${it.optString("tag", "")}"
|
||||||
if (child is String) result.content += child
|
it.optJSONObject("attrs")?.let {
|
||||||
else if (child is JSONObject) {
|
|
||||||
result.content += "<${child.optString("tag", "")}"
|
|
||||||
child.optJSONObject("attrs")?.let {
|
|
||||||
for (key in it.keys()) {
|
for (key in it.keys()) {
|
||||||
result.content += " $key=\"${it.optString(key, "")}\""
|
result.content += " $key=\"${it.optString(key, "")}\""
|
||||||
}
|
}
|
||||||
for (i in 0 until length()) {
|
|
||||||
result.content += "${it.names()}"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
result.content += ">"
|
result.content += ">"
|
||||||
child.optJSONArray("children").parseContent(result)
|
it.optJSONArray("children")?.parseContent(result)
|
||||||
result.content += "</${child.optString("tag", "")}>"
|
result.content += "</${it.optString("tag", "")}>"
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
if (optJSONObject(i) == null) optString(i)?.let {
|
||||||
e.printStackTrace()
|
result.content += it
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
|
||||||
e.printStackTrace()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,2 @@
|
||||||
package telegra.ph
|
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>"
|
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>"
|
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>"
|
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)
|
webView?.loadDataWithBaseURL(it.url, html, "text/html; charset=UTF-8", null, null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue