Added unescapeXml extension function.

This commit is contained in:
Erik C. Thauvin 2022-02-14 22:25:26 -08:00
parent fe6ddf267d
commit 25d7a74568
4 changed files with 13 additions and 50 deletions

View file

@ -340,44 +340,7 @@ object Utils {
* Converts XML/XHTML entities to plain text.
*/
@JvmStatic
fun unescapeXml(str: String): String = Jsoup.parse(str).text()
/**
* Converts milliseconds to year month week day hour and minutes.
*/
@JvmStatic
fun uptime(uptime: Long): String {
uptime.toDuration(DurationUnit.MILLISECONDS).toComponents { wholeDays, hours, minutes, _, _ ->
val years = wholeDays / 365
var days = wholeDays % 365
val months = days / 30
days %= 30
val weeks = days / 7
days %= 7
with(StringBuffer()) {
if (years > 0) {
append(years).append(" year".plural(years)).append(' ')
}
if (months > 0) {
append(weeks).append(" month".plural(months)).append(' ')
}
if (weeks > 0) {
append(weeks).append(" week".plural(weeks)).append(' ')
}
if (days > 0) {
append(days).append(" day".plural(days)).append(' ')
}
if (hours > 0) {
append(hours).append(" hour".plural(hours.toLong())).append(' ')
}
append(minutes).append(" minute".plural(minutes.toLong()))
return toString()
}
}
}
fun String.unescapeXml(): String = Jsoup.parse(this).text()
/**
* Reads contents of a URL.

View file

@ -115,7 +115,7 @@ class GoogleSearch : ThreadedModule() {
val ja = json.getJSONArray("items")
for (i in 0 until ja.length()) {
val j = ja.getJSONObject(i)
results.add(NoticeMessage(unescapeXml(j.getString("title"))))
results.add(NoticeMessage(j.getString("title").unescapeXml()))
results.add(NoticeMessage(helpFormat(j.getString("link"), false), Colors.DARK_GREEN))
}
} else {

View file

@ -102,7 +102,7 @@ class StockQuote : ThreadedModule() {
try {
val info = json.getString("Information")
if (info.isNotEmpty()) {
throw ModuleException(debugMessage, unescapeXml(info))
throw ModuleException(debugMessage, info.unescapeXml())
}
} catch (ignore: JSONException) {
// Do nothing
@ -110,11 +110,11 @@ class StockQuote : ThreadedModule() {
try {
var error = json.getString("Note")
if (error.isNotEmpty()) {
throw ModuleException(debugMessage, unescapeXml(error))
throw ModuleException(debugMessage, error.unescapeXml())
}
error = json.getString("Error Message")
if (error.isNotEmpty()) {
throw ModuleException(debugMessage, unescapeXml(error))
throw ModuleException(debugMessage, error.unescapeXml())
}
} catch (ignore: JSONException) {
// Do nothing
@ -173,8 +173,8 @@ class StockQuote : ThreadedModule() {
add(
PublicMessage(
"Symbol: " + unescapeXml(quote.getString("01. symbol"))
+ " [" + unescapeXml(symbolInfo.getString("2. name")) + ']'
"Symbol: " + quote.getString("01. symbol").unescapeXml()
+ " [" + symbolInfo.getString("2. name").unescapeXml() + ']'
)
)
@ -183,13 +183,13 @@ class StockQuote : ThreadedModule() {
add(
PublicMessage(
"Price:".padEnd(pad).prependIndent()
+ unescapeXml(quote.getString("05. price"))
+ quote.getString("05. price").unescapeXml()
)
)
add(
PublicMessage(
"Previous:".padEnd(pad).prependIndent()
+ unescapeXml(quote.getString("08. previous close"))
+ quote.getString("08. previous close").unescapeXml()
)
)
@ -205,7 +205,7 @@ class StockQuote : ThreadedModule() {
add(
NoticeMessage(
"${it.first}:".padEnd(pad).prependIndent()
+ unescapeXml(quote.getString(it.second))
+ quote.getString(it.second).unescapeXml()
)
)
}
@ -213,8 +213,8 @@ class StockQuote : ThreadedModule() {
add(
NoticeMessage(
"Change:".padEnd(pad).prependIndent()
+ unescapeXml(quote.getString("09. change"))
+ " [" + unescapeXml(quote.getString("10. change percent")) + ']'
+ quote.getString("09. change").unescapeXml()
+ " [" + quote.getString("10. change percent").unescapeXml() + ']'
)
)
}

View file

@ -254,7 +254,7 @@ class UtilsTest {
@Test
fun testUnescapeXml() {
assertThat(unescapeXml("<a name="test & ''">")).isEqualTo(
assertThat("<a name="test & ''">".unescapeXml()).isEqualTo(
"<a name=\"test & ''\">"
)
}