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. * Converts XML/XHTML entities to plain text.
*/ */
@JvmStatic @JvmStatic
fun unescapeXml(str: String): String = Jsoup.parse(str).text() fun String.unescapeXml(): String = Jsoup.parse(this).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()
}
}
}
/** /**
* Reads contents of a URL. * Reads contents of a URL.

View file

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

View file

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

View file

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