-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure lazy loaded images are grabbed in reader mode (#366)
* Extract parsed item local handling * Ensure lazy loaded images are grabbed in reader mode - Remove extra call to Jsoup parse
- Loading branch information
Showing
8 changed files
with
174 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,58 @@ | ||
package com.jocmp.capy.accounts | ||
|
||
import java.time.OffsetDateTime | ||
|
||
internal data class ParsedItem( | ||
val id: String, | ||
val title: String? = null, | ||
val contentHTML: String? = null, | ||
val url: String? = null, | ||
val summary: String? = null, | ||
val imageURL: String? = null, | ||
val publishedAt: OffsetDateTime? | ||
) | ||
import com.prof18.rssparser.model.RssItem | ||
import org.jsoup.Jsoup | ||
import java.net.URI | ||
import java.net.URL | ||
|
||
internal class ParsedItem(private val item: RssItem, private val siteURL: String?) { | ||
val contentHTML: String? | ||
get() { | ||
val currentContent = item.content.orEmpty().ifBlank { | ||
item.description.orEmpty() | ||
} | ||
|
||
if (currentContent.isBlank()) { | ||
return null | ||
} | ||
|
||
return currentContent | ||
} | ||
|
||
val summary: String? | ||
get() = item.description?.let { | ||
if (it.isBlank()) { | ||
null | ||
} else { | ||
Jsoup.parse(it).text() | ||
} | ||
} | ||
|
||
val title: String | ||
get() = Jsoup.parse(item.title.orEmpty()).text() | ||
|
||
val url: String? = cleanedURL(item.link)?.toString() | ||
|
||
val imageURL: String? | ||
get() = cleanedURL(item.image)?.toString() | ||
|
||
private fun cleanedURL(inputURL: String?): URL? { | ||
val url = inputURL.orEmpty() | ||
|
||
if (url.isBlank()) { | ||
return null | ||
} | ||
|
||
return try { | ||
val uri = URI(url) | ||
|
||
if (uri.isAbsolute) { | ||
uri.toURL() | ||
} else { | ||
URI(siteURL).resolve(uri).toURL() | ||
} | ||
} catch (e: Throwable) { | ||
null | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.jocmp.capy.articles | ||
|
||
import org.jsoup.nodes.Element | ||
|
||
internal fun cleanLinks(element: Element) { | ||
element.getElementsByTag("img").forEachIndexed { index, child -> | ||
child.attr("src", child.absUrl("src")) | ||
val hasSizing = | ||
child.attr("width").isNotBlank() && child.attr("height").isNotBlank() | ||
|
||
if (index > 0 || hasSizing) { | ||
child.attr("loading", "lazy") | ||
} | ||
} | ||
|
||
element.select("img[data-src]").forEach { child -> | ||
child.attr("src", child.absUrl("data-src")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
capy/src/test/java/com/jocmp/capy/accounts/ParsedItemTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.jocmp.capy.accounts | ||
|
||
import com.prof18.rssparser.model.RssItem | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class ParsedItemTest { | ||
@Test | ||
fun title_whenPresent() { | ||
val title = "My Plain Title" | ||
val item = RssItem.Builder().title(title).build() | ||
val parsedItem = ParsedItem(item, siteURL = "") | ||
|
||
assertEquals(expected = title, actual = parsedItem.title) | ||
} | ||
|
||
@Test | ||
fun title_whenPresentAndHTML() { | ||
val title = "My <i>Fancy</i> Title" | ||
val item = RssItem.Builder().title(title).build() | ||
val parsedItem = ParsedItem(item, siteURL = "") | ||
|
||
assertEquals(expected = "My Fancy Title", actual = parsedItem.title) | ||
} | ||
|
||
@Test | ||
fun title_whenNull() { | ||
val item = RssItem.Builder().title(null).build() | ||
val parsedItem = ParsedItem(item, siteURL = "") | ||
|
||
assertEquals(expected = "", actual = parsedItem.title) | ||
} | ||
|
||
@Test | ||
fun url_whenPresent() { | ||
val url = "https://example.com/article" | ||
val item = RssItem.Builder().link(url).build() | ||
val parsedItem = ParsedItem(item, siteURL = "") | ||
|
||
assertEquals(expected = url, actual = parsedItem.url) | ||
} | ||
|
||
@Test | ||
fun url_whenNull() { | ||
val item = RssItem.Builder().link(null).build() | ||
val parsedItem = ParsedItem(item, siteURL = "") | ||
|
||
assertEquals(expected = null, actual = parsedItem.url) | ||
} | ||
|
||
@Test | ||
fun url_whenBlank() { | ||
val item = RssItem.Builder().link("").build() | ||
val parsedItem = ParsedItem(item, siteURL = "") | ||
|
||
assertEquals(expected = null, actual = parsedItem.url) | ||
} | ||
|
||
@Test | ||
fun url_withRelativePathMissingSiteURL() { | ||
val item = RssItem.Builder().link("/article").build() | ||
val parsedItem = ParsedItem(item, siteURL = "") | ||
|
||
assertEquals(expected = null, actual = parsedItem.url) | ||
} | ||
|
||
@Test | ||
fun url_withRelativePathAndInvalidSiteURL() { | ||
val item = RssItem.Builder().link("/article").build() | ||
val parsedItem = ParsedItem(item, siteURL = "wrong") | ||
|
||
assertEquals(expected = null, actual = parsedItem.url) | ||
} | ||
|
||
@Test | ||
fun url_withRelativePathAndValidSiteURL() { | ||
val item = RssItem.Builder().link("/article").build() | ||
val parsedItem = ParsedItem(item, siteURL = "https://example.com") | ||
|
||
assertEquals(expected = "https://example.com/article", actual = parsedItem.url) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters